c# - 如何通过 lpEnvironment 在 C# 中调用 CreateProcess()

标签 c# c++ winapi environment-variables clr

我将 native CreateProcess 导入到我的 C# 项目中 用于 ICorDebug 目的 http://msdn.microsoft.com/en-us/library/vstudio/ms232508(v=vs.100).aspx

 [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        void CreateProcess([In, MarshalAs(UnmanagedType.LPWStr)] string lpApplicationName, [In, MarshalAs(UnmanagedType.LPWStr)] string lpCommandLine, [In] SECURITY_ATTRIBUTES lpProcessAttributes, [In] SECURITY_ATTRIBUTES lpThreadAttributes, [In] int bInheritHandles, [In] uint dwCreationFlags, [In] IntPtr lpEnvironment, [In, MarshalAs(UnmanagedType.LPWStr)] string lpCurrentDirectory, [In] STARTUPINFO pStartupInfo, [In] PROCESS_INFORMATION pProcessInformation, [In] CorDebugCreateProcessFlags debuggingFlags, [MarshalAs(UnmanagedType.Interface)] out ICorDebugProcess ppProcess);

我称之为尝试以这种方式传递 lpEnvironment

            IntPtr intPtrEnv;
            if (variables != string.Empty)
                intPtrEnv = Marshal.StringToHGlobalUni(variables);
            else
                intPtrEnv = new IntPtr(0);

 p_codebugger.CreateProcess(
                                exepath,
                                exepath,
                                null,
                                null,
                                1, // inherit handles
                                (UInt32)CreateProcessFlags.CREATE_NEW_CONSOLE,
                                intPtrEnv,
                                ".",
                                si,
                                pi,
                                CorDebugCreateProcessFlags.DEBUG_NO_SPECIAL_OPTIONS,
                                out proc);

变量字符串包含:

"COR_ENABLE_PROFILING=1\0COR_PROFILER=PROFILER_GUID\0COR_PROFILER_PATH=GetProfilerFullPat\0\0"

我得到一个错误 值超出允许范围

关于如何将环境 block 从 c# 传递到 c++ dll 有什么建议吗?

最佳答案

好的,我设法解决了我的问题。 首先,我使用 StringBuilder 而不是 IntPtr。 添加字符串 "COR_ENABLE_PROFILING=1\0COR_PROFILER=PROFILER_GUID\0COR_PROFILER_PATH=GetProfilerFullPat\0\0" 我只需添加(“COR_ENABLE_PROFILING=1”)并增加 Stringbuilder 长度 + 1 等...; 末尾应该再增加一次 lenght++(这是 ansi 编码); 第二件事是更改并将编码添加到导入的方法中 而不是 [In] IntPtr lpEnvironment 添加 [In, MarshalAs(UnmanagedType.LPStr)] StringBuilder lpEnvironment

关于c# - 如何通过 lpEnvironment 在 C# 中调用 CreateProcess(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25353445/

相关文章:

c# - “IApplicationBuilder”不包含 'MapAzureSignalR' 的定义

c++ - 字符数组被分解为 3 个整型变量

c++ - ISO 8601 时间戳 C++

c++ - C++ 代码生成的优雅方式

c++ - 使用 WinAPI 的简单 AES 加密

c - 编辑窗口在一定限制后停止显示文本。 ( Windows )

c# - 为什么没有 linq for byte, uint?

c# - 用于检查数字负性的右移运算符

c# - 创建两个日期之间所有日期的数组或列表

c++ - GetOpenFileName 在默认目录 'Computer' 下打开可能吗?