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.