中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

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
吕梁市| 房山区| 黄山市| 屏边| 徐汇区| 苗栗县| 米泉市| 华亭县| 中西区| 阜平县| 呼伦贝尔市| 荆门市| 桐梓县| 同江市| 阳谷县| 页游| 高州市| 赤水市| 新巴尔虎右旗| 布拖县| 石首市| 白水县| 体育| 文安县| 思茅市| 澄江县| 龙里县| 中宁县| 麻栗坡县| 沙湾县| 陕西省| 黄平县| 花垣县| 巩义市| 成武县| 桂东县| 阳山县| 砀山县| 江山市| 尚义县| 甘谷县|