C/C++ Windows API——关闭、启动、查询服务状态
【摘要】
// ServiceDemo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#inc...
// ServiceDemo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
int main()
{
BOOL ret;
/*
_Must_inspect_result_ WINADVAPI SC_HANDLE WINAPI OpenSCManagerW(
_In_opt_ LPCWSTR lpMachineName,
_In_opt_ LPCWSTR lpDatabaseName,
_In_ DWORD dwDesiredAccess
);
dwDesiredAccess可以是如下值
#define SC_MANAGER_CONNECT 0x0001
#define SC_MANAGER_CREATE_SERVICE 0x0002
#define SC_MANAGER_ENUMERATE_SERVICE 0x0004
#define SC_MANAGER_LOCK 0x0008
#define SC_MANAGER_QUERY_LOCK_STATUS 0x0010
#define SC_MANAGER_MODIFY_BOOT_CONFIG 0x0020
#define SC_MANAGER_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | \
SC_MANAGER_CONNECT | \
SC_MANAGER_CREATE_SERVICE | \
SC_MANAGER_ENUMERATE_SERVICE | \
SC_MANAGER_LOCK | \
SC_MANAGER_QUERY_LOCK_STATUS | \
SC_MANAGER_MODIFY_BOOT_CONFIG)
*/
LPCWSTR lpMachineName = NULL;
LPCWSTR lpDatabaseName = NULL;
DWORD dwSCManagerDesiredAccess = SC_MANAGER_ALL_ACCESS;
SC_HANDLE scHandle = OpenSCManager(lpMachineName, lpDatabaseName, dwSCManagerDesiredAccess);
if (scHandle == NULL) {
printf("OpenSCManager -> ret=0 -> fail(%ld)\n", GetLastError());
}
else {
printf("OpenSCManager -> succ\n");
/*
_Must_inspect_result_ WINADVAPI SC_HANDLE WINAPI OpenService(
_In_ SC_HANDLE hSCManager,
_In_ LPCTSTR lpServiceName,
_In_ DWORD dwDesiredAccess
);
#define SERVICE_QUERY_CONFIG 0x0001
#define SERVICE_CHANGE_CONFIG 0x0002
#define SERVICE_QUERY_STATUS 0x0004
#define SERVICE_ENUMERATE_DEPENDENTS 0x0008
#define SERVICE_START 0x0010
#define SERVICE_STOP 0x0020
#define SERVICE_PAUSE_CONTINUE 0x0040
#define SERVICE_INTERROGATE 0x0080
#define SERVICE_USER_DEFINED_CONTROL 0x0100
#define SERVICE_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | \
SERVICE_QUERY_CONFIG | \
SERVICE_CHANGE_CONFIG | \
SERVICE_QUERY_STATUS | \
SERVICE_ENUMERATE_DEPENDENTS | \
SERVICE_START | \
SERVICE_STOP | \
SERVICE_PAUSE_CONTINUE | \
SERVICE_INTERROGATE | \
SERVICE_USER_DEFINED_CONTROL)
*/
//打印服务
LPCTSTR lpServiceName = _T("Spooler");
DWORD dwDesiredAccess = SERVICE_QUERY_STATUS | SERVICE_START | SERVICE_STOP;
SC_HANDLE serviceHandle = OpenService(scHandle, lpServiceName, dwDesiredAccess);
if (serviceHandle == NULL) {
printf("OpenService scHandle=%p, lpServiceName=%s -> ret=0 -> fail(%ld)\n", scHandle, lpServiceName, GetLastError());
}
else {
printf("OpenService scHandle=%p, lpServiceName=%s -> serviceHandle=%p\n", scHandle, lpServiceName, serviceHandle);
/*
WINADVAPI BOOL WINAPI StartServiceW(
_In_ SC_HANDLE hService,
_In_ DWORD dwNumServiceArgs,
_In_reads_opt_(dwNumServiceArgs)
LPCWSTR *lpServiceArgVectors
);
*/
ret = StartService(serviceHandle, NULL, NULL);
if (!ret) {
//〖1056〗-服务的实例已在运行中。
printf("StartService serviceHandle=%p -> fail(%ld)\n", serviceHandle, GetLastError());
} else {
printf("StartService serviceHandle=%p -> succ\n", serviceHandle);
/*
_Must_inspect_result_ WINADVAPI BOOL WINAPI QueryServiceStatus(
_In_ SC_HANDLE hService,
_Out_ LPSERVICE_STATUS lpServiceStatus
);
typedef struct _SERVICE_STATUS {
DWORD dwServiceType;
DWORD dwCurrentState;
DWORD dwControlsAccepted;
DWORD dwWin32ExitCode;
DWORD dwServiceSpecificExitCode;
DWORD dwCheckPoint;
DWORD dwWaitHint;
} SERVICE_STATUS, *LPSERVICE_STATUS;
dwServiceType可以是下列值
#define SERVICE_KERNEL_DRIVER 0x00000001
#define SERVICE_FILE_SYSTEM_DRIVER 0x00000002
#define SERVICE_ADAPTER 0x00000004
#define SERVICE_RECOGNIZER_DRIVER 0x00000008
#define SERVICE_DRIVER (SERVICE_KERNEL_DRIVER | \
SERVICE_FILE_SYSTEM_DRIVER | \
SERVICE_RECOGNIZER_DRIVER)
#define SERVICE_WIN32_OWN_PROCESS 0x00000010
#define SERVICE_WIN32_SHARE_PROCESS 0x00000020
#define SERVICE_WIN32 (SERVICE_WIN32_OWN_PROCESS | \
SERVICE_WIN32_SHARE_PROCESS)
#define SERVICE_INTERACTIVE_PROCESS 0x00000100
#define SERVICE_TYPE_ALL (SERVICE_WIN32 | \
SERVICE_ADAPTER | \
SERVICE_DRIVER | \
SERVICE_INTERACTIVE_PROCESS)
dwCurrentState可以是如下值
#define SERVICE_STOPPED 0x00000001
#define SERVICE_START_PENDING 0x00000002
#define SERVICE_STOP_PENDING 0x00000003
#define SERVICE_RUNNING 0x00000004
#define SERVICE_CONTINUE_PENDING 0x00000005
#define SERVICE_PAUSE_PENDING 0x00000006
#define SERVICE_PAUSED 0x00000007
*/
SERVICE_STATUS serviceStatus = { 0 };
do {
ret = QueryServiceStatus(serviceHandle, &serviceStatus);
//休眠(挂起线程),单位毫秒
Sleep(300);
} while (ret && serviceStatus.dwCurrentState != SERVICE_RUNNING);
}
/*
WINADVAPI BOOL WINAPI ControlService(
_In_ SC_HANDLE hService,
_In_ DWORD dwControl, //需要有SERVICE_USER_DEFINED_CONTROL权限。
_Out_ LPSERVICE_STATUS lpServiceStatus
);
dwControl可以取如下值
#define SERVICE_CONTROL_STOP 0x00000001
#define SERVICE_CONTROL_PAUSE 0x00000002
#define SERVICE_CONTROL_CONTINUE 0x00000003
#define SERVICE_CONTROL_INTERROGATE 0x00000004
#define SERVICE_CONTROL_SHUTDOWN 0x00000005
#define SERVICE_CONTROL_PARAMCHANGE 0x00000006
#define SERVICE_CONTROL_NETBINDADD 0x00000007
#define SERVICE_CONTROL_NETBINDREMOVE 0x00000008
#define SERVICE_CONTROL_NETBINDENABLE 0x00000009
#define SERVICE_CONTROL_NETBINDDISABLE 0x0000000A
#define SERVICE_CONTROL_DEVICEEVENT 0x0000000B
#define SERVICE_CONTROL_HARDWAREPROFILECHANGE 0x0000000C
#define SERVICE_CONTROL_POWEREVENT 0x0000000D
#define SERVICE_CONTROL_SESSIONCHANGE 0x0000000E
#define SERVICE_CONTROL_PRESHUTDOWN 0x0000000F
#define SERVICE_CONTROL_TIMECHANGE 0x00000010
#define SERVICE_CONTROL_TRIGGEREVENT 0x00000020
*/
DWORD dwControl = SERVICE_CONTROL_STOP;
SERVICE_STATUS serviceStatus = { 0 };
//关闭服务
ret = ControlService(serviceHandle, dwControl, &serviceStatus);
//〖1052〗-请求的控件对此服务无效。〖1062〗-服务未启动。
printf("ControlService serviceHandle=%p, dwControl=%ld -> ret=%d, GetLastError()=%ld -> serviceStatus.dwServiceType=%ld, dwCurrentState=%ld, dwControlsAccepted=%ld, dwWin32ExitCode=%ld, dwServiceSpecificExitCode=%ld, dwCheckPoint=%ld, dwWaitHint=%ld\n",
serviceHandle, dwControl, ret, GetLastError(),
serviceStatus.dwServiceType,
serviceStatus.dwCurrentState,
serviceStatus.dwControlsAccepted,
serviceStatus.dwWin32ExitCode,
serviceStatus.dwServiceSpecificExitCode,
serviceStatus.dwCheckPoint,
serviceStatus.dwWaitHint
);
//The CloseServiceHandle function does not destroy the service control manager object referred to by the handle. A service control manager object cannot be destroyed. A service object can be destroyed by calling the DeleteService function.
CloseServiceHandle(serviceHandle);
DeleteService(serviceHandle);
}
CloseServiceHandle(scHandle);
DeleteService(serviceHandle);
}
system("pause");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
代码中用于测试的服务是Spooler(即打印服务)
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/52906215
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)