为什么 OpenService() 在我检查窗口服务状态时不起作用?
why OpenService() doesn't work when I check a window service status?
我今天下午都在处理一个非常困惑的问题,我想通过QueryServiceStatusEx检查Windows服务状态,但总是得到0。MSDN 说
"如果函数失败,则返回值为零。要扩展 错误信息,调用 GetLastError。
为了获得更多错误信息,我调用 GetLastError,错误代码为 1。
ERROR_INVALID_HANDLE:句柄无效。
这是我的代码,例如我检查窗口服务:"后台处理程序",我的代码哪里有问题?为什么我无法使用 OpenService(( SC_HANDLE服务?
bool isServiceStart()
{
SERVICE_STATUS_PROCESS status;
SC_HANDLE schSCManager;
SC_HANDLE schService;
//get hadnle to the scm database
schSCManager = OpenSCManager(
NULL, //local machine
NULL, //services acitive database
SC_MANAGER_ALL_ACCESS
);
if(NULL == schSCManager){
qDebug() << "Open SCManager failed: " << (GetLastError() == ERROR_ACCESS_DENIED);
CloseServiceHandle(schSCManager);
return false;
}
//Get a hadle to the service
QString serviceName = "Spooler";
schService = OpenService(
schSCManager, //database
(LPCTSTR)serviceName.data(),
SERVICE_ALL_ACCESS
);
if(schService == NULL){
qDebug() << "service doesn't exist: " << GetLastError();
CloseServiceHandle(schSCManager);
return false;
}
DWORD dwBytesNeeded;
if(!QueryServiceStatusEx(
schService,
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &status, // address of structure
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded // size needed if buffer is too small
))
{
qInfo() << "service status" << status.dwCurrentState;
}else{
qInfo() << "hahaha alway is 0" <<GetLastError();
}
return false;
}
你的条件是错误的,你写"hahaha alway is 0"
当QueryServiceStatusEx
返回非零时。
要么移除条件下的!
运算符,要么切换输出的位置。
相关文章:
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- OSX MetalKit CVMetalTextureCacheCreateTextureFromImage失败,状态:
- 检测win32服务创建和删除的最佳方法
- std::future_error:无关联状态
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 如何避免LED在循环状态变化中闪烁?
- boost 是否有按特殊类型值编码状态"compact optional"?
- gRPC 生成的代码缺少服务实现
- 在使用 gSoap 链接多个 SOAP 服务时启用保持活动状态
- 如何使用 sd-bus 获取服务的状态?
- 为什么 OpenService() 在我检查窗口服务状态时不起作用?
- Windows服务处于“启动”状态
- 服务始终处于STOPPED状态
- 修改c/c++中Windows服务的状态
- 如何以编程方式获取服务的状态(运行/停止)
- Windows监听服务状态改变
- 如何使Windows服务状态为"Manual"?