SC_HANDLE OpenSCManager( LPCTSTR lpMachineName, // pointer to machine name string LPCTSTR lpDatabaseName, // pointer to database name string DWORD dwDesiredAccess // type of access );
SC_HANDLE OpenService( SC_HANDLE hSCManager, // handle to service control manager // database LPCTSTR lpServiceName, // pointer to name of service to start DWORD dwDesiredAccess // type of access to service );
BOOL EnumServicesStatus( SC_HANDLE hSCManager, // handle to service control manager database DWORD dwServiceType, // type of services to enumerate DWORD dwServiceState, // state of services to enumerate LPENUM_SERVICE_STATUS lpServices, // pointer to service status buffer DWORD cbBufSize, // size of service status buffer LPDWORD pcbBytesNeeded, // pointer to variable for bytes needed LPDWORD lpServicesReturned, // pointer to variable for number returned LPDWORD lpResumeHandle // pointer to variable for next entry );
BOOL QueryServiceConfig( SC_HANDLE hService, // handle of service LPQUERY_SERVICE_CONFIG lpServiceConfig, // address of service config. structure DWORD cbBufSize, // size of service configuration buffer LPDWORD pcbBytesNeeded // address of variable for bytes needed );
SERVICE_STATUS status; BOOL isSuccess=QueryServiceStatus(service,&status); if (!isSuccess) { Printf("QueryServiceStatus error!\n"); }
if ( status.dwCurrentState==SERVICE_STOPPED ) { isSuccess=StartService(service,NULL,NULL);
if (!isSuccess) { Printf("启动服务失败!"); }
} 下面是停止服务的实现代码:
if ( status.dwCurrentState!=SERVICE_STOPPED ) { isSuccess=ControlService(service,SERVICE_CONTROL_STOP,&status); if (!isSuccess ) { Printf("停止服务失败!"); } }
创建/删除服务
创建服务使用的API为CreateService,它的原形为:
SC_HANDLE CreateService( SC_HANDLE hSCManager, // handle to service control manager // database LPCTSTR lpServiceName, // pointer to name of service to start LPCTSTR lpDisplayName, // pointer to display name DWORD dwDesiredAccess, // type of access to service DWORD dwServiceType, // type of service DWORD dwStartType, // when to start service DWORD dwErrorControl, // severity if service fails to start LPCTSTR lpBinaryPathName, // pointer to name of binary file LPCTSTR lpLoadOrderGroup, // pointer to name of load ordering // group LPDWORD lpdwTagId, // pointer to variable to get tag identifier LPCTSTR lpDependencies, // pointer to array of dependency names LPCTSTR lpServiceStartName, // pointer to account name of service LPCTSTR lpPassword // pointer to password for service account );
BOOL ChangeServiceConfig( SC_HANDLE hService // handle to service DWORD dwServiceType, // type of service DWORD dwStartType, // when to start service DWORD dwErrorControl, // severity if service fails to start LPCTSTR lpBinaryPathName, // pointer to service binary file name LPCTSTR lpLoadOrderGroup, // pointer to load ordering group name LPDWORD lpdwTagId, // pointer to variable to get tag identifier LPCTSTR lpDependencies, // pointer to array of dependency names LPCTSTR lpServiceStartName, // pointer to account name of service LPCTSTR lpPassword, // pointer to password for service account LPCTSTR lpDisplayName // pointer to display name );
SC_LOCK sclLock; sclLock = LockServiceDatabase(scm); if (sclLock == NULL) {
if (GetLastError() != ERROR_SERVICE_DATABASE_LOCKED) Printf("LockServiceDatabase error\n");
} if (! ChangeServiceConfig( service, // handle of service SERVICE_NO_CHANGE, // service type: no change SERVICE_DISABLED, // 这里做了更改 SERVICE_NO_CHANGE, // error control: no change NULL, // binary path: no change NULL, // load order group: no change NULL, // tag ID: no change NULL, // dependencies: no change NULL, // account name: no change NULL, // password: no change NULL)) //displayname { Printf("ChangeServiceConfig error!\n"); } UnlockServiceDatabase(sclLock);