#include "WinSvc.h"// Service Control Manager SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM == NULL) { return; }//打开Windows Installer服务 SC_HANDLE hService = ::OpenService(hSCM, "MSIServer", SERVICE_ALL_ACCESS); if (hService == NULL) { return; }SERVICE_STATUS ss; if (::ControlService(hService, SERVICE_CONTROL_INTERROGATE, &ss)) { if (ss.dwCurrentState == SERVICE_RUNNING) { if (::ControlService(hService, SERVICE_CONTROL_STOP, &ss)) AfxMessageBox("服务Windows Installer停止", MB_ICONINFORMATION); } } else { if (::StartService(hService, 0, NULL)) AfxMessageBox("服务Windows Installer启动", MB_ICONINFORMATION); }::CloseServiceHandle(hService);
呵呵,下面使我以前的:BOOL WaitForServiceToReachState(SC_HANDLE hService, DWORD dwDesiredState, SERVICE_STATUS* pss, DWORD dwMilliseconds) { DWORD dwLastState, dwLastCheckPoint; BOOL fFirstTime = TRUE; BOOL fServiceOk = TRUE; DWORD dwTimeout = GetTickCount() + dwMilliseconds; // Loop until the service reaches the desired state. while (TRUE) { // Get state fServiceOk = ::QueryServiceStatus(hService, pss);
// If we can't query the service, we're done if (!fServiceOk) break;
if (pss->dwCurrentState == dwDesiredState) break;
// timed out if ( (dwMilliseconds != INFINITE) && (dwTimeout > GetTickCount())) { SetLastError(ERROR_TIMEOUT); break; }
// If this is our first time, save the service's state & checkpoint if (fFirstTime) { dwLastState = pss->dwCurrentState; dwLastCheckPoint = pss->dwCheckPoint; fFirstTime = FALSE; } else { // If not first time & state has changed, save state & checkpoint if (dwLastState != pss->dwCurrentState) { dwLastState = pss->dwCurrentState; dwLastCheckPoint = pss->dwCheckPoint; } else { // State hasn't change, check that checkpoint is increasing if (pss->dwCheckPoint > dwLastCheckPoint) { // Checkpoint has increased, save checkpoint dwLastCheckPoint = pss->dwCheckPoint; } else { // Checkpoint hasn't increased, service failed, we're done! fServiceOk = FALSE; break; } } } // We're not done, wait the specified period of time Sleep(pss->dwWaitHint); } // return the last SERVICE_STATUS to the caller so // the caller can check the service state and error codes.
return(fServiceOk); }////////////////////////////////////////////////////////////////////////////// // 按照名称停止服务 void Srv_StopMyService(LPCTSTR pszSrv) { #define WAITING_TIMEOUT 10000 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); SC_HANDLE hService = OpenService(hSCM, pszSrv, SERVICE_STOP); // Tell the service to stop. SERVICE_STATUS ss; ControlService(hService, SERVICE_CONTROL_STOP, &ss); // Wait up to 10 seconds for the service to stop. WaitServiceReachState(hService, SERVICE_STOP, &ss, WAITING_TIMEOUT);
// Close the service and the SCM CloseServiceHandle(hService); CloseServiceHandle(hSCM); }
BOOL EnumServicesStatus( SC_HANDLE hSCManager, // handle to SCM database DWORD dwServiceType, // service type DWORD dwServiceState, // service state LPENUM_SERVICE_STATUS lpServices, // status buffer DWORD cbBufSize, // size of status buffer LPDWORD pcbBytesNeeded, // buffer size needed LPDWORD lpServicesReturned, // number of entries returned LPDWORD lpResumeHandle // next entry );
SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCM == NULL)
{
return;
}//打开Windows Installer服务
SC_HANDLE hService = ::OpenService(hSCM, "MSIServer", SERVICE_ALL_ACCESS);
if (hService == NULL)
{
return;
}SERVICE_STATUS ss;
if (::ControlService(hService, SERVICE_CONTROL_INTERROGATE, &ss))
{
if (ss.dwCurrentState == SERVICE_RUNNING)
{
if (::ControlService(hService, SERVICE_CONTROL_STOP, &ss))
AfxMessageBox("服务Windows Installer停止", MB_ICONINFORMATION);
}
}
else
{
if (::StartService(hService, 0, NULL))
AfxMessageBox("服务Windows Installer启动", MB_ICONINFORMATION);
}::CloseServiceHandle(hService);
SERVICE_STATUS* pss, DWORD dwMilliseconds)
{
DWORD dwLastState,
dwLastCheckPoint;
BOOL fFirstTime = TRUE;
BOOL fServiceOk = TRUE;
DWORD dwTimeout = GetTickCount() + dwMilliseconds; // Loop until the service reaches the desired state.
while (TRUE)
{
// Get state
fServiceOk = ::QueryServiceStatus(hService, pss);
// If we can't query the service, we're done
if (!fServiceOk)
break;
if (pss->dwCurrentState == dwDesiredState)
break;
// timed out
if ( (dwMilliseconds != INFINITE) &&
(dwTimeout > GetTickCount()))
{
SetLastError(ERROR_TIMEOUT);
break;
}
// If this is our first time, save the service's state & checkpoint
if (fFirstTime)
{
dwLastState = pss->dwCurrentState;
dwLastCheckPoint = pss->dwCheckPoint;
fFirstTime = FALSE;
}
else
{
// If not first time & state has changed, save state & checkpoint
if (dwLastState != pss->dwCurrentState)
{
dwLastState = pss->dwCurrentState;
dwLastCheckPoint = pss->dwCheckPoint;
}
else
{
// State hasn't change, check that checkpoint is increasing
if (pss->dwCheckPoint > dwLastCheckPoint)
{
// Checkpoint has increased, save checkpoint
dwLastCheckPoint = pss->dwCheckPoint;
}
else
{
// Checkpoint hasn't increased, service failed, we're done!
fServiceOk = FALSE;
break;
}
}
}
// We're not done, wait the specified period of time
Sleep(pss->dwWaitHint);
}
// return the last SERVICE_STATUS to the caller so
// the caller can check the service state and error codes.
return(fServiceOk);
}//////////////////////////////////////////////////////////////////////////////
// 按照名称停止服务
void Srv_StopMyService(LPCTSTR pszSrv)
{
#define WAITING_TIMEOUT 10000
SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); SC_HANDLE hService = OpenService(hSCM, pszSrv, SERVICE_STOP);
// Tell the service to stop.
SERVICE_STATUS ss;
ControlService(hService, SERVICE_CONTROL_STOP, &ss); // Wait up to 10 seconds for the service to stop.
WaitServiceReachState(hService, SERVICE_STOP, &ss, WAITING_TIMEOUT);
// Close the service and the SCM
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
}
SC_HANDLE hSCManager, // handle to SCM database
DWORD dwServiceType, // service type
DWORD dwServiceState, // service state
LPENUM_SERVICE_STATUS lpServices, // status buffer
DWORD cbBufSize, // size of status buffer
LPDWORD pcbBytesNeeded, // buffer size needed
LPDWORD lpServicesReturned, // number of entries returned
LPDWORD lpResumeHandle // next entry
);
//提升进程的权限,lpszPrivilegeName表示进程的权限名 取值请参看winnt.h定义
BOOL CSystem:: EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable)
{
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY | TOKEN_READ, &hToken))
return FALSE; LUID luid;
if (!LookupPrivilegeValue(NULL, lpszPrivilegeName, &luid))
return TRUE; TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = (bEnable) ? SE_PRIVILEGE_ENABLED : 0; AdjustTokenPrivileges(hToken, FALSE, &tp, NULL, NULL, NULL); CloseHandle(hToken); return (GetLastError() == ERROR_SUCCESS);
}