Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

DeleteService (advapi32)
 
.
Summary
The DeleteService function marks the specified service for deletion from the service control manager database.

C# Signature:

[DllImport("advapi32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteService( IntPtr hService );

VB.Net Signature:

<DllImport("advapi32.dll", SetLastError:=True)> _
Public Function DeleteService(ByVal hService As IntPtr) As Boolean
End Function

VB Signature:

Declare Function DeleteService Lib "advapi32.dll" (ByVal hService As Integer) As Integer

User-Defined Types:

None.

Notes:

Use the System.ServiceProcess.ServiceInstaller and System.ServiceProcess.ServiceProcessInstaller when ever possible; however, there are a some cases, such as the state file being corrupted or lost, they do not handel and ended up having to use the API to remove the service.

VB Signature:

Declare Function DeleteService Lib "advapi32.dll" (ByVal hService As Integer) As Integer

User-Defined Types:

None.

Notes:

Use the System.ServiceProcess.ServiceInstaller and System.ServiceProcess.ServiceProcessInstaller when ever possible; however, there are a some cases, such as the state file being corrupted or lost, they do not handel and ended up having to use the API to remove the service.

Tips & Tricks:

Please add some!

Tips & Tricks:

Please add some!

Sample Code:

    #region SERVICE_ACCESS
    [Flags]
    public enum SERVICE_ACCESS : uint
    {
        STANDARD_RIGHTS_REQUIRED   = 0xF0000,
        SERVICE_QUERY_CONFIG       = 0x00001,
        SERVICE_CHANGE_CONFIG      = 0x00002,
        SERVICE_QUERY_STATUS       = 0x00004,
        SERVICE_ENUMERATE_DEPENDENTS   = 0x00008,
        SERVICE_START      = 0x00010,
        SERVICE_STOP       = 0x00020,
        SERVICE_PAUSE_CONTINUE     = 0x00040,
        SERVICE_INTERROGATE    = 0x00080,
        SERVICE_USER_DEFINED_CONTROL   = 0x00100,
        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)
    }
    #endregion
    #region SCM_ACCESS
    [Flags]
    public enum SCM_ACCESS : uint
    {
        STANDARD_RIGHTS_REQUIRED       = 0xF0000,
        SC_MANAGER_CONNECT     = 0x00001,
        SC_MANAGER_CREATE_SERVICE      = 0x00002,
        SC_MANAGER_ENUMERATE_SERVICE   = 0x00004,
        SC_MANAGER_LOCK    = 0x00008,
        SC_MANAGER_QUERY_LOCK_STATUS   = 0x00010,
        SC_MANAGER_MODIFY_BOOT_CONFIG  = 0x00020,
        SC_MANAGER_ALL_ACCESS      = STANDARD_RIGHTS_REQUIRED |
            SC_MANAGER_CONNECT |
            SC_MANAGER_CREATE_SERVICE |
            SC_MANAGER_ENUMERATE_SERVICE |
            SC_MANAGER_LOCK |
            SC_MANAGER_QUERY_LOCK_STATUS |
            SC_MANAGER_MODIFY_BOOT_CONFIG
    }
    #endregion

        #region DeleteService
        [DllImport("advapi32.dll", SetLastError=true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeleteService( IntPtr hService );
        #endregion
        #region OpenService
        [DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, SERVICE_ACCESS dwDesiredAccess);
        #endregion
        #region OpenSCManager
        [DllImport("advapi32.dll", EntryPoint="OpenSCManagerW", ExactSpelling=true, CharSet=CharSet.Unicode, SetLastError=true)]
        static extern IntPtr OpenSCManager( string machineName, string databaseName, SCM_ACCESS dwDesiredAccess );
        #endregion
        #region CloseServiceHandle
        [DllImport("advapi32.dll", SetLastError=true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CloseServiceHandle( IntPtr hSCObject );
        #endregion

            try
            {
                IntPtr schSCManager = OpenSCManager( null, null, SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
                if( schSCManager != IntPtr.Zero )
                {
                    IntPtr schService = OpenService( schSCManager, txtServiceName.Text, SERVICE_ACCESS.SERVICE_ALL_ACCESS);
                    if( schService != IntPtr.Zero )
                    {
                        if(DeleteService( schService )== false)
                        {
                            MessageBox.Show(
                                string.Format("DeleteService failed {0}",Marshal.GetLastWin32Error()));
                        }
                    }
                    CloseServiceHandle( schSCManager );
                    // if you don't close this handle, Services control panel
                    // shows the service as "disabled", and you'll get 1072 errors
                    // trying to reuse this service's name
                    CloseServiceHandle( schService );

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }        

Alternative Managed API:

One could use the System.ServiceProcess.ServiceInstaller and System.ServiceProcess.ServiceProcessInstaller, but doing so will create an InstallState file that may be undesirable.

Do you know one? Please contribute it!

One could use the System.ServiceProcess.ServiceInstaller and System.ServiceProcess.ServiceProcessInstaller, but doing so will create an InstallState file that may be undesirable.

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions