static Application() { // get hInstance #if !NET_STANDARD_20 if (hInstance == HINSTANCE.Zero) { #if CS2X hInstance = Marshal.GetHINSTANCE(); #else hInstance = Marshal.GetHINSTANCE(typeof(Application).Module); #endif } #endif // get nCmdShow if (nCmdShow == 0) { const int STARTF_USESHOWWINDOW = 0x00000001; const int SW_SHOWDEFAULT = 10; var info = new STARTUPINFOA(); GetStartupInfoA(&info); if ((info.dwFlags & STARTF_USESHOWWINDOW) == 0) { nCmdShow = SW_SHOWDEFAULT; } else { nCmdShow = info.wShowWindow; } } }
public static extern bool CreateProcessA(string lpApplicationName, string lpCommandLine, SECURITY_ATTRIBUTES lpProcessAttributes, SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, CreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, STARTUPINFOA lpStartupInfo, PROCESS_INFORMATION lpProcessInformation );
public static void CreateProcessAsUser(SafeObjectHandle handle, string exePath, string cmdLine) { if (!string.IsNullOrEmpty(cmdLine)) { cmdLine = " " + cmdLine; } IntPtr pEnv = IntPtr.Zero; const int CREATE_UNICODE_ENVIRONMENT = 0x00000400; const int NORMAL_PRIORITY_CLASS = 0x00000020; const int CREATE_NEW_CONSOLE = 0x00000010; int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT; if (!handle.IsInvalid) { CreateEnvironmentBlock(out pEnv, handle, true); } PROCESS_INFORMATION pi; STARTUPINFOA si = new STARTUPINFOA(); si.cb = (uint)Marshal.SizeOf(si); //si.lpDesktop = Marshal.StringToHGlobalAnsi("winsta0\\default"); si.lpDesktop = "winsta0\\default"; IntPtr pHandle = IntPtr.Zero; if (!handle.IsInvalid) { pHandle = handle.DangerousGetHandle(); } if (!CreateProcessAsUser(pHandle , exePath , cmdLine , IntPtr.Zero //进程安全属性 , IntPtr.Zero // 线程安全属性 , false // 句柄不可继承 , (CreateProcessFlags)dwCreationFlags // 创建标识 , pEnv // 环境信息 , null // 当前路径 , ref si , out pi)) { var errorCode = GetLastError(); throw new Exception("CreateProcessAsUser 失败"); } }
static extern bool CreateProcessAsUser(IntPtr hToken, string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, CreateProcessFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFOA lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);