- bool StopNoNeedServices(LPCWSTR ServerName)
- {
- SC_HANDLE hScm, hService;
- SERVICE_STATUS status;
- hScm = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
- hService = OpenService(hScm,ServerName,SERVICE_ALL_ACCESS|DELETE);
- BOOL isSuccess = QueryServiceStatus(hService,&status);
- //指定服务不存在
- if (!isSuccess)
- {
- return true;
- }
- if ( status.dwCurrentState != SERVICE_STOPPED )
- {
- //停用服务
- DWORD dRt = ControlService(hService,SERVICE_CONTROL_STOP,&status);
- if (!dRt)
- {
- cout<<"failed to stop the services "<<UnicodeToAscii(ServerName).c_str()<<endl;
- DWORD dError = GetLastError();
- return false;
- }
- else
- cout<<"success to stop the services "<<UnicodeToAscii(ServerName).c_str()<<endl;
- }
- //禁用服务
- if ( !ChangeServiceConfig(hService,SERVICE_NO_CHANGE,SERVICE_DISABLED,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL) )
- {
- cout<<"failed to disabled the services."<<endl;
- }
- ::CloseServiceHandle( hService);
- ::CloseServiceHandle( hScm);
- return true;
- }