Transferred from: I _like_cpp's blog
Delete Windows Services
This question is boring for programmers, but it is quite useful in some cases. I deleted and uninstalled a software, but all the results were deleted. The result service was still there. It was very unsightly. I couldn't find the Registry again, so I had to write a small one myself.ProgramTo delete a 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 to the oninitdialog member function: Code 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.