标签:
卸载服务的过程是这样的,用OpenSCManager打开SCM,使用OpenService打开准备卸载的服务,通过QueryServiceStatus查询该服务的状态是否停止,如果否,则先停止该服务ControlService,最后用DeleteService删除服务。
1 #include <windows.h> 2 #include <winsvc.h> 3 #include <stdio.h> 4 5 void UninstallServices(); 6 void main() 7 { 8 UninstallServices(); 9 } 10 11 12 void UninstallServices() 13 { 14 char name[100]; 15 SC_HANDLE scm; 16 SC_HANDLE service; 17 SERVICE_STATUS status; 18 19 printf("服务名称:"); 20 scanf("%s",&name); 21 22 if((scm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE)) == NULL) 23 printf("error!\n"); 24 25 service = OpenService(scm,name,SERVICE_ALL_ACCESS | DELETE); 26 BOOL isSuccess = QueryServiceStatus(service,&status); 27 if(status.dwCurrentState != SERVICE_STOPPED) 28 { 29 isSuccess = ControlService(service,SERVICE_CONTROL_STOP,&status); 30 Sleep(500); 31 } 32 isSuccess = DeleteService(service); 33 if(!isSuccess) 34 printf("Uninstall Falid!\n"); 35 else 36 printf("Uninstall Success!\n"); 37 38 CloseServiceHandle(service); 39 CloseServiceHandle(scm); 40 }
通过运行这个程序,输入服务名称,我们可以将上一篇建立的服务程序删掉了
标签:
原文地址:http://www.cnblogs.com/lanf/p/5093372.html