OpenService()打开服务总出错,该如何解决
OpenService()打开服务总出错
代码段:
DWORD dwErr = 0;
//检测服务初始化
memset(&m_SvcStatus, 0x00, sizeof(SERVICE_STATUS));
// 打开服务管理对象
m_hSC = OpenSCManager(NULL, /*NULL*/SERVICES_ACTIVE_DATABASE , /*GENERIC_READ*/SC_MANAGER_ALL_ACCESS);
if (m_hSC != NULL)
{
m_hSvc = OpenService(m_hSC, SERVICE, /*GENERIC_READ*/GENERIC_ALL);
dwErr = GetLastError();
}
问题:
OpenService()打开有的windows的服务,有的却报1060, errlook说是“指定的服务并未以已安装的服务存在。 ”
没有弄明白为什么?
------解决方案--------------------
服务一开始Open失败很正常,你先QueryServiceStatusEx下,然后StartService
代码段:
DWORD dwErr = 0;
//检测服务初始化
memset(&m_SvcStatus, 0x00, sizeof(SERVICE_STATUS));
// 打开服务管理对象
m_hSC = OpenSCManager(NULL, /*NULL*/SERVICES_ACTIVE_DATABASE , /*GENERIC_READ*/SC_MANAGER_ALL_ACCESS);
if (m_hSC != NULL)
{
m_hSvc = OpenService(m_hSC, SERVICE, /*GENERIC_READ*/GENERIC_ALL);
dwErr = GetLastError();
}
问题:
OpenService()打开有的windows的服务,有的却报1060, errlook说是“指定的服务并未以已安装的服务存在。 ”
没有弄明白为什么?
------解决方案--------------------
服务一开始Open失败很正常,你先QueryServiceStatusEx下,然后StartService
- C/C++ code
schService = OpenService( schSCManager, // SCM database szSvcName, // name of service SERVICE_ALL_ACCESS); // full access if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); return; } // Check the status in case the service is not stopped. if (!QueryServiceStatusEx( schService, // handle to service SC_STATUS_PROCESS_INFO, // information level (LPBYTE) &ssStatus, // address of structure sizeof(SERVICE_STATUS_PROCESS), // size of structure &dwBytesNeeded ) ) // size needed if buffer is too small { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } // Check if the service is already running. It would be possible // to stop the service here, but for simplicity this example just returns. if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING) { printf("Cannot start the service because it is already running\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } // Wait for the service to stop before attempting to start it. while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) { // Save the tick count and initial checkpoint. dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; // Do not wait longer than the wait hint. A good interval is // one-tenth of the wait hint but not less than 1 second // and not more than 10 seconds. dwWaitTime = ssStatus.dwWaitHint / 10; if( dwWaitTime < 1000 ) dwWaitTime = 1000; else if ( dwWaitTime > 10000 ) dwWaitTime = 10000; Sleep( dwWaitTime ); // Check the status until the service is no longer stop pending. if (!QueryServiceStatusEx( schService, // handle to service SC_STATUS_PROCESS_INFO, // information level (LPBYTE) &ssStatus, // address of structure sizeof(SERVICE_STATUS_PROCESS), // size of structure &dwBytesNeeded ) ) // size needed if buffer is too small { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } if ( ssStatus.dwCheckPoint > dwOldCheckPoint ) { // Continue to wait and check. dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; } else { if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) { printf("Timeout waiting for service to stop\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } } } // Attempt to start the service. if (!StartService( schService, // handle to service 0, // number of arguments NULL) ) // no arguments { printf("StartService failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; } else printf("Service start pending...\n"); // Check the status until the service is no longer start pending. if (!QueryServiceStatusEx( schService, // handle to service SC_STATUS_PROCESS_INFO, // info level (LPBYTE) &ssStatus, // address of structure sizeof(SERVICE_STATUS_PROCESS), // size of structure &dwBytesNeeded ) ) // if buffer too small { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return; }