如何在c#中通过lpEnvironment调用CreateProcess()
How to call CreateProcess() in C# passing lpEnvironment
我将本地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 COR_PROFILER=PROFILER_GUID COR_PROFILER_PATH=GetProfilerFullPat "
我得到一个错误值超出允许范围
任何建议如何通过环境块从c#到c++ dll?
好的,我设法解决了我的问题。首先,我使用StringBuilder而不是IntPtr。添加字符串"COR_ENABLE_PROFILING=1 COR_PROFILER=PROFILER_GUID COR_PROFILER_PATH=GetProfilerFullPat "
我只是添加("COR_ENABLE_PROFILING=1")并增加Stringbuilder的长度+ 1等…;结束应该再增加一次,长度为++(这是ANSI编码);第二件事是更改并将封送添加到导入的方法中代替[In] IntPtr lpEnvironment
添加[In, MarshalAs(UnmanagedType.LPStr)] StringBuilder lpEnvironment
没有必要为此使用p/Invoke,您可以使用。net Process。
如果你想自定义进程启动,传递环境变量等,使用一个接受ProcessStartInfo对象的重载。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- std::cout.imbue()多重调用
- 如何在c#中通过lpEnvironment调用CreateProcess()