如何使用 c/c++ 启动/停止名称中有空格的 Windows 服务 [英] how to start/stop windows services that have spaces in their names using c/c++

查看:43
本文介绍了如何使用 c/c++ 启动/停止名称中有空格的 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 c/c++ 应用程序来启动/停止我系统的任何 Windows 服务.

I am trying to write a c/c++ application that start/stop any windows service of my system.

我的应用程序对于名称之间没有空格的服务工作正常,但对于名称之间有空格的服务没有响应.

My application is working fine for services that have no spaces in between their name, but for service names that have space between them are not responding.

以下是我用来打开服务的命令:

Following are the commands i am using for opening service:

SC_HANDLE serviceDbHandle = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
SC_HANDLE serviceHandle = OpenService(serviceDbHandle,
                                      "My Service",
                                      SC_MANAGER_ALL_ACCESS);

这里 "My Service" - 第二个参数是 LPCSTR 并且它的 null 终止正在产生问题.

Here "My Service" - 2nd parameter is LPCSTR and its null termination is creating problem.

如何启动/停止名称中包含空格的服务 - 任何线索或建议都会有所帮助.

How can i start/stop services that have spaces in their name - any clue or suggestions would be helpful.

我在 xp 机器上使用 Visual Studio 2005.

Iam using Visual studio 2005 in xp machine.

谢谢,

阿尼尔

推荐答案

您需要使用服务的内部名称(没有空格),而不是显示名称(可能).

You need to use the service's internal name (which has no spaces), not the display name (which may).

来自 MSDN:

要打开的服务的名称.这是创建服务对象时 CreateService 函数的 lpServiceName 参数指定的名称,不是用户界面应用程序显示的服务显示名称识别服务.

The name of the service to be opened. This is the name specified by the lpServiceName parameter of the CreateService function when the service object was created, not the service display name that is shown by user interface applications to identify the service.

这篇关于如何使用 c/c++ 启动/停止名称中有空格的 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆