__int64 WinPmem::uninstall_driver() { SC_HANDLE scm, service; SERVICE_STATUS ServiceStatus; scm = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); if (!scm) return 0; service = OpenService(scm, service_name, SERVICE_ALL_ACCESS); if (service) { ControlService(service, SERVICE_CONTROL_STOP, &ServiceStatus); }; DeleteService(service); CloseServiceHandle(service); Log(TEXT("Driver Unloaded.\n")); return 1; CloseServiceHandle(scm); return 0; }
bool WinServiceUninstall() { SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT); if (serviceControlManager) { SC_HANDLE service = OpenService(serviceControlManager, serviceName, SERVICE_QUERY_STATUS | DELETE); if (service) { SERVICE_STATUS serviceStatus2; if (QueryServiceStatus(service, &serviceStatus2)) { if (serviceStatus2.dwCurrentState == SERVICE_STOPPED) DeleteService(service); } CloseServiceHandle(service); } CloseServiceHandle(serviceControlManager); } return true; }
/* Remove installed service from servicecontrolmanager */ void wsvc_remove(FILE* out) { SC_HANDLE scm; SC_HANDLE sv; if(out) fprintf(out, "removing unbound service\n"); scm = OpenSCManager(NULL, NULL, (int)SC_MANAGER_ALL_ACCESS); if(!scm) fatal_win(out, "could not OpenSCManager"); sv = OpenService(scm, SERVICE_NAME, DELETE); if(!sv) { CloseServiceHandle(scm); fatal_win(out, "could not OpenService"); } if(!DeleteService(sv)) { CloseServiceHandle(sv); CloseServiceHandle(scm); fatal_win(out, "could not DeleteService"); } CloseServiceHandle(sv); CloseServiceHandle(scm); event_reg_remove(out); if(out) fprintf(out, "unbound service removed\n"); }
static BOOL remove_service(void){ SC_HANDLE scm; SC_HANDLE service; if((scm = OpenSCManager(NULL, NULL, GENERIC_WRITE)) == NULL) return FALSE; service = OpenServiceW(scm, real_service_name, SERVICE_ALL_ACCESS); if(service == NULL){ CloseServiceHandle(scm); return FALSE; } if(!DeleteService(service)){ last_error = GetLastError(); return FALSE; } CloseServiceHandle(service); CloseServiceHandle(scm); return TRUE; }
static void remove_service(SC_HANDLE hSCManager, SC_HANDLE hService, char* name, char* disp_name) { SERVICE_STATUS status; printf("Removing service: %-40s ... ", disp_name); if(hService==NULL) { hService = OpenService(hSCManager, name, SERVICE_ALL_ACCESS); if(hService==NULL) { printf("\n!ERROR %d opening service: %s\n",GetLastError(),name); return; } } // try to stop the service if(ControlService( hService, SERVICE_CONTROL_STOP, &status)) { printf("\nStopping: %s ... ",name); while(QueryServiceStatus(hService, &status) && status.dwCurrentState == SERVICE_STOP_PENDING) Sleep(1000); if(status.dwCurrentState == SERVICE_STOPPED) printf("Stopped.\n"); else printf("FAILED!\n"); } // now remove the service if(DeleteService(hService)) printf("Successful\n"); else printf("!ERROR %d\n",GetLastError()); CloseServiceHandle(hService); }
void SvcDelete() { SC_HANDLE schSCManager; SC_HANDLE schService; schSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT); if (!schSCManager) { printf("OpendSCManager failed (%d)\n", GetLastError()); return; } schService = OpenService( schSCManager, SVCNAME, DELETE); if (!schService) { printf("OpenService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); return; } if (!DeleteService(schService)) { printf("DeleteService failed (%d)\n", GetLastError()); } else { printf("Servcie deleted successfully\n"); } CloseServiceHandle(schService); CloseServiceHandle(schSCManager); }
static int UninstallService(const char * sServiceName) { SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if(schSCManager == NULL) { printf("OpenSCManager failed (%d)!", GetLastError()); return EXIT_FAILURE; } SC_HANDLE schService = OpenService(schSCManager, sServiceName, SERVICE_QUERY_STATUS | SERVICE_STOP | DELETE); if(schService == NULL) { printf("OpenService failed (%d)!", GetLastError()); CloseServiceHandle(schSCManager); return EXIT_FAILURE; } SERVICE_STATUS_PROCESS ssp; DWORD dwBytesNeeded; if(QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded) != 0) { if(ssp.dwCurrentState != SERVICE_STOPPED && ssp.dwCurrentState != SERVICE_STOP_PENDING) { ControlService(schService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp); } } if(DeleteService(schService) == false) { printf("DeleteService failed (%d)!", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return EXIT_FAILURE; } else { printf("PtokaX service '%s' deleted successfully.", sServiceName); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return EXIT_SUCCESS; } }
void wisvc_UninstallKublService (char **argv, char *service_name) { int called_as_service = 0; /* Needed by macro err_printf */ SC_HANDLE schService = wisvc_OpenKublService (argv, service_name, "uninstall", DELETE); if (!DeleteService (schService)) { DWORD erhe = GetLastError (); if (ERROR_SERVICE_MARKED_FOR_DELETE == erhe) { err_printf (( "%s: Cannot %s service \"%s\" because a service with that " "name still exists, although it has been marked for delete. Use ISQL to " "stop the old service with shutdown or raw_exit() before continuing " " (errno=%ld).\n", argv[0], "uninstall", service_name, erhe)); } else { err_printf (( "%s: Uninstalling service \"%s\" failed. " "DeleteService returned zero, errno=%ld.\n", argv[0], service_name, erhe)); } exit (1); } else { err_printf (("%s: Service \"%s\" uninstalled successfully.\n", argv[0], service_name)); } CloseServiceHandle (schService); }
NDASDI_API BOOL WINAPI NdasDiDeleteServiceSCH( IN SC_HANDLE schSCManager, IN LPCTSTR ServiceName) { BOOL fSuccess = FALSE; DPInfo(_FT("Deleting Service %s.\n"), ServiceName); AutoSCLock scLock = LockServiceDatabase(schSCManager); if (NULL == (SC_LOCK) scLock) { DPErrorEx(_FT("Locking service database failed: ")); return FALSE; } AutoSCHandle hService = OpenService( schSCManager, ServiceName, DELETE); if (NULL == (SC_HANDLE) hService) { DPErrorEx(_FT("Opening a service %s failed: "), ServiceName); return FALSE; } fSuccess = DeleteService(hService); if (!fSuccess) { DPErrorEx(_FT("Deleting a service %s failed: "), ServiceName); return FALSE; } DPInfo(_FT("Service %s deleted successfully.\n"), ServiceName); return TRUE; }
/** * Deletes the service. * * @returns 0 on success. * @returns -1 on failure. */ int usblibOsDeleteService(void) { /* * Assume it didn't exist, so we'll create the service. */ int rc = -1; SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG); DWORD LastError = GetLastError(); NOREF(LastError); AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError)); if (hSMgr) { SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE); if (hService) { /* * Delete the service. */ if (DeleteService(hService)) rc = 0; else { DWORD LastError = GetLastError(); NOREF(LastError); AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError)); } CloseServiceHandle(hService); } else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) rc = 0; else { DWORD LastError = GetLastError(); NOREF(LastError); AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError)); } CloseServiceHandle(hSMgr); } return rc; }
VOID UninstallService() { LOG_TRACE(); SC_HANDLE hSCMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (NULL == hSCMgr) { std::cout << "Uninstall service failed. (OpenSCManager return null. GetLastError == " << GetLastError() << ")" << std::endl; LOG_RUN_ERROR("OpenSCManager failed. (%d)", GetLastError()); return; } SC_HANDLE hSvc = OpenService(hSCMgr,MY_SERVICE_NAME, SERVICE_STOP | DELETE); if (NULL == hSvc) { std::cout << "Uninstall service failed. (OpenService return null. GetLastError == " << GetLastError() << ")" << std::endl; LOG_RUN_ERROR("CreateService failed. (%d)", GetLastError()); CloseServiceHandle(hSCMgr); return; } //停止服务 ControlService(hSvc, SERVICE_CONTROL_STOP, &g_svc_status); //删除服务 if (DeleteService(hSvc)) { LOG_RUN_DEBUG("Uninstall service success."); std::cout << "Uninstall service success." << std::endl; } else { LOG_RUN_DEBUG("DeleteService failed. (%d)", GetLastError()); std::cout << "Uninstall service failed. (DeleteService return false. GetLastError == " << GetLastError() << ")" << std::endl; } CloseServiceHandle(hSvc); CloseServiceHandle(hSCMgr); }
void ServiceManager::UnInstall() { SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS); if (schSCManager==0) { long nError = GetLastError(); TCHAR pTemp[121]; _stprintf(pTemp, _T("OpenSCManager failed, error code = %d"), nError); LOG_WRITER_INSTANCE.WriteLog( pTemp); } else { SC_HANDLE schService = OpenService( schSCManager, SERVICE_PROPERTIES_INSTANCE.GetServiceName(), SERVICE_ALL_ACCESS); if (schService==0) { long nError = GetLastError(); TCHAR pTemp[121]; _stprintf(pTemp, _T("OpenService failed, error code = %d"), nError); LOG_WRITER_INSTANCE.WriteLog( pTemp); } else { if(!DeleteService(schService)) { TCHAR pTemp[121]; _stprintf(pTemp, _T("Failed to delete service %s"), SERVICE_PROPERTIES_INSTANCE.GetServiceName()); LOG_WRITER_INSTANCE.WriteLog( pTemp); } CloseServiceHandle(schService); } CloseServiceHandle(schSCManager); } CString logFileName=FolderHelper::GetModuleFileName().c_str(); logFileName.Replace(_T(".exe"),_T(".log")); DeleteFile(logFileName); }
// ------------------------------------------------------------------------ void remove_symon(void){ // Удаляем сервис из системы SC_HANDLE hServiceControlManager, hService; if( NULL == ( hServiceControlManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT ))){ fprintf( stderr, "ERROR-%u. Open SCM failed. Admin\n", GetLastError() ); return; } if( NULL == ( hService = OpenService( hServiceControlManager, service_name, SERVICE_ALL_ACCESS | DELETE ))){ fprintf( stderr, "ERROR-%u. Open service failed.\n", GetLastError() ); CloseServiceHandle(hServiceControlManager); return; } if( !QueryServiceStatus( hService, &service_status )){ fprintf( stderr, "ERROR-%u. Query service status failed.\n", GetLastError() ); CloseServiceHandle(hServiceControlManager); CloseServiceHandle(hService); return; } if( service_status.dwCurrentState != SERVICE_STOPPED ){ // Если запущен - останавливаем fprintf( stderr, "WARNING! %u. Service is working. It will be stoped.\n", GetLastError() ); if( !ControlService(hService, SERVICE_CONTROL_STOP, &service_status )){ fprintf( stderr, "ERROR-%u. Control service failed.\n", GetLastError() ); CloseServiceHandle(hServiceControlManager); CloseServiceHandle(hService); return; } Sleep(500); } if( !DeleteService( hService )){ fprintf( stderr, "ERROR-%u. Delete service failed.\n", GetLastError() ); CloseServiceHandle(hServiceControlManager); CloseServiceHandle(hService); return; } CloseServiceHandle(hServiceControlManager); CloseServiceHandle(hService); fprintf( stderr, "The service is deleted.\n" ); // Готово }
static int NTServiceUninstall( intf_thread_t *p_intf ) { intf_sys_t *p_sys = p_intf->p_sys; SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if( handle == NULL ) { msg_Err( p_intf, "could not connect to Services Control Manager database" ); return VLC_EGENERIC; } /* First, open a handle to the service */ SC_HANDLE service = OpenService( handle, p_sys->psz_service, DELETE ); if( service == NULL ) { msg_Err( p_intf, "could not open service" ); CloseServiceHandle( handle ); return VLC_EGENERIC; } /* Remove the service */ if( !DeleteService( service ) ) { msg_Err( p_intf, "could not delete service \"%s\"", p_sys->psz_service ); } else { msg_Dbg( p_intf, "service deleted successfuly" ); } CloseServiceHandle( service ); CloseServiceHandle( handle ); return VLC_SUCCESS; }
BOOL WindowsService::Remove() { bool ret_val= false; if (! IsInstalled()) return true; // open a connection to the SCM SC_HANDLE scm= OpenSCManager(0, 0,SC_MANAGER_CREATE_SERVICE); if (! scm) return false; SC_HANDLE service= OpenService(scm, serviceName, DELETE); if (service) { if (DeleteService(service)) ret_val= true; DWORD dw= ::GetLastError(); CloseServiceHandle(service); } CloseServiceHandle(scm); return ret_val; }
int _cdecl uninstall_minifilter (void) { SC_HANDLE hService = NULL; SC_HANDLE hSCManager = NULL; int ret = 0; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); PrintResult(L"OpenSCManager", L"Opening Service Control Manager Succeded"); if(hSCManager) { if(!hService) { SERVICE_STATUS ss; hService = OpenServiceW(hSCManager, L"protegeDados", SERVICE_START | DELETE | SERVICE_STOP); ControlService(hService, SERVICE_CONTROL_STOP, &ss); DeleteService(hService); ret = 1; } CloseServiceHandle(hSCManager); } return ret; }
bool InstallService(String name, String display_name, String& cmdline, String& status) { ServiceHandle scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if(!scm) { status = "Cannot open service manager"; return false; } ServiceHandle me = OpenService(scm, name, SC_MANAGER_ALL_ACCESS); if(me) { SERVICE_STATUS srvstat; if(ControlService(me, SERVICE_CONTROL_STOP, &srvstat)) { int start = msecs(); while(QueryServiceStatus(me, &srvstat) && srvstat.dwCurrentState != SERVICE_STOPPED && msecs(start) < 10000) Sleep(100); } if(!DeleteService(me)) { status = NFormat("Error deleting existing service: %s", GetLastErrorMessage()); return false; } me.Close(); } me = CreateService(scm, name, display_name, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, cmdline, 0, 0, 0, 0, 0); if(!me) { status = NFormat("Error creating service: %s", GetLastErrorMessage()); return false; } if(!StartService(me, 0, NULL)) { status = NFormat("Error starting service: %s", GetLastErrorMessage()); return false; } return true; }
BOOL Delete(LPCTSTR ServiceName) { SC_HANDLE hSCManager = NULL; SC_HANDLE hSc = NULL; #ifdef SCDBG _tprintf(_T("service to delete - %s\n\n"), ServiceName); #endif hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); if (hSCManager != NULL) { hSc = OpenService(hSCManager, ServiceName, DELETE); if (hSc != NULL) { if (DeleteService(hSc)) { _tprintf(_T("[SC] DeleteService SUCCESS\n")); CloseServiceHandle(hSc); CloseServiceHandle(hSCManager); return TRUE; } } } ReportLastError(); if (hSc) CloseServiceHandle(hSc); if (hSCManager) CloseServiceHandle(hSCManager); return FALSE; }
/************************************* * BOOL DeleteSampleService(LPTSTR szNameOfService) * 功能 删除服务 * * 参数 LPTSTR szNameOfService 服务的名字 **************************************/ BOOL DeleteSampleService(LPTSTR szNameOfService) { schService = OpenService( schSCManager, // SCM 句柄 szNameOfService, // 服务名 DELETE); // 可删除 if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); return FALSE; } // 删除服务 if (! DeleteService(schService) ) { printf("DeleteService failed (%d)\n", GetLastError()); return FALSE; } else printf("DeleteService succeeded\n"); // 关闭句柄 CloseServiceHandle(schService); return TRUE; }
bool CService::UnInstallService( std::wstring wstrService ) { SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCManager == NULL) { return false; } SC_HANDLE hService = OpenService(hSCManager, wstrService.c_str(), SERVICE_STOP|DELETE); if (hService == NULL) { CloseServiceHandle(hSCManager); return false; } SERVICE_STATUS status; ControlService(hService, SERVICE_CONTROL_STOP, &status); // 删除服务 bool bDel = DeleteService(hService); CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return bDel; }
bool ServiceUtil::remove(const tstring& name) { bool bReturn = false; do { scoped_svc_handle hService(name, GENERIC_ALL, SERVICE_ALL_ACCESS); if (!hService.valid()) { break; } if (!DeleteService(hService.get())) { ErrorLogLastErr(TSTR("DeleteService[%s] fail"), name.c_str()); break; } bReturn = true; } while (false); return bReturn; }
BOOL Uninstall() { EventLog log; // open SCManager SC_HANDLE scMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (scMgr == NULL) { log.LogLastError("Unable to open service control manager"); _tprintf(_T("Unable to open service manager!\n")); return FALSE; } // open service and delete SC_HANDLE svc = OpenService(scMgr, _T("EasyDynamicDNS"), DELETE); if (svc == NULL) { log.LogLastError("Unable to open service"); _tprintf(_T("Unable to open service!\n")); CloseServiceHandle(scMgr); return FALSE; } if (DeleteService(svc) != TRUE) { log.LogLastError("Unable to delete service"); _tprintf(_T("Unable to delete service!\n")); CloseServiceHandle(svc); CloseServiceHandle(scMgr); return FALSE; } CloseServiceHandle(svc); // close SCManager CloseServiceHandle(scMgr); log.LogEvent(EVENTLOG_INFORMATION_TYPE, "EasyDynamicDNS uninstalled."); EventLog::UnregisterEventSource(); return TRUE; }
int dha_uninstall(void) { SC_HANDLE hSCManager = NULL; SC_HANDLE hService = NULL; char szPath[MAX_PATH]; int result = 0; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); hService = OpenService(hSCManager, DRV_NAME, SERVICE_ALL_ACCESS); dha_stop(); result = DeleteService(hService); if(!result) print_last_error("Error while deleting service"); CloseServiceHandle(hService); CloseServiceHandle(hSCManager); GetWindowsDirectory(szPath, MAX_PATH); strcpy(szPath + strlen(szPath), "\\system32\\drivers\\" DRV_FILENAME); DeleteFile(szPath); return 0; }
// uninstall self int Uninstall(void) { HKEY key; if(!OsIsNt) { if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&key)==ERROR_SUCCESS) { RegDeleteValue(key,wscfg.ws_regname); RegCloseKey(key); if(RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",&key)==ERROR_SUCCESS) { RegDeleteValue(key,wscfg.ws_regname); RegCloseKey(key); return 0; } } } else { SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS); if (schSCManager!=0) { SC_HANDLE schService = OpenService( schSCManager, wscfg.ws_svcname, SERVICE_ALL_ACCESS); if (schService!=0) { if(DeleteService(schService)!=0) { CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return 0; } CloseServiceHandle(schService); } CloseServiceHandle(schSCManager); } } return 1; }
/** * Stops and removes the service * * @param name The name of the service to uninstall * * @author mickem * * @date 03-13-2004 * */ void Uninstall(std::wstring name) { SC_HANDLE schService; SC_HANDLE schSCManager; Stop(name); schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!schSCManager) throw SCException("OpenSCManager failed: " + error::lookup::last_error()); schService = OpenService(schSCManager, name.c_str(), SERVICE_ALL_ACCESS); if (schService) { if(!DeleteService(schService)) { std::string err = "DeleteService failed: " + error::lookup::last_error(); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); throw SCException(err); } CloseServiceHandle(schService); } else { std::string err = "OpenService failed: " + error::lookup::last_error(); CloseServiceHandle(schSCManager); throw SCException(err); } CloseServiceHandle(schSCManager); }
BOOL SvcUninstall(void){ SC_HANDLE schSCManager; schSCManager = OpenSCManager( NULL, // local computer NULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) return FALSE; SC_HANDLE hService = OpenServiceW(schSCManager,SVCNAME,SERVICE_STOP|DELETE); if(hService == NULL){ CloseServiceHandle(schSCManager); return FALSE; } SERVICE_STATUS status; BOOL ret=FALSE; ControlService(hService,SERVICE_CONTROL_STOP,&status); if(DeleteService(hService)) ret=TRUE; CloseServiceHandle(hService); CloseServiceHandle(schSCManager); return ret; }
BOOL DeleteServiceEx(HWND hWnd, SC_HANDLE schSCManager, char *szSvcName) { SC_HANDLE schService; schService = OpenService( schSCManager, // SCM база с данни szSvcName, // име на сервиса DELETE); // достъп за изтриване if (schService == NULL) { MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR); return FALSE; } // Изтрива го сервиса if (!DeleteService(schService)) { MessageBox(hWnd, GetErrorText(GetLastError()), "FileHide", MB_ICONERROR); CloseServiceHandle(schService); return FALSE; } MessageBox(hWnd, "Сервисът е успешно изтриен", "FileHide", MB_ICONEXCLAMATION); CloseServiceHandle(schService); return TRUE; }
static int dc_remove_service(wchar_t *name) { SC_HANDLE h_scm = NULL; SC_HANDLE h_svc = NULL; int resl; do { if ( (h_scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL ) { resl = ST_SCM_ERROR; break; } if (h_svc = OpenService(h_scm, name, SERVICE_ALL_ACCESS)) { DeleteService(h_svc); CloseServiceHandle(h_svc); } resl = ST_OK; } while (0); if (h_scm != NULL) { CloseServiceHandle(h_scm); } return resl; }
void ArchDaemonWindows::uninstallDaemon(const char* name) { // remove parameters for this service. ignore failures. HKEY key = openNTServicesKey(); key = ArchMiscWindows::openKey(key, name); if (key != NULL) { ArchMiscWindows::deleteKey(key, _T("Parameters")); ArchMiscWindows::closeKey(key); } // open service manager SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE); if (mgr == NULL) { // can't open service manager throw XArchDaemonUninstallFailed(new XArchEvalWindows); } // open the service. oddly, you must open a service to delete it. SC_HANDLE service = OpenService(mgr, name, DELETE | SERVICE_STOP); if (service == NULL) { DWORD err = GetLastError(); CloseServiceHandle(mgr); if (err != ERROR_SERVICE_DOES_NOT_EXIST) { throw XArchDaemonUninstallFailed(new XArchEvalWindows(err)); } throw XArchDaemonUninstallNotInstalled(new XArchEvalWindows(err)); } // stop the service. we don't care if we fail. SERVICE_STATUS status; ControlService(service, SERVICE_CONTROL_STOP, &status); // delete the service const bool okay = (DeleteService(service) == 0); const DWORD err = GetLastError(); // clean up CloseServiceHandle(service); CloseServiceHandle(mgr); // give windows a chance to remove the service before // we check if it still exists. ARCH->sleep(1); // handle failure. ignore error if service isn't installed anymore. if (!okay && isDaemonInstalled(name)) { if (err == ERROR_SUCCESS) { // this seems to occur even though the uninstall was successful. // it could be a timing issue, i.e., isDaemonInstalled is // called too soon. i've added a sleep to try and stop this. return; } if (err == ERROR_IO_PENDING) { // this seems to be a spurious error return; } if (err != ERROR_SERVICE_MARKED_FOR_DELETE) { throw XArchDaemonUninstallFailed(new XArchEvalWindows(err)); } throw XArchDaemonUninstallNotInstalled(new XArchEvalWindows(err)); } }
int unregisterService() { #ifdef WIN32 Logging::LoggerRef logRef(ppg->getLogger(), "ServiceDeregistrator"); SC_HANDLE scManager = NULL; SC_HANDLE scService = NULL; SERVICE_STATUS ssSvcStatus = { }; int ret = 0; for (;;) { scManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); if (scManager == NULL) { logRef(Logging::FATAL, Logging::BRIGHT_RED) << "OpenSCManager failed with error: " << GetLastErrorMessage(GetLastError()); ret = 1; break; } scService = OpenService(scManager, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE); if (scService == NULL) { logRef(Logging::FATAL, Logging::BRIGHT_RED) << "OpenService failed with error: " << GetLastErrorMessage(GetLastError()); ret = 1; break; } if (ControlService(scService, SERVICE_CONTROL_STOP, &ssSvcStatus)) { logRef(Logging::INFO) << "Stopping " << SERVICE_NAME; Sleep(1000); while (QueryServiceStatus(scService, &ssSvcStatus)) { if (ssSvcStatus.dwCurrentState == SERVICE_STOP_PENDING) { logRef(Logging::INFO) << "Waiting..."; Sleep(1000); } else { break; } } std::cout << std::endl; if (ssSvcStatus.dwCurrentState == SERVICE_STOPPED) { logRef(Logging::INFO) << SERVICE_NAME << " is stopped"; } else { logRef(Logging::FATAL, Logging::BRIGHT_RED) << SERVICE_NAME << " failed to stop" << std::endl; } } if (!DeleteService(scService)) { logRef(Logging::FATAL, Logging::BRIGHT_RED) << "DeleteService failed with error: " << GetLastErrorMessage(GetLastError()); ret = 1; break; } logRef(Logging::INFO) << SERVICE_NAME << " is removed"; break; } if (scManager) { CloseServiceHandle(scManager); } if (scService) { CloseServiceHandle(scService); } return ret; #else return 0; #endif }