- VOID DoSvcStartType()
- {
- SC_HANDLE schSCManager;
- SC_HANDLE schService;
- // Get a handle to the SCM database.
- schSCManager = OpenSCManager(
- NULL, // local computer
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access rights
- if (NULL == schSCManager)
- {
- printf("OpenSCManager failed (%d)/n", GetLastError());
- return;
- }
- // Get a handle to the service.
- schService = OpenService(
- schSCManager, // SCM database
- szSvcName, // name of service
- SERVICE_CHANGE_CONFIG); // need change config access
- if (schService == NULL)
- {
- printf("OpenService failed (%d)/n", GetLastError());
- CloseServiceHandle(schSCManager);
- return;
- }
- // Change the service start type.
- SC_LOCK sclLock;
- sclLock = LockServiceDatabase(schService);
- if (! ChangeServiceConfig(
- schService, // handle of service
- SERVICE_NO_CHANGE, // service type: no change
- //SERVICE_CHANGE_CONFIG,
- SERVICE_DEMAND_START, // service start type
- 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
- L"ABC", // password: no change
- NULL) ) // display name: no change
- {
- //SERVICE_CHANGE_CONFIG
- printf("ChangeServiceConfig failed (%d)/n", GetLastError());
- }
- else printf("Service enabled successfully./n");
- //将启动类型更改为禁用
- if (! ChangeServiceConfig(
- schService, // handle of service
- SERVICE_NO_CHANGE, // service type: no change
- SERVICE_DISABLED,
- //SERVICE_DEMAND_START, // service start type
- 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) ) // display name: no change
- {
- //SERVICE_CHANGE_CONFIG
- printf("2:ChangeServiceConfig failed (%d)/n", GetLastError());
- }
- else printf("2:Service enabled successfully./n");
- UnlockServiceDatabase(sclLock);
- CloseServiceHandle(schService);
- CloseServiceHandle(schSCManager);
- }
c++写修改Windows服务类型的代码
最新推荐文章于 2023-02-10 16:15:30 发布