Create process with wide chars on Windows

Windows does not support UTF-8, so pushing a file with non-ASCII
characters failed.

Convert the UTF-8 command line to a wide characters string and call
CreateProcessW().

Fixes <https://github.com/Genymobile/scrcpy/issues/422>
This commit is contained in:
Romain Vimont
2019-02-10 12:53:03 +01:00
parent c0b65b14df
commit 477c0a2cab
3 changed files with 40 additions and 2 deletions

View File

@@ -18,7 +18,7 @@ static int build_cmd(char *cmd, size_t len, const char *const argv[]) {
}
enum process_result cmd_execute(const char *path, const char *const argv[], HANDLE *handle) {
STARTUPINFO si;
STARTUPINFOW si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
@@ -29,12 +29,19 @@ enum process_result cmd_execute(const char *path, const char *const argv[], HAND
return PROCESS_ERROR_GENERIC;
}
wchar_t *wide = utf8_to_wide_char(cmd);
if (!wide) {
LOGC("Cannot allocate wide char string");
return PROCESS_ERROR_GENERIC;
}
#ifdef WINDOWS_NOCONSOLE
int flags = CREATE_NO_WINDOW;
#else
int flags = 0;
#endif
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) {
if (!CreateProcessW(NULL, wide, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) {
free(wide);
*handle = NULL;
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
return PROCESS_ERROR_MISSING_BINARY;
@@ -42,6 +49,7 @@ enum process_result cmd_execute(const char *path, const char *const argv[], HAND
return PROCESS_ERROR_GENERIC;
}
free(wide);
*handle = pi.hProcess;
return PROCESS_SUCCESS;
}