亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c#中createprocessasuser怎么使用

小億
174
2023-08-02 16:41:40
欄目: 編程語言

在C#中使用CreateProcessAsUser函數可以創建一個新的進程,并以指定用戶的身份運行該進程。下面是一個示例代碼:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
IntPtr userToken = IntPtr.Zero;
IntPtr primaryToken = IntPtr.Zero;
try
{
// 獲取當前用戶的訪問令牌
bool success = LogonUser("<用戶名>", "<域>", "<密碼>", 2, 0, ref userToken);
if (!success)
{
throw new System.ComponentModel.Win32Exception();
}
// 創建一個與用戶訪問令牌相關聯的主訪問令牌
success = DuplicateToken(userToken, 2, ref primaryToken);
if (!success)
{
throw new System.ComponentModel.Win32Exception();
}
// 設置啟動信息
STARTUPINFO startupInfo = new STARTUPINFO();
startupInfo.cb = Marshal.SizeOf(startupInfo);
// 啟動新進程的信息
PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();
// 創建新進程
success = CreateProcessAsUser(primaryToken, "<要運行的程序路徑>", null, IntPtr.Zero, IntPtr.Zero, false,
0, IntPtr.Zero, null, ref startupInfo, out processInfo);
if (!success)
{
throw new System.ComponentModel.Win32Exception();
}
Console.WriteLine("新進程已啟動,進程ID為: " + processInfo.dwProcessId);
}
finally
{
// 關閉句柄
if (userToken != IntPtr.Zero)
{
CloseHandle(userToken);
}
if (primaryToken != IntPtr.Zero)
{
CloseHandle(primaryToken);
}
}
}
// 導入Windows API
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL,
ref IntPtr DuplicateTokenHandle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool CreateProcessAsUser(IntPtr hToken, string lpApplicationName, string lpCommandLine,
IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags,
IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct STARTUPINFO
{
public int cb;
public String lpReserved;
public String lpDesktop;
public String lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
}

注意替換代碼中的<用戶名><域><密碼><要運行的程序路徑>為實際的值。

0
康定县| 阳泉市| 南华县| 大同县| 扎鲁特旗| 德惠市| 吉安市| 阳泉市| 霍山县| 湾仔区| 股票| 阿拉善盟| 琼中| 通化市| 房山区| 景东| 绩溪县| 喜德县| 浮梁县| 奎屯市| 无极县| 建阳市| 广宗县| 石屏县| 繁峙县| 洱源县| 漠河县| 富裕县| 凤冈县| 大同县| 皮山县| 广水市| 庆阳市| 昌平区| 南部县| 清流县| 黎平县| 读书| 安徽省| 肥乡县| 洛宁县|