windows 服務啟動外部程式

來源:互聯網
上載者:User

標籤: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 服務啟動外部程式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.