OpenService()打开服务总出错

cmh360 2009-10-16 09:17:21
代码段:

DWORD dwErr = 0;

//检测服务初始化
memset(&m_SvcStatus, 0x00, sizeof(SERVICE_STATUS));
// 打开服务管理对象
m_hSC = OpenSCManager(NULL, /*NULL*/SERVICES_ACTIVE_DATABASE , /*GENERIC_READ*/SC_MANAGER_ALL_ACCESS);
if (m_hSC != NULL)
{
m_hSvc = OpenService(m_hSC, SERVICE, /*GENERIC_READ*/GENERIC_ALL);
dwErr = GetLastError();
}

问题:

OpenService()打开有的windows的服务,有的却报1060, errlook说是“指定的服务并未以已安装的服务存在。 ”
没有弄明白为什么?


...全文
625 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
resent 2011-11-15
  • 打赏
  • 举报
回复
楼主,没分啊...所以我选择沉默.
cmh360 2009-10-16
  • 打赏
  • 举报
回复


可能系统有的服务是不能打开的
cmh360 2009-10-16
  • 打赏
  • 举报
回复

schService = OpenService(
schSCManager, // SCM database
szSvcName, // name of service
SERVICE_ALL_ACCESS); // full access

问题是,我这个都是失败的,无法往下进行了
MoXiaoRab 2009-10-16
  • 打赏
  • 举报
回复
服务一开始Open失败很正常,你先QueryServiceStatusEx下,然后StartService


schService = OpenService(
schSCManager, // SCM database
szSvcName, // name of service
SERVICE_ALL_ACCESS); // full access

if (schService == NULL)
{
printf("OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}

// Check the status in case the service is not stopped.

if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}

// Check if the service is already running. It would be possible
// to stop the service here, but for simplicity this example just returns.

if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING)
{
printf("Cannot start the service because it is already running\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}

// Wait for the service to stop before attempting to start it.

while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
// Save the tick count and initial checkpoint.

dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;

// Do not wait longer than the wait hint. A good interval is
// one-tenth of the wait hint but not less than 1 second
// and not more than 10 seconds.

dwWaitTime = ssStatus.dwWaitHint / 10;

if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;

Sleep( dwWaitTime );

// Check the status until the service is no longer stop pending.

if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}

if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// Continue to wait and check.

dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
printf("Timeout waiting for service to stop\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
}
}

// Attempt to start the service.

if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
printf("StartService failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
else printf("Service start pending...\n");

// Check the status until the service is no longer start pending.

if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}

cmh360 2009-10-16
  • 打赏
  • 举报
回复
UP
cmh360 2009-10-16
  • 打赏
  • 举报
回复
_T("ClipBook")
MoXiaoRab 2009-10-16
  • 打赏
  • 举报
回复
SERVICE怎么写的
cmh360 2009-10-16
  • 打赏
  • 举报
回复
真没有人知道?

2,643

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧