Deleting the Windows Service


We can delete the windows service using the .net. We need low level delete for this case. If service is running, it should be stopped first. DeleteService method deletes the closed service. Following full code implements the service deletion strategy.

class Program
{

static void Main(string[] args)

{

try

{

ServiceController sc = new ServiceController("Full Service Name");

// If service is not found in the machine, we should get exception from sc.Status

if (sc.Status == ServiceControllerStatus.Running)

{

TimeSpan waitTo = new TimeSpan(0, 0, 30);

sc.Stop();

sc.WaitForStatus(ServiceControllerStatus.Stopped, waitTo);

}

IntPtr schSCManager = OpenSCManager(null, null, SCM_ACCESS.SC_MANAGER_ALL_ACCESS);

if (schSCManager != IntPtr.Zero)

{

IntPtr schService = OpenService(schSCManager, "Full Service Name",

SERVICE_ACCESS.SERVICE_ALL_ACCESS);

if (schService != IntPtr.Zero)

DeleteService(schService);

CloseServiceHandle(schSCManager);

// if we don't close this handle, Services control panel

// shows the service as "disabled", and we'll get 1072 errors

// trying to reuse this service's name

if (schService != IntPtr.Zero)

CloseServiceHandle(schService);

}

}

catch (Exception)

{

Console.WriteLine("Failed");

}

Console.ReadKey();

}

[DllImport("advapi32.dll", SetLastError = true)]

[return: MarshalAs(UnmanagedType.Bool)]

public static extern bool DeleteService(IntPtr hService);

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]

static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, SERVICE_ACCESS dwDesiredAccess);

[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]

static extern IntPtr OpenSCManager(string machineName, string databaseName, SCM_ACCESS dwDesiredAccess);

[DllImport("advapi32.dll", SetLastError = true)]

[return: MarshalAs(UnmanagedType.Bool)]

static extern bool CloseServiceHandle(IntPtr hSCObject);

}

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

}

[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

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s