Delete Windows Service [reprint]

Source: Internet
Author: User

This question is boring for programmers, but it is quite useful in some cases. Like deleting and uninstalling a software, the results are all deleted, and the result service is still there. It is very unsightly and the registry cannot be found, I had to write a small program to delete the service.

First, complete the interface as follows:

To use Windows service management function groups, you must first open the Service Manager, which includes winsvc. h In the header file.

Add the SC _HANDLE sh; definition to the dialog box class.

Add the following code to the OnInitDialog member function to initialize service management.

Sh = OpenSCManager (0, SERVICES_ACTIVE_DATABASE, SC _MANAGER_ALL_ACCESS); if (! Sh) {MessageBox ("faile to Open SCManager"); return FALSE ;}

Add to OnClose member function

CloseServiceHandle (sh );

 

Then process two button events

Void CdelsDlg: ListServices () {// TODO: Add your control notification handler code here DWORD eh = 0, d1, nret, I; BOOL ret; SCV. resetContent (); ret = EnumServicesStatus (sh, SERVICE_WIN32 | SERVICE_DRIVER, SERVICE_INACTIVE, ess, sizeof (ess), & d1, & nret, & eh); if (! Ret) {char temp [32]; sprintf (temp, "% d", GetLastError (); MessageBox (temp); MessageBox ("failed to enum services "); return;} for (I = 0; I <nret; I ++) {SCV. addString (ess [I]. lpDisplayName );}}

 

Void CdelsDlg: DelService () {// TODO: Add your control notification handler code here int I, ret; char temp [256]; I = SCV. getCurSel (); sprintf (temp, "Do you realy want to remove the service \ r \ n" "% d: \ t % s \ r \ n % s", I, ess [I]. lpServiceName, ess [I]. lpDisplayName); ret = MessageBox (temp, "Alert! ", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2); if (ret = IDYES) {SC _HANDLE ds; ds = OpenService (sh, ess [I]. lpServiceName, SC _MANAGER_ALL_ACCESS); if (! Ds) {MessageBox ("Failed to Open Service"); return;} if (DeleteService (ds) {MessageBox ("Success to remove the service ");} else {sprintf (temp, "Failed to remove the service \ r \ nERROR CODE: \ t % d", GetLastError (); MessageBox (temp );} closeServiceHandle (ds );}}

The former enumerates non-active services and adds them to the list box. The latter directly deletes the corresponding services.

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.