Windows Service launches external program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.