Using system;
Using system. runtime. interopservices;
Namespace myserviceinstaller
{
Class serviceinstaller
{
# Region private variables
Private string _ servicepath;
Private string _ servicename;
Private string _ servicedisplayname;
# Endregion private variables
# Region dllimport
[Dllimport ("advapi32.dll")]
Public static extern intptr openscmanager (string lpmachinename, string lpscdb, int scparameter );
[Dllimport ("advapi32.dll")]
Public static extern intptr createservice (intptr SC _handle, string lpsvcname, string lpdisplayname,
Int dwdesiredaccess, int dwservicetype, int dwstarttype, int dwerrorcontrol, string lppathname,
String lploadordergroup, int lpdwtagid, string lpdependencies, string lpservicestartname, string lppassword );
[Dllimport ("advapi32.dll")]
Public static extern void closeservicehandle (intptr schandle );
[Dllimport ("advapi32.dll")]
Public static extern int startservice (intptr svhandle, int dwnumserviceargs, string lpserviceargvectors );
[Dllimport ("advapi32.dll", setlasterror = true)]
Public static extern intptr openservice (intptr schandle, string lpsvcname, int dwnumserviceargs );
[Dllimport ("advapi32.dll")]
Public static extern int deleteservice (intptr svhandle );
[Dllimport ("kernel32.dll")]
Public static extern int getlasterror ();
# Endregion dllimport
/// <Summary>
/// Application entry.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
String svcpath;
String svcname;
String svcdispname;
// Service program path
Svcpath = @ "C:/myservice.exe ";
Svcdispname = "myservice ";
Svcname = "myservice ";
Serviceinstaller c = new serviceinstaller ();
C. installservice (svcpath, svcname, svcdispname );
Console. Read ();
}
/// <Summary>
/// Install and run
/// </Summary>
/// <Param name = "svcpath"> program path. </param>
/// <Param name = "svcname"> service name </param>
/// <Param name = "svcdispname"> service display name. </param>
/// <Returns> whether the service is successfully installed </returns>
Public bool installservice (string svcpath, string svcname, string svcdispname)
{
# Region constants declaration.
Int SC _manager_create_service = 0x0002;
Int service_win32_own_process = 0x00000010;
// Int service_demand_start = 0x00000003;
Int service_error_normal = 0x00000001;
Int standard_rights_required = 0xf0000;
Int service_query_config = 0x0001;
Int service_change_config = 0x0002;
Int service_query_status = 0x0004;
Int service_enumerate_dependents = 0x0008;
Int service_start = 0x0010;
Int service_stop = 0x0020;
Int service_pause_continue = 0x0040;
Int service_interrogate = 0x0080;
Int service_user_defined_control = 0x0100;
Int service_all_access = (standard_rights_required |
Service_query_config |
Service_change_config |
Service_query_status |
Service_enumerate_dependents |
Service_start |
Service_stop |
Service_pause_continue |
Service_interrogate |
Service_user_defined_control );
Int service_auto_start = 0x00000002;
# Endregion constants declaration.
Try
{
Intptr SC _handle = openscmanager (null, null, SC _manager_create_service );
If (SC _handle.toint32 ()! = 0)
{
Intptr sv_handle = createservice (SC _handle, svcname, svcdispname, service_all_access, region, service_auto_start, service_error_normal, svcpath, null, 0, null, null );
If (sv_handle.toint32 () = 0)
{
Closeservicehandle (SC _handle );
Return false;
}
Else
{
// Try to start the service
Int I = startservice (sv_handle, 0, null );
If (I = 0)
{
Return false;
}
Closeservicehandle (SC _handle );
Return true;
}
}
Else
Return false;
}
Catch (exception E)
{
Throw E;
}
}
/// <Summary>
/// Anti-installation service.
/// </Summary>
/// <Param name = "svcname"> service name. </param>
Public bool uninstallservice (string svcname)
{
Int generic_write = 0x40000000;
Intptr SC _hndl = openscmanager (null, null, generic_write );
If (SC _hndl.toint32 ()! = 0)
{
Int Delete = 0x10000;
Intptr svc_hndl = openservice (SC _hndl, svcname, delete );
If (svc_hndl.toint32 ()! = 0)
{
Int I = deleteservice (svc_hndl );
If (I! = 0)
{
Closeservicehandle (SC _hndl );
Return true;
}
Else
{
Closeservicehandle (SC _hndl );
Return false;
}
}
Else
Return false;
}
Else
Return false;
}
}
}