C++/서비스 상태 쿼리 Edit Diff Refresh Backlink Random Search History Help Setting Hide Show #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++ 상태 서비스 쿼리 이 글에는 0 개의 댓글이 있습니다. Please enable JavaScript to view the comments powered by Disqus. comments powered by Disqus