How to uninstall a service when it is stopped?
-
hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?
-
hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?
nobody knows why?
-
hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?
-
Hi, You should post a bit of code, responsible for the process of service uninstall. Regards
the code is below,all code i found to uninstall service like this: void CService::UnInstallService(TCHAR *tcServiceName) { SC_HANDLE schService; SC_HANDLE schSCManager; if(lstrlen(tcServiceName)<=0) return; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, tcServiceName, SERVICE_ALL_ACCESS); if (schService) { // try to stop the service if ( ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus ) ) { _tprintf(TEXT("Stopping %s."), tcServiceName); Sleep( 1000 ); while( QueryServiceStatus( schService, &serviceStatus ) ) { if ( serviceStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); Sleep( 1000 ); } else break; } if ( serviceStatus.dwCurrentState == SERVICE_STOPPED ) _tprintf(TEXT("\n%s stopped.\n"), tcServiceName ); else _tprintf(TEXT("\n%s failed to stop.\n"), tcServiceName ); } // now remove the service if( DeleteService(schService) ) _tprintf(TEXT("%s removed.\n"), tcServiceName ); else _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schService); } else _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schSCManager); } else _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); } someone can help me or give me the reason?
-
hello, i write a service and it can run well,but i have found a problem,that is if the service is running i can uninstall it successfully(stop it ,delete the record in the service list and registry),but if the service is already stoppped,then i can not delete the service(the record is still in the service list and the registry),and the service labeld disabled and the service can not be installed again after reboot the machine,what is the matter?what can i do to solve this problem?
everybody can download some sample code from here,and tried,almost have the problem.
-
the code is below,all code i found to uninstall service like this: void CService::UnInstallService(TCHAR *tcServiceName) { SC_HANDLE schService; SC_HANDLE schSCManager; if(lstrlen(tcServiceName)<=0) return; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, tcServiceName, SERVICE_ALL_ACCESS); if (schService) { // try to stop the service if ( ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus ) ) { _tprintf(TEXT("Stopping %s."), tcServiceName); Sleep( 1000 ); while( QueryServiceStatus( schService, &serviceStatus ) ) { if ( serviceStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); Sleep( 1000 ); } else break; } if ( serviceStatus.dwCurrentState == SERVICE_STOPPED ) _tprintf(TEXT("\n%s stopped.\n"), tcServiceName ); else _tprintf(TEXT("\n%s failed to stop.\n"), tcServiceName ); } // now remove the service if( DeleteService(schService) ) _tprintf(TEXT("%s removed.\n"), tcServiceName ); else _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schService); } else _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256)); CloseServiceHandle(schSCManager); } else _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256)); } someone can help me or give me the reason?
-
everybody can download some sample code from here,and tried,almost have the problem.
-
you can download a sample service from the link: http://www.codeproject.com/system/cntservice.asp