急:API函数StartService()的用法?

Richard2001 2001-07-24 01:43:34
代码如下:

SC_HANDLE hService,hSCManager;
hService=NULL;

if ((hSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS)) == NULL){
Application->MessageBox("打开系统服务控制台失败!","出错信息",0);
CloseServiceHandle(hSCManager);
return;
}
if ((hService=OpenService(hSCManager,"CustomReqTransService",SERVICE_START)) == NULL){
Application->MessageBox("打开服务失败!","出错信息",0);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return;
}

if (StartService(hService,0,NULL) == 0){
Application->MessageBox("服务启动失败!","出错信息",0);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return;
}
Application->MessageBox("服务启动成功,请用系统的服务控制台查看!","信息",0);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);

但StartService(hService,0,NULL)的执行不能成功,请问如何解决?

...全文
630 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Richard2001 2001-07-27
  • 打赏
  • 举报
回复
斑竹们,高手们,大侠们,大哥大姐们,Help me please!
Richard2001 2001-07-26
  • 打赏
  • 举报
回复
是服务名没错。
经努力,服务已能处于“启动“状态(即SERVICE_START_PENDING)状态,服务控制面板上的对应的“停止“按钮都是灰的。我要的是处于“已启动“(即SERVICE_RUNNING)状态。

问题在哪里?

代码:

void __fastcall TConsoleForm::btnStartClick(TObject *Sender)
{
SC_HANDLE hService;
hService=NULL;

if ((hSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT)) == NULL){
Application->MessageBox("打开系统服务控制台失败!","出错信息",0);
CloseServiceHandle(hSCManager);
return;
}
if ((hService=OpenService(hSCManager,("SendInService"),SERVICE_START)) == NULL){
Application->MessageBox("打开服务失败!","出错信息",0);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return;
}

if (StartService(hService,0,NULL) == 0){
Application->MessageBox("服务启动失败!" ,"出错信息",0);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return;
}


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

SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwStatus;

if (!QueryServiceStatus(hService,&ssStatus))
{
Application->MessageBox("Query Service Status 失败!" ,"出错信息",0);
}

// Save the tick count and initial checkpoint.

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

while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.

dwWaitTime = ssStatus.dwWaitHint / 10;

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

Sleep( dwWaitTime );

// Check the status again.

if (!QueryServiceStatus(
hService, // handle to service
&ssStatus) ) // address of structure
break;

if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.

dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint
break;
}
}
}

if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
Application->MessageBox("服务启动成功,请用系统的服务控制台查看!","信息",0);
dwStatus = NO_ERROR;
}
else
{
Application->MessageBox("服务启动不成功!","信息",0);
dwStatus = GetLastError();
}

CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);

}


Richard2001 2001-07-26
  • 打赏
  • 举报
回复
我的机器配置:Dell GX110机,PIII 864MHz Cpu,Windows 2000 Professional
Richard2001 2001-07-25
  • 打赏
  • 举报
回复
谢谢帮忙。但怎么样解决这个问题?
土豆 2001-07-25
  • 打赏
  • 举报
回复
hService=OpenService(hSCManager,"CustomReqTransService",SERVICE_START)
单从代码看不出错误,名称对不对?有两个名字的,一个服务名,一个显示的名字,你有没有弄错?这里要用服务名
土豆 2001-07-24
  • 打赏
  • 举报
回复
用VC的工具ErrorLookUP 查到:1083配置成在该可执行程序中运行的这个服务不能执行该服务。
Richard2001 2001-07-24
  • 打赏
  • 举报
回复
用GetLastError()返回错误码:1083。
但我的MSDN上只能查到 Error Codes: 1082,没有1083。
Richard2001 2001-07-24
  • 打赏
  • 举报
回复
To victorchen_2000(微力):

Service 也是自己用C++写的,在DOS窗口下用 c:\>Service.exe /install 安装,在服务控制台中能正确启动,并工作正常。

现在想做个界面,有安装服务(用CreateService(..)函数),启动服务(上面的代码),停止服务(ControlService()),御载服务(DeleteService())等功能。其中,安装服务和御载服务功能已测试通过。(停止服务功能要等启动功能做好才能测试)

用CreateService()函数安装的服务不能在服务控制面板中手动启动,只能用StartService()启动。



victorchen_2000 2001-07-24
  • 打赏
  • 举报
回复
service是你自己写的吗?如果是的,是不是Service写的有错误?
用GetLastError看看什么错误啊!
可以在控制面板中手动启动吗?
Richard2001 2001-07-24
  • 打赏
  • 举报
回复
高手们,请出手!

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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