#ifndef SAFEFREE # define SAFEFREE(x) if(x){ ::free((void *)x); x = NULL; } #endif ... TCHAR szServiceName[] = _T("myservice"); SC_HANDLE hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); ENUM_SERVICE_STATUS *lpService = NULL; DWORD pcbBytesNeeded = 0; DWORD lpServicesReturned = 0; DWORD lpResumeHandle = 0; DWORD dwError = 0; if(!EnumServicesStatus(hScm, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0, &pcbBytesNeeded, &lpServicesReturned, &lpResumeHandle) || (dwError = GetLastError() ) == ERROR_MORE_DATA) { DWORD dw_Bytes = pcbBytesNeeded + sizeof(ENUM_SERVICE_STATUS); lpService = (ENUM_SERVICE_STATUS *) malloc(dw_Bytes) ; lpResumeHandle=NULL ; if(!EnumServicesStatus(hScm, SERVICE_WIN32, SERVICE_ACTIVE, lpService, dw_Bytes, &pcbBytesNeeded, &lpServicesReturned, &lpResumeHandle) ) { dwError = GetLastError(); // _T("%s GetActiveStatus() fail, GetLastError=%d"), szServiceName, dwError); SAFEFREE(lpService) ; CloseServiceHandle(hScm); return 0; } BOOL bFound = FALSE; for (DWORD i=0; i < lpServicesReturned; i++) { CString finder(lpService[i].lpServiceName); if (finder.Compare(szServiceName) == 0) { // Current State if (lpService[i].ServiceStatus.dwCurrentState == SERVICE_RUNNING) { // _T("%s service is running"), szServiceName); } else { // _T("%s service not running, CurrentState=%d"), szServiceName, lpService[i].ServiceStatus.dwCurrentState); } bFound = TRUE; break; } } if (bFound == FALSE) { // _T("%s service not created"), szServiceName); } } SAFEFREE(lpService); CloseServiceHandle(hScm);
C++
상태
서비스
쿼리
Retrieved from http://hyacinth.byus.net/moniwiki/wiki.php/C++/서비스 상태 쿼리
last modified 2014-09-12 16:25:25