我将 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/