简体   繁体   English

CreateProcessWithTokenW-C#中的用法示例

[英]CreateProcessWithTokenW - Example of usage in C#

I'm trying to use the CreateProcessWithTokenW() win32 API function to start a new process with a token. 我正在尝试使用CreateProcessWithTokenW() win32 API函数来启动带有令牌的新进程。 The problem is that I'm quite new to the win32 API and I have no idea how to use the function properly, and which structs etc. that are needed. 问题是我对win32 API还是很陌生,我也不知道如何正确使用该函数以及所需的结构等。 Could someone provide me with an example of how to use the function correctly in C#? 有人可以为我提供一个如何在C#中正确使用该函数的示例吗?

This is unmanaged code so you need to use P/Invoke (Platform Invoke), here's the function signature for CreateProcessWithTokenW() : 这是非托管代码,因此您需要使用P / Invoke(平台调用),这是CreateProcessWithTokenW()的函数签名:

[DllImport("advapi32", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool CreateProcessWithTokenW(
    IntPtr hToken, 
    LogonFlags dwLogonFlags, 
    string lpApplicationName, 
    string lpCommandLine, 
    CreationFlags dwCreationFlags, 
    IntPtr lpEnvironment, 
    string lpCurrentDirectory, 
    [In] ref STARTUPINFO lpStartupInfo, 
    out PROCESS_INFORMATION lpProcessInformation);

You can use an enum like this to pass in the LogonFlags param (to keep the .net feeling :) ) : 您可以使用这样的枚举来传递LogonFlags参数(以保持.net的感觉:)):

public enum LogonFlags
{
     WithProfile = 1,
     NetCredentialsOnly
}

Here's the enum for the CreationFlags following the documentation available here : 以下是此处提供的文档之后的CreationFlags枚举:

public enum CreationFlags
{
    DefaultErrorMode = 0x04000000,
    NewConsole = 0x00000010,
    NewProcessGroup = 0x00000200,
    SeparateWOWVDM = 0x00000800,
    Suspended = 0x00000004,
    UnicodeEnvironment = 0x00000400,
    ExtendedStartupInfoPresent = 0x00080000
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM