вообще тут напрашивается класс 
например что-то вроде
| Код | /*-----------------------------------------------------------------------*/ class SCHandle { private : boost::shared_ptr<void> m_handle;
public : SCHandle (SC_HANDLE handle = NULL) : m_handle (handle, CloseServiceHandle) {;} ~SCHandle() {;}
operator SC_HANDLE () const { return (SC_HANDLE)m_handle.get(); } }; /*-----------------------------------------------------------------------*/ class ServiceManager { private : std::string m_name; SCHandle m_hService; SERVICE_STATUS m_status;
unsigned GetStatus (void) const; unsigned GetUserStatus (void) const;
public : explicit ServiceManager (const std::string& serviceName); ~ServiceManager() {;}
bool IsExist (void) const; bool IsRunning (void) const; bool IsStopped (void) const; bool IsPaused (void) const;
bool Start (void) const; bool Stop (void); bool Pause (void); bool Continue (void);
bool Install (const std::string& displayName, unsigned startType, const char* services = NULL, const char* group = NULL); bool Uninstall (void);
};
|
| Код | ServiceManager::ServiceManager (const std::string& serviceName) : m_name (serviceName), m_hService (NULL) { ZeroMemory (&m_status, sizeof (SERVICE_STATUS));
SCHandle hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_CONNECT); if (!hSCManager) { DebugMessage ("ServiceManager::ServiceManager (OpenSCManager)"); throw std::exception("Ошибка открытия SCManager"); }
m_hService = OpenService (hSCManager, m_name.c_str(), SERVICE_ALL_ACCESS); if (!m_hService) { DebugMessage ("ServiceManager::ServiceManager (OpenService)"); }
} /* -------------------------------------------------------------------------- */ bool ServiceManager::IsExist (void) const { return (m_hService != NULL); } /* -------------------------------------------------------------------------- */ bool ServiceManager::IsRunning (void) const { return (GetStatus() == SERVICE_RUNNING); } /* -------------------------------------------------------------------------- */ bool ServiceManager::IsStopped (void) const { return (GetStatus() == SERVICE_STOPPED); } /* -------------------------------------------------------------------------- */ bool ServiceManager::IsPaused (void) const { return (GetStatus() == SERVICE_PAUSED); } /* -------------------------------------------------------------------------- */ unsigned ServiceManager::GetStatus (void) const { SERVICE_STATUS status = {0}; QueryServiceStatus (m_hService, &status); return status.dwCurrentState; } /* -------------------------------------------------------------------------- */ unsigned ServiceManager::GetUserStatus (void) const { SERVICE_STATUS status = {0}; QueryServiceStatus (m_hService, &status); return status.dwServiceSpecificExitCode; } /* -------------------------------------------------------------------------- */ bool ServiceManager::Start (void) const { if (!m_hService) return false; if (!StartService (m_hService, 0, NULL)) { DebugMessage ("ServiceManager::Start (StartService)"); return false; }
return true; } /* -------------------------------------------------------------------------- */ bool ServiceManager::Stop (void) { if (!m_hService) return false; if (!ControlService(m_hService, SERVICE_CONTROL_STOP, &m_status)) { DebugMessage ("ServiceManager::Stop (ControlService)"); return false; } return true; } /* -------------------------------------------------------------------------- */ bool ServiceManager::Pause (void) { if (!m_hService) return false; if (!ControlService (m_hService, SERVICE_CONTROL_PAUSE, &m_status)) { if (GetLastError () != ERROR_SERVICE_NOT_ACTIVE) { DebugMessage ("ServiceManager::Pause (ControlService)"); return false; } }
return true; } /* -------------------------------------------------------------------------- */ bool ServiceManager::Continue (void) { if (!m_hService) return false; if (!ControlService(m_hService, SERVICE_CONTROL_CONTINUE, &m_status)) { DebugMessage ("ServiceManager::Continue (ControlService)"); return false; }
return true; } /* -------------------------------------------------------------------------- */ bool ServiceManager::Install (const std::string& displayName, unsigned startType, const char* services, const char* group) { if (m_hService) return true;
SCHandle hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_CREATE_SERVICE); if (!hSCManager) { DebugMessage ("ServiceManager::Install (OpenSCManager)"); return false; }
m_hService = OpenService (hSCManager, m_name.c_str(), SERVICE_ALL_ACCESS); if (m_hService) return true;
m_hService = CreateService ( hSCManager, m_name.c_str(), displayName.c_str(), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, startType, SERVICE_ERROR_NORMAL, GetProcessPath (), group, NULL, services, NULL, NULL);
if (!m_hService) { DebugMessage ("ServiceManager::Install (CreateService)"); return false; } return true; } /* -------------------------------------------------------------------------- */ bool ServiceManager::Uninstall (void) { if (!m_hService) return true; if (m_status.dwCurrentState != SERVICE_STOPPED) { if (!ControlService (m_hService, SERVICE_CONTROL_STOP, &m_status)) { DebugMessage ("ServiceManager::Uninstall (ControlService)"); return false; } }
if (!DeleteService (m_hService)) { DebugMessage ("ServiceManager::Uninstall (DeleteService)"); return false; } return true; } /* -------------------------------------------------------------------------- */
|
|