From c4c2ced167a578163aa0266f5d847e0c2057d207 Mon Sep 17 00:00:00 2001 From: KitasaitamalBlues Date: Thu, 20 Jun 2024 17:51:38 +0800 Subject: [PATCH 1/2] fix createProcess errorcode 82 when environment is NULL --- src/flutter_pty_win.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/flutter_pty_win.c b/src/flutter_pty_win.c index b1f85bc..d506353 100644 --- a/src/flutter_pty_win.c +++ b/src/flutter_pty_win.c @@ -86,10 +86,10 @@ static LPWSTR build_environment(char **environment) environment_block_length += (int)strlen(environment[i]) + 1; i++; } + + environment_block = malloc((environment_block_length + 1) * sizeof(WCHAR)); } - environment_block = malloc((environment_block_length + 1) * sizeof(WCHAR)); - if (environment_block != NULL) { int i = 0; @@ -362,7 +362,7 @@ FFI_PLUGIN_EXPORT PtyHandle *pty_create(PtyOptions *options) PROCESS_INFORMATION processInfo; ZeroMemory(&processInfo, sizeof(processInfo)); - Sleep(1000); + // Sleep(1000); ok = CreateProcessW(NULL, command, From d9c34fc495c9b35b2f38c0b56f93b98300c66896 Mon Sep 17 00:00:00 2001 From: KitasaitamalBlues Date: Fri, 21 Jun 2024 10:17:09 +0800 Subject: [PATCH 2/2] Fixed the problem that the parent environment variable block was not inherited when there were environment variables, which caused the PowerShell to fail to be created. --- src/flutter_pty_win.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/flutter_pty_win.c b/src/flutter_pty_win.c index d506353..0e6d30a 100644 --- a/src/flutter_pty_win.c +++ b/src/flutter_pty_win.c @@ -86,7 +86,7 @@ static LPWSTR build_environment(char **environment) environment_block_length += (int)strlen(environment[i]) + 1; i++; } - + environment_block = malloc((environment_block_length + 1) * sizeof(WCHAR)); } @@ -317,7 +317,7 @@ FFI_PLUGIN_EXPORT PtyHandle *pty_create(PtyOptions *options) return NULL; } - STARTUPINFOEX startupInfo; + STARTUPINFOEXW startupInfo; ZeroMemory(&startupInfo, sizeof(startupInfo)); startupInfo.StartupInfo.cb = sizeof(startupInfo); @@ -361,7 +361,33 @@ FFI_PLUGIN_EXPORT PtyHandle *pty_create(PtyOptions *options) PROCESS_INFORMATION processInfo; ZeroMemory(&processInfo, sizeof(processInfo)); + // merge parent environment + if (environment_block != NULL) + { + LPWSTR temp = environment_block; + environment_block = NULL; + LPWSTR parentEnvBlock = GetEnvironmentStringsW(); + int parent_envBlock_size = 0; + LPWSTR env = parentEnvBlock; + while (*env) + { + parent_envBlock_size += wcslen(env) + 1; + env += wcslen(env) + 1; + } + int new_env_size = 0; + LPWSTR new_env = temp; + while (*new_env) + { + new_env_size += wcslen(new_env) + 1; + new_env += wcslen(new_env) + 1; + } + int _malloc_size = sizeof(WCHAR) * (parent_envBlock_size + new_env_size + 1); + environment_block = malloc(_malloc_size); + memset(environment_block, 0, _malloc_size); + memcpy(environment_block, parentEnvBlock, sizeof(WCHAR) * parent_envBlock_size); + memcpy(environment_block + parent_envBlock_size, temp, sizeof(WCHAR) * new_env_size); + } // Sleep(1000); ok = CreateProcessW(NULL,