private void OpenApp4Service(string appName, string cmdLine) { try { //string appStartPath = @"C:\\Deepleo.exe"; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser(userTokenHandle, appName, cmdLine, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } //int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { _log.ErrorFormat("Start Application failed, its path is {0} ,exception: {1}", appName, ex.Message); } }
/// <summary> /// 打开应用程序 /// </summary> /// <param name="appPath"></param> public static void AppStart(string appPath) { try { string appStartPath = appPath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) ApiDefinitions.CloseHandle(userTokenHandle); int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { LogHelpr.Error("打开程序时出错:" + ex.ToString()); } }
/// <summary> /// 切换进程2 /// </summary> /// <param name="killAppName"></param> /// <param name="openAppName"></param> /// <param name="openAppNamePath"></param> /// <param name="args"></param> /// <returns></returns> public static void ToggleProcess4UserSession(string killAppName, string openAppName, string openAppNamePath, string args) { //获得到进程,杀死进程 if (!string.IsNullOrEmpty(killAppName)) { Process[] processes = Process.GetProcessesByName(killAppName); if (processes.Length > 0) { _log.Info(string.Format("Kill进程:{0}", killAppName)); foreach (Process process in processes) { process.Kill(); } } } //判断要开启的进程是否已经开启,已经开启则无需开启 var startProcess = Process.GetProcessesByName(openAppName); if (startProcess.Length == 0) { if (!string.IsNullOrEmpty(openAppNamePath)) { //启动进程 if (File.Exists(openAppNamePath)) { try { IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser(userTokenHandle, openAppNamePath, args, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } //int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { _log.ErrorFormat("Start Application failed, its path is {0} ,exception: {1}", openAppNamePath, ex.Message); } } } } }
/// <summary> /// 服务启动程序 显示界面 /// </summary> private void HasUI() { string appName = "demo-winform"; string appPath = "G://demo-winform.exe"; Process[] localByName = Process.GetProcessesByName(appName); if (localByName.Length == 0) //如果得到的进程数是0, 那么说明程序未启动,需要启动程序 { try { IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { Console.WriteLine("启动程序失败!" + ex); } } else { //如果程序已经启动,则执行这一部分代码 } }
private int OpenForm(string filePath) { try { string appStartPath = filePath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)System.Runtime.InteropServices.Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } int processId = (int)procInfo.dwProcessId; return(processId); } catch (Exception ex) { Logs.Log("", "调用Cjwdev.WindowsApi时出错:" + ex.ToString()); } return(0); }
public void AppStart(string appPath) { try { string appStartPath = appPath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); logger("AppStart failed! " + ex.ToString()); } }