hSCManager,hService:SC_HANDLE;
   returnstatus:TServiceStatus;
.
.
.
.
.
procedure CloseService(ServerName:String);
begin
  hService:=OpenService(hscmanager,Pchar(ServerName),SERVICE_ALL_ACCESS);
  if hService>=0 then
  begin
    QueryServiceStatus(hService,returnstatus);
    if returnstatus.dwCurrentState=SERVICE_RUNNING then
      ControlService(hService,SERVICE_CONTROL_STOP,returnstatus);
    CloseServiceHandle(hService);//关闭该Service
  end;//if hService>=0 then
end;
.
.
.
.
.请问我这个函数有问题吗?怎么关闭不了服务啊!

解决方案 »

  1.   

    你调用ControlService返回的returnstatus值是多少啊?
      

  2.   

    你开发环境是什么,用的账号是管理员吗,
    还是,要用一些WINDOWS自带的函数什么的,最好看看它的使用环境,条件。
      

  3.   

    BOOL SetServiceStatus(    SERVICE_STATUS_HANDLE hServiceStatus~, // service status handle 
        LPSERVICE_STATUS lpServiceStatus  // address of status structure 
       );Value Meaning
    SERVICE_STOPPED The service is not running.
    SERVICE_START_PENDING The service is starting.
    SERVICE_STOP_PENDING The service is stopping.
    SERVICE_RUNNING The service is running.
    SERVICE_CONTINUE_PENDING The service continue is pending.
    SERVICE_PAUSE_PENDING The service pause is pending.
    SERVICE_PAUSED The service is paused.
      

  4.   

    我的是管理员权限!hService这个变量的值一直是0。不知道为什么!谁有相关的代码给我用用!
      

  5.   

    http://borland.mblogger.cn/aiirii/posts/2893.aspx
      

  6.   

    呵呵,好好利用Googlehttp://lysoft.7u7.net