.NET:如何从 C# 中使用 STARTUPINFOEX 调用 CreateProcessAsUser()
Web 上用于调用 CreateProcessAsUser() 的大多数示例代码都具有与以下类似的 PInvoke 签名:
public static extern bool CreateProcessAsUser(IntPtr hToken,
string lpApplicationName,
string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
int creationFlags,
IntPtr environment,
string currentDirectory,
ref STARTUPINFO startupInfo,
out PROCESS_INFORMATION processInfo);
问题是,我想传递 STARTUPINFOEX,这在 Vista/W7 中是允许的。如果我正在编写 C/C++ 那么我可以直接转换它。
我应该如何在 C# 中处理这个问题? STARTUPINFOEX 看起来像这样:
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFOEX
{
public STARTUPINFO StartupInfo;
public IntPtr lpAttributeList;
};
我应该将 IntPtr 传递给结构吗?如果我更改 Pinvoke 签名并执行类似的操作...
IntPtr pStartupInfoEx = Marshal.AllocHGlobal(Marshal.SizeOf(startupInfoEx));
Marshal.StructureToPtr(startupInfoEx, pStartupInfoEx, true);
.. 当我执行 CreateProcessAsUser() 时,我的 shell 会崩溃:(
感谢收到任何想法。谢谢。
Most sample code on the web for calling CreateProcessAsUser() has a similar PInvoke signature to the following:
public static extern bool CreateProcessAsUser(IntPtr hToken,
string lpApplicationName,
string lpCommandLine,
ref SECURITY_ATTRIBUTES lpProcessAttributes,
ref SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
int creationFlags,
IntPtr environment,
string currentDirectory,
ref STARTUPINFO startupInfo,
out PROCESS_INFORMATION processInfo);
The problem is, I want to pass STARTUPINFOEX instead, which is allowed in Vista/W7. If I was writing C/C++ then I could just cast it.
How should I deal with this in C#? STARTUPINFOEX looks like this:
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFOEX
{
public STARTUPINFO StartupInfo;
public IntPtr lpAttributeList;
};
Should I be passing an IntPtr to the structure instead? If I change the Pinvoke signature and do something like this...
IntPtr pStartupInfoEx = Marshal.AllocHGlobal(Marshal.SizeOf(startupInfoEx));
Marshal.StructureToPtr(startupInfoEx, pStartupInfoEx, true);
.. I crash my shell when I execute the CreateProcessAsUser() :(
Any ideas gratefully received. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该像 STARTUPINFO 一样传递它,我的意思是使用
ref
关键字。该结构的编组与 STARTUPINFO 的编组方式相同。I think you should just pass it like the STARTUPINFO, I mean with the
ref
keyword. The marshaling of the structure is done the same way it is done for the STARTUPINFO.