標籤:sage stop 文章 proc token rsh temp string ons
服務使用Process啟動外部程式沒表單
在WinXP和Win2003環境中,安裝服務後,按右鍵服務“屬性”-“登入”選項卡-選擇“本地系統帳戶”並勾選“允許服務與案頭互動”即可.
在Win7及以後的環境中,由於微軟加強了許可權管理,將此功能禁用,需要引用第三方dll (Cjwdev.WindowsApi.dll)
連結: https://pan.baidu.com/s/1P6bBHI_AkOOTxvRTHh06aw 密碼: phdj
具體原因分析可參考此文章 :http://www.cnblogs.com/gnielee/archive/2010/04/07/session0-isolation-part1.html
public void AppStart(string appPath,string parms) { 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, parms, 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; temppid = _currentAquariusProcessId; WriteLog("temppid", _currentAquariusProcessId.ToString()); } catch (Exception ex) { WriteLog("AppStart", ex.Message); } }
CreateProcessAsUser
關於參數 lpCommandLine,這裡可以放你給被建立進程傳遞的參數。 注意 C程式在傳遞參數時需要在參數前加一個空格,因為c程式main函數入參預設第一個參數是模組名稱,從第二個參數開始才是你想要傳遞進去的參數,而參數以空格分隔 所以你要加一個空格啦。
關於通過Bat檔案安裝卸載服務
cd /d %~dp0sc create MonitorService binpath="%cd%\WindowsService1.exe" start= autosc start MonitorServicepause
sc stop MonitorService sc delete MonitorService pause
windows 服務啟動外部程式