Desktop Functions:

   Smart Device Functions:

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

DeleteService (advapi32)
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:



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!

Sample Code:

    #region SERVICE_ACCESS
    public enum SERVICE_ACCESS : uint
        SERVICE_QUERY_CONFIG       = 0x00001,
        SERVICE_CHANGE_CONFIG      = 0x00002,
        SERVICE_QUERY_STATUS       = 0x00004,
        SERVICE_START      = 0x00010,
        SERVICE_STOP       = 0x00020,
        SERVICE_PAUSE_CONTINUE     = 0x00040,
        SERVICE_INTERROGATE    = 0x00080,
            SERVICE_QUERY_CONFIG     |
            SERVICE_CHANGE_CONFIG    |
            SERVICE_QUERY_STATUS     |
            SERVICE_START    |
            SERVICE_STOP     |
            SERVICE_PAUSE_CONTINUE       |
            SERVICE_INTERROGATE      |
    #region SCM_ACCESS
    public enum SCM_ACCESS : uint
        STANDARD_RIGHTS_REQUIRED       = 0xF0000,
        SC_MANAGER_CONNECT     = 0x00001,
        SC_MANAGER_CREATE_SERVICE      = 0x00002,
        SC_MANAGER_LOCK    = 0x00008,
        SC_MANAGER_QUERY_LOCK_STATUS   = 0x00010,
            SC_MANAGER_CONNECT |
            SC_MANAGER_LOCK |

        #region DeleteService
        [DllImport("advapi32.dll", SetLastError=true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeleteService( IntPtr hService );
        #region OpenService
        [DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
        static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, SERVICE_ACCESS dwDesiredAccess);
        #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 );
        #region CloseServiceHandle
        [DllImport("advapi32.dll", SetLastError=true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CloseServiceHandle( IntPtr hSCObject );

                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)
                                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)

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.


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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version