SC_HANDLE sc=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); //服务控制器
//获得服务状态
DWORD get_srvst(SC_HANDLE sc)
{
SC_HANDLE schService;
if (NULL == sc)
printf("OpenSCManager failed (%d)\n", GetLastError());
schService = OpenService(
sc, // SCM database
L"sharedaccess", // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return 0;
}
SC_STATUS_TYPE st;
LPSERVICE_STATUS_PROCESS buf;
buf=(LPSERVICE_STATUS_PROCESS) LocalAlloc(LPTR, 1024);
DWORD dwneed;
if(QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,(LPBYTE)buf,1024,&dwneed))
{
return buf->dwCurrentState;
}
return 0;
}
//获得服务状态
DWORD get_srvst(SC_HANDLE sc)
{
SC_HANDLE schService;
if (NULL == sc)
printf("OpenSCManager failed (%d)\n", GetLastError());
schService = OpenService(
sc, // SCM database
L"sharedaccess", // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return 0;
}
SC_STATUS_TYPE st;
LPSERVICE_STATUS_PROCESS buf;
buf=(LPSERVICE_STATUS_PROCESS) LocalAlloc(LPTR, 1024);
DWORD dwneed;
if(QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,(LPBYTE)buf,1024,&dwneed))
{
return buf->dwCurrentState;
}
return 0;
}
int Open_Service(SC_HANDLE sc)
{
SC_HANDLE schService;
if (NULL == sc)
printf("OpenSCManager failed (%d)\n", GetLastError());
schService = OpenService(
sc, // SCM database
L"sharedaccess", // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return 0;
}
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
return 0;
}
else
{
printf("Service start pending.\n");
}
CloseServiceHandle(schService);
return 0;
}
int Close_Service(SC_HANDLE sc)
{
SC_HANDLE schService;
if (NULL == sc)
printf("OpenSCManager failed (%d)\n", GetLastError());
schService = OpenService(
sc, // SCM database
L"sharedaccess", // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
return 0;
}
SERVICE_STATUS ssStatus;
ControlService(schService,SERVICE_CONTROL_STOP,&ssStatus);
CloseServiceHandle(schService);
}
Value | Meaning |
---|---|
SERVICE_CONTINUE_PENDING 0x00000005 |
The service continue is pending. |
SERVICE_PAUSE_PENDING 0x00000006 |
The service pause is pending. |
SERVICE_PAUSED 0x00000007 |
The service is paused. |
SERVICE_RUNNING 0x00000004 |
The service is running. |
SERVICE_START_PENDING 0x00000002 |
The service is starting. |
SERVICE_STOP_PENDING 0x00000003 |
The service is stopping. |
SERVICE_STOPPED 0x00000001 |
The service is not running. |