91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
浦江县| 沈丘县| 马山县| 将乐县| 三亚市| 克山县| 崇明县| 长乐市| 克什克腾旗| 安远县| 双牌县| 鹰潭市| 荔波县| 洞头县| 温宿县| 遂溪县| 尉犁县| 濮阳市| 西藏| 定远县| 察隅县| 台州市| 宁明县| 靖州| 来宾市| 石棉县| 武邑县| 海口市| 安多县| 宜川县| 宁河县| 铜陵市| 文登市| 大石桥市| 青浦区| 土默特右旗| 永安市| 翁牛特旗| 庆城县| 开江县| 怀远县|