SC_HANDLE hSCManager = NULL;
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
int nRet = 0;
if(hSCManager == NULL)
{
return ;
}
DWORD dwServicesReturned = 0;
DWORD cbBytesNeeded = 0;
DWORD cbBufSize = 0;
nRet = EnumServicesStatus(hSCManager,SERVICE_WIN32,SERVICE_STATE_ALL,NULL,0,&cbBytesNeeded,&dwServicesReturned,0);
ENUM_SERVICE_STATUS *lpServices = new ENUM_SERVICE_STATUS[cbBytesNeeded];
cbBufSize = cbBytesNeeded;
nRet = EnumServicesStatus(hSCManager,SERVICE_WIN32,SERVICE_STATE_ALL,lpServices,cbBufSize,&cbBytesNeeded,&dwServicesReturned,0);
if(nRet == 0)
{
return;
}
if(lpServices == 0)
{
CloseServiceHandle(hSCManager);
return;
}
for(DWORD i = 0; i < dwServicesReturned; i++, lpServices++)
{
LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
lvi.state =LVIS_STATEIMAGEMASK;
lvi.stateMask =0;
lvi.iSubItem = 0;
lvi.iItem = i++;
lvi.pszText = lpServices->lpServiceName;
lvi.iImage = 0;
int i = ListView_InsertItem(hServiceList,&lvi);
ListView_SetItemText(hServiceList,i,1,lpServices->lpDisplayName);
ListView_SetItemText(hServiceList,i,2,ServiceSysGetType(lpServices->ServiceStatus.dwCurrentState));
}
CloseServiceHandle(hSCManager);
------------------------------------------启动
SC_HANDLE hSCManager = NULL,hService=NULL;
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hSCManager == NULL)
{
MessageBox(NULL,"err hsc",0,0);
return ;
}
hService = OpenService(hSCManager,sername,SERVICE_ALL_ACCESS);
if(hService==NULL)
{
MessageBox(NULL,"err serv",0,0);
return;
}
if(StartService(hService,0,NULL))
{
CloseServiceHandle(hSCManager);
ServiceSysEnum();
}
-------------------------------------停止--------------
SC_HANDLE hSCManager,hService;
hSCManager = OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS);
if(hSCManager == NULL)
{
return;
}
hService = OpenService(hSCManager,sername,SERVICE_ALL_ACCESS);
if(hService == NULL)
{
CloseServiceHandle(hSCManager);
return;
}
if(!StartService(hService,0,NULL))
{
SERVICE_STATUS ServerStatus;
ControlService(hService,SERVICE_CONTROL_STOP,&ServerStatus);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
ServiceSysEnum();
return;
}