function ServiceStart(sMachine, sService : string ) : boolean;
  var
    schm, schs : SC_Handle;  // service control manager handle & service handle
    ss : TServiceStatus;  // service status
    psTemp : PChar; // temp char pointer
    dwChkP : DWord;   // check point
  begin
    ss.dwCurrentState := 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
      // we want to start the service and query service status
      schs := OpenService(schm, PChar(sService), SERVICE_START or SERVICE_QUERY_STATUS);
      // if successful...
      if(schs > 0)then begin
        psTemp := Nil;
        if(StartService(schs, 0, psTemp)) then begin
          // check status
          if (QueryServiceStatus(schs, ss))then begin
            while (SERVICE_RUNNING <> ss.dwCurrentState)do begin
              //
              // dwCheckPoint contains a value that the service increments periodically
              // to report its progress during a lengthy operation.
              // save current value
              dwChkP := ss.dwCheckPoint;
              // wait a bit before checking status again
              //
              // dwWaitHint is the estimated amount of time
              // the calling program should wait before calling
              // QueryServiceStatus() again
              //
              // idle events should be
              // handled here...
              //
              Sleep(ss.dwWaitHint);
              if (not QueryServiceStatus(schs, ss))then begin
                // couldn't check status
                // break from the loop
                break;
              end;
              if (ss.dwCheckPoint < dwChkP) then begin
                // QueryServiceStatus didn't increment dwCheckPoint as it should have.
                // avoid an infinite loop by breaking
                break;
              end;
            end;
          end;
        end;
        // close service handle
        CloseServiceHandle(schs);
      end;
      // close service control manager handle
      CloseServiceHandle(schm);
  end;
  // return TRUE if the service status is running
  Result := SERVICE_RUNNING = ss.dwCurrentState;
end;

 

下载地址:https://www.123pan.com/ps/4s8bVv-XDjK3.html

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。