Service uses process to start an external program without a form
In WinXP and Win2003 environments, after you install the service, right-click the service Properties-Login tab-Select Local System account and tick allow service to interact with desktop.
In Win7 and later environments, due to Microsoft's enhanced rights Management, disabling this feature requires referencing third-party DLLs (Cjwdev.WindowsApi.dll)
Link: https://pan.baidu.com/s/1P6bBHI_AkOOTxvRTHh06aw Password: PHDJ
Specific reason analysis can refer to this article: 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
With regard to the parameter lpcommandline, here you can put the parameters that you passed to the created process. Note that the C program needs to pass parameters with a space before the parameter, because the C program main function in the default parameter is the module name, starting from the second parameter is the argument you want to pass in, and the parameters are separated by a space so you have to add a space.
About loading and unloading services via BAT file
cd/d%~DP0SC Create Monitorservice binpath= "%cd%\windowsservice1.exe" start= Autosc start Monitorservicepause
sc stop Monitorservice SC delete monitorservice pause
Windows Service launches external program