C # Install and uninstall services

Source: Internet
Author: User

This is a class for C # to install and uninstall services. If you are interested, please take a look.
[Csharp]
Using System;
Using System. Runtime. InteropServices;
Namespace family. 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
/////
//// C # service application installation portal.
/////
//
// [STAThread]
// Static void Main (string [] args)
//{
//
// String svcPath;
// String svcName;
// String svcDispName;
//// C # path for installing the Service Program
// SvcPath = @ "d: \ service \ EAEWS.exe ";
// SvcDispName = "myEAEWS ";
// SvcName = "myEAEWS ";
// ServiceInstaller c = new ServiceInstaller ();
// C. InstallService (svcPath, svcName, svcDispName );
// Console. Read ();
//
//}

///
/// Install and run
///
/// C # installation path.
/// Service name
/// Service display name.
/// Check whether the service is successfully installed.
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;
}
}
///
/// Anti-installation service.
///
/// Service name.
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;
}
}
}

 

Related Article

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.