【发布时间】:2015-07-15 01:42:46
【问题描述】:
我通过执行以下操作创建了一个运行良好的 Windows 服务。
SC_HANDLE hService = ::CreateService(*m_ServiceConfig, // SCM database
name.c_str(), // name of service
displayname.c_str(), // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // service type (interactive for debug)
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
path.c_str(), // path to service's binary
nullptr, // no load ordering group
nullptr, // no tag identifier
dependencies, // dependencies
nullptr, // LocalSystem account
nullptr); // no password
如您所见,我将访问权限指定为SERVICE_ALL_ACCESS,并且在线此常量声明您有权暂停继续并停止服务,
但是,当我运行命令 net stop <service-name> 时,我得到以下输出
请求的暂停继续或停止对此服务无效
我的问题是我创建服务的方式有什么问题吗?
服务控制处理函数
DWORD WINAPI ServiceControlHandler(DWORD dwControl, DWORD dwEventType, LPVOID /*lpEventData*/, LPVOID /*lpContext*/)
{
// Handle the requested control code.
switch (dwControl)
{
case SERVICE_CONTROL_STOP:
TVLOG_INFO(L"SERVICE_CONTROL_STOP");
{
MessageBoxA(NULL, "Please stop the service", "Uninstall service error!", MB_OK | MB_ICONERROR);
}
【问题讨论】:
-
如果没有看到您的 ServiceMain 或 Control Handler 函数,很难判断。
-
@IInspectable 我已经添加了我的控制处理函数,我现在只是使用停止命令
-
请说明您如何向 SCM 报告服务状态。这会影响其他应用程序(例如
net)与您的服务交互的方式。此外,如果不指定MB_SERVICE_NOTIFICATION标志,则不能在服务中使用MessageBox()。