Windows服務啟動進程----Cjwdev.WindowsApi.dll


windows服務無法啟動外部程

做一個windows服務監聽服務,涉及到windows服務啟動外部程序的一個過程,但是調試測試發現,無法簡單的用process.start()這種方法,

原因是在vista和win7這樣的系統下,服務是運行在session 0,而基本上應用是運行在session 1,所以windows服務下無法啟動process.start()。

網上看到windows服務下無法啟動外部程序的原因,解決方案

1、在WinXP和Win2003環境中,安裝服務后,右鍵單擊服務“屬性”-“登錄”選項卡-選擇“本地系統帳戶”並勾選“允許服務與桌面交互”即可.

2、在Win7環境中,由於微軟加強了權限管理,將此功能禁用,需要引用第三方dll,即Cjwdev.WindowsApi.dll

Cjwdev.WindowsApi.xml,dll下載鏈接:http://pan.baidu.com/share/link?shareid=159544&uk=3288736938

解決方法:使用這樣一個第三方庫,Cjwdev.WindowsApi.dll,能夠從服務啟動外部程序;

 

public static void Openlocalexe(string path)
{

int _currentAquariusProcessID;
/*appStartpath設置為全路徑地址*/
string appStartpath = path;
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);
try
{
ApiDefinitions.CreateProcessAsUser(userTokenHandle, appStartpath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startinfo, out procinfo);
if (userTokenHandle != IntPtr.Zero)
ApiDefinitions.CloseHandle(userTokenHandle);

_currentAquariusProcessID = (int)procinfo.dwProcessId;
}
catch (Exception exc)
{
Program.Log.Error(exc.Message);
//System.Runtime.InteropServices.Interop.ShowMessageBox(exc.Message, "Comfirm");
}
}

注意:要在windows服務里面去調用啟動外部程序,並且外部程序不能使控制台程序,建議是winform.exe這樣方便測試。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM