|
| 1 | +#define WIN32_LEAN_AND_MEAN |
| 2 | +#include <windows.h> |
| 3 | + |
| 4 | +#include <shellapi.h> |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include <string_view> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace { |
| 11 | + |
| 12 | +std::wstring modulePath() |
| 13 | +{ |
| 14 | + std::wstring path(260, L'\0'); |
| 15 | + for(;;) { |
| 16 | + const auto length = |
| 17 | + GetModuleFileNameW(nullptr, path.data(), static_cast<DWORD>(path.size())); |
| 18 | + if(length == 0) { |
| 19 | + return {}; |
| 20 | + } |
| 21 | + if(length < path.size()) { |
| 22 | + path.resize(length); |
| 23 | + return path; |
| 24 | + } |
| 25 | + path.resize(path.size() * 2); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +std::wstring parentDirectory(std::wstring_view path) |
| 30 | +{ |
| 31 | + const auto separator = path.find_last_of(L"\\/"); |
| 32 | + if(separator == std::wstring_view::npos) { |
| 33 | + return L"."; |
| 34 | + } |
| 35 | + return std::wstring(path.substr(0, separator)); |
| 36 | +} |
| 37 | + |
| 38 | +std::wstring joinPath(std::wstring_view left, std::wstring_view right) |
| 39 | +{ |
| 40 | + if(left.empty()) { |
| 41 | + return std::wstring(right); |
| 42 | + } |
| 43 | + |
| 44 | + std::wstring path(left); |
| 45 | + if(path.back() != L'\\' && path.back() != L'/') { |
| 46 | + path.push_back(L'\\'); |
| 47 | + } |
| 48 | + path.append(right); |
| 49 | + return path; |
| 50 | +} |
| 51 | + |
| 52 | +bool fileExists(std::wstring_view path) |
| 53 | +{ |
| 54 | + const auto attributes = GetFileAttributesW(std::wstring(path).c_str()); |
| 55 | + return attributes != INVALID_FILE_ATTRIBUTES && |
| 56 | + (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0; |
| 57 | +} |
| 58 | + |
| 59 | +std::wstring quoteCommandLineArgument(std::wstring_view argument) |
| 60 | +{ |
| 61 | + if(!argument.empty() && argument.find_first_of(L" \t\n\v\"") == std::wstring_view::npos) { |
| 62 | + return std::wstring(argument); |
| 63 | + } |
| 64 | + |
| 65 | + std::wstring quoted; |
| 66 | + quoted.push_back(L'"'); |
| 67 | + |
| 68 | + auto backslashes = std::size_t{0}; |
| 69 | + for(const auto character : argument) { |
| 70 | + if(character == L'\\') { |
| 71 | + ++backslashes; |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + if(character == L'"') { |
| 76 | + quoted.append((backslashes * 2) + 1, L'\\'); |
| 77 | + quoted.push_back(character); |
| 78 | + backslashes = 0; |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + quoted.append(backslashes, L'\\'); |
| 83 | + backslashes = 0; |
| 84 | + quoted.push_back(character); |
| 85 | + } |
| 86 | + |
| 87 | + quoted.append(backslashes * 2, L'\\'); |
| 88 | + quoted.push_back(L'"'); |
| 89 | + return quoted; |
| 90 | +} |
| 91 | + |
| 92 | +std::wstring formatWindowsError(DWORD errorCode) |
| 93 | +{ |
| 94 | + wchar_t* rawMessage = nullptr; |
| 95 | + const auto length = FormatMessageW( |
| 96 | + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 97 | + FORMAT_MESSAGE_IGNORE_INSERTS, |
| 98 | + nullptr, |
| 99 | + errorCode, |
| 100 | + 0, |
| 101 | + reinterpret_cast<wchar_t*>(&rawMessage), |
| 102 | + 0, |
| 103 | + nullptr); |
| 104 | + |
| 105 | + std::wstring message = |
| 106 | + length == 0 ? L"Unknown Windows error." : std::wstring(rawMessage, length); |
| 107 | + if(rawMessage != nullptr) { |
| 108 | + LocalFree(rawMessage); |
| 109 | + } |
| 110 | + |
| 111 | + while(!message.empty() && |
| 112 | + (message.back() == L'\r' || message.back() == L'\n' || message.back() == L' ')) { |
| 113 | + message.pop_back(); |
| 114 | + } |
| 115 | + return message; |
| 116 | +} |
| 117 | + |
| 118 | +bool launcherMessageBoxesAreDisabled() |
| 119 | +{ |
| 120 | + const auto required = |
| 121 | + GetEnvironmentVariableW(L"REMBG_GUI_LAUNCHER_SILENT", nullptr, 0); |
| 122 | + if(required == 0) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + std::wstring value(required, L'\0'); |
| 127 | + const auto length = GetEnvironmentVariableW( |
| 128 | + L"REMBG_GUI_LAUNCHER_SILENT", value.data(), static_cast<DWORD>(value.size())); |
| 129 | + return length > 0 && value.front() == L'1'; |
| 130 | +} |
| 131 | + |
| 132 | +int failWithMessage(std::wstring_view message) |
| 133 | +{ |
| 134 | + if(!launcherMessageBoxesAreDisabled()) { |
| 135 | + MessageBoxW(nullptr, std::wstring(message).c_str(), L"Rembg GUI", MB_ICONERROR | MB_OK); |
| 136 | + } |
| 137 | + return 1; |
| 138 | +} |
| 139 | + |
| 140 | +std::wstring buildChildCommandLine(std::wstring_view childPath) |
| 141 | +{ |
| 142 | + auto commandLine = quoteCommandLineArgument(childPath); |
| 143 | + |
| 144 | + auto argc = 0; |
| 145 | + const auto argv = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 146 | + if(argv == nullptr) { |
| 147 | + return commandLine; |
| 148 | + } |
| 149 | + |
| 150 | + for(auto index = 1; index < argc; ++index) { |
| 151 | + commandLine.push_back(L' '); |
| 152 | + commandLine.append(quoteCommandLineArgument(argv[index])); |
| 153 | + } |
| 154 | + |
| 155 | + LocalFree(argv); |
| 156 | + return commandLine; |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace |
| 160 | + |
| 161 | +int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) |
| 162 | +{ |
| 163 | + const auto launcherPath = modulePath(); |
| 164 | + if(launcherPath.empty()) { |
| 165 | + return failWithMessage(L"Rembg GUI could not find its install folder."); |
| 166 | + } |
| 167 | + |
| 168 | + const auto bundleRoot = parentDirectory(launcherPath); |
| 169 | + const auto childPath = joinPath(bundleRoot, L"bin\\rembg-gui-app.exe"); |
| 170 | + |
| 171 | + if(!fileExists(childPath)) { |
| 172 | + return failWithMessage( |
| 173 | + L"Rembg GUI could not find bin\\rembg-gui-app.exe.\n\n" |
| 174 | + L"Extract the whole zip file first, then run rembg-gui.exe from the " |
| 175 | + L"extracted folder.\n\n" |
| 176 | + L"Do not move rembg-gui.exe away from the bin, Qt6, and translations folders."); |
| 177 | + } |
| 178 | + |
| 179 | + auto commandLine = buildChildCommandLine(childPath); |
| 180 | + std::vector<wchar_t> mutableCommandLine(commandLine.begin(), commandLine.end()); |
| 181 | + mutableCommandLine.push_back(L'\0'); |
| 182 | + |
| 183 | + STARTUPINFOW startupInfo{}; |
| 184 | + startupInfo.cb = sizeof(startupInfo); |
| 185 | + PROCESS_INFORMATION processInfo{}; |
| 186 | + |
| 187 | + const auto started = CreateProcessW( |
| 188 | + childPath.c_str(), |
| 189 | + mutableCommandLine.data(), |
| 190 | + nullptr, |
| 191 | + nullptr, |
| 192 | + FALSE, |
| 193 | + 0, |
| 194 | + nullptr, |
| 195 | + bundleRoot.c_str(), |
| 196 | + &startupInfo, |
| 197 | + &processInfo); |
| 198 | + |
| 199 | + if(started == FALSE) { |
| 200 | + return failWithMessage( |
| 201 | + L"Rembg GUI could not start bin\\rembg-gui-app.exe.\n\n" + |
| 202 | + formatWindowsError(GetLastError())); |
| 203 | + } |
| 204 | + |
| 205 | + const auto waitResult = WaitForSingleObject(processInfo.hProcess, INFINITE); |
| 206 | + auto exitCode = DWORD{1}; |
| 207 | + if(waitResult == WAIT_OBJECT_0) { |
| 208 | + GetExitCodeProcess(processInfo.hProcess, &exitCode); |
| 209 | + } else { |
| 210 | + failWithMessage(L"Rembg GUI lost track of the app process.\n\n" + |
| 211 | + formatWindowsError(GetLastError())); |
| 212 | + } |
| 213 | + |
| 214 | + CloseHandle(processInfo.hThread); |
| 215 | + CloseHandle(processInfo.hProcess); |
| 216 | + return static_cast<int>(exitCode); |
| 217 | +} |
0 commit comments