Repository: dotnet/roslyn
File: src/Compilers/Shared/BuildServerConnection.cs (C#)

Code before:
                {
                    if (environmentVariables != null)
                    {
                        environmentBlockPtr = CreateEnvironmentBlock(environmentVariables);
                    }

                    bool success = CreateProcess(
                        lpApplicationName: null,
                        lpCommandLine: builder,
                        lpProcessAttributes: NullPtr,
                        lpThreadAttributes: NullPtr,
                        bInheritHandles: false,
                        dwCreationFlags: dwCreationFlags,
                        lpEnvironment: environmentBlockPtr,
                        lpCurrentDirectory: clientDirectory,
                        lpStartupInfo: ref startInfo,
                        lpProcessInformation: out processInfo);

                    if (success)
                    {
                        logger.Log("Successfully created process with process id {0}", processInfo.dwProcessId);
                        CloseHandle(processInfo.hProcess);
                        CloseHandle(processInfo.hThread);
                        processId = processInfo.dwProcessId;
                    }
                    else
                    {
                        logger.LogError("Failed to create process. GetLastError={0}", Marshal.GetLastWin32Error());
                    }
                    return success;
                }
                finally
                {
                    if (environmentBlockPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(environmentBlockPtr);
                    }
                }
            }
            else
            {
                try
                {
                    var startInfo = new ProcessStartInfo()
                    {
                        FileName = serverInfo.processFilePath,
                        Arguments = serverInfo.commandLineArguments,
                        UseShellExecute = false,
                        WorkingDirectory = clientDirectory,
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,

Review comment (bug):
@copilot the `CreateProcess` call is failing with error code 57 which I assume is `ERROR_INVALID_PARAMETER`. Did you need to set a flag since this is passing UTF-16 encoded strings for the environment block?

Code after:

                PROCESS_INFORMATION processInfo;

                var builder = new StringBuilder($@"""{serverInfo.processFilePath}"" {serverInfo.commandLineArguments}");

                IntPtr environmentBlockPtr = IntPtr.Zero;
                try
                {
                    if (environmentVariables != null)
                    {
                        environmentBlockPtr = CreateEnvironmentBlock(environmentVariables);
                        // When passing a Unicode environment block, we must set the CREATE_UNICODE_ENVIRONMENT flag
                        dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT;
                    }

                    bool success = CreateProcess(
                        lpApplicationName: null,
                        lpCommandLine: builder,
                        lpProcessAttributes: NullPtr,
                        lpThreadAttributes: NullPtr,
                        bInheritHandles: false,
                        dwCreationFlags: dwCreationFlags,
                        lpEnvironment: environmentBlockPtr,
                        lpCurrentDirectory: clientDirectory,
                        lpStartupInfo: ref startInfo,
                        lpProcessInformation: out processInfo);

                    if (success)
                    {
                        logger.Log("Successfully created process with process id {0}", processInfo.dwProcessId);
                        CloseHandle(processInfo.hProcess);
                        CloseHandle(processInfo.hThread);
                        processId = processInfo.dwProcessId;
                    }
                    else
                    {
                        logger.LogError("Failed to create process. GetLastError={0}", Marshal.GetLastWin32Error());
                    }
                    return success;
                }
                finally
                {
                    if (environmentBlockPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(environmentBlockPtr);
                    }
                }
            }
            else
            {
                try