我用下面的这一段代码实现对服务是否安装的判断,但是会出现有些服务能判断出来,有些却判断不出来,请高人指点下
use WinSvc;//-------------------------------------
// 获取某个系统服务的当前状态
//
// return status code if successful
// -1 if not
//
// return codes:
// SERVICE_STOPPED
// SERVICE_RUNNING
// SERVICE_PAUSED
//
// following return codes are used to indicate that the service is in the
// middle of getting to one of the above states:
// SERVICE_START_PENDING
// SERVICE_STOP_PENDING
// SERVICE_CONTINUE_PENDING
// SERVICE_PAUSE_PENDING
//
// sMachine:
// machine name, ie: \SERVER
// empty = local machine
//
//sService
// service name, ie: Alerter
//
function TFormConfig.ServiceGetStatus(sMachine, sService: string ): DWord;
var
  //service control
  //manager handle
  schm,
  //service handle
  schs: SC_Handle;
  //service status
  ss: TServiceStatus;
  //current service status
  dwStat : DWord;
begin
  dwStat := 0;
  //connect to the service
  //control manager
  schm := OpenSCManager(PChar(sMachine), Nil, SC_MANAGER_CONNECT);
  //if successful...
  if(schm > 0)then
  begin
  //open a handle to
  //the specified service
  schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS);
  //if successful...
  if(schs > 0)then
  begin
  //retrieve the current status
  //of the specified service
  if(QueryServiceStatus(schs, ss))then
  begin
  dwStat := ss.dwCurrentState;
  end;
  //close service handle
  CloseServiceHandle(schs);
  end;  // close service control
  // manager handle
  CloseServiceHandle(schm);
  end;  Result := dwStat;
end;{判断某服务是否安装,未安装返回true,已安装返回false}
function TFormConfig.ServiceUninstalled(sMachine, sService : string ) : boolean;
begin
  Result := 0 = ServiceGetStatus(sMachine, sService);
end;{判断某服务是否启动,启动返回true,未启动返回false}
function TFormConfig.ServiceRunning(sMachine, sService : string ) : boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService );
end;{判断某服务是否停止,停止返回true,未停止返回false}
function TFormConfig.ServiceStopped(sMachine, sService : string ) : boolean;
begin
  Result := SERVICE_STOPPED = ServiceGetStatus(sMachine, sService );
end;

解决方案 »

  1.   

    给你一个函数,百试百灵:function ServiceExists(const ServiceName: string): Boolean;
    var
      SCM, SCH: SC_Handle;
    begin
      Result := False;
      SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS)
      if SCM <> 0 then
      begin
        SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
        Result := ERROR_SERVICE_DOES_NOT_EXIST <> GetLastError;
        if SCH <> 0 then CloseServiceHandle(SCH);
        CloseServiceHandle(SCM);
      end;
    end;
      

  2.   

    我要是不引用WinSvc头文件的话,你觉得程序能运行吗