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

📄 u_svc.pas

📁 防火墙DELPHI代码 防火墙DELPHI代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:

            //
            // 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 stopped
  Result :=
    SERVICE_STOPPED =
      ss.dwCurrentState;
end;

function ChangeServiceStartType(
  sMachine,
  sService : String;
  dwStartType: DWORD ) : boolean;
var
  //
  // service control
  // manager handle
  schm,
  //
  // service handle
  schs   : SC_Handle;
begin
  Result :=False;
  // 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),
      // we want to
      // change the service
      SERVICE_CHANGE_CONFIG);

    // if successful...
    if(schs > 0)then
    begin
      if ChangeServiceConfig(
        schs,
        SERVICE_NO_CHANGE,
        dwStartType,
        SERVICE_NO_CHANGE,
        nil,
        nil,
        nil,
        nil,
        nil,
        nil,
        nil)then
        Result :=True;
      // close service handle
      CloseServiceHandle(schs);
    end;
    // close service control
    // manager handle
    CloseServiceHandle(schm);
  end;
end;

//-------------------------------------
// Get the service key name that is
// associated with a specified
// service's display name
// ie: 'Browser' is the key name for
//     'Computer Browser'
//
// sMachine:
//   machine name, ie: \\SERVER
//   empty = local machine
//
// sService
//   service display name,
//   ie: 'Computer Browser'
//
function ServiceGetKeyName(
  sMachine,
  sServiceDispName : string ) : string;
var
  //
  // service control
  // manager handle
  schm          : SC_Handle;

  //
  // max key name len
  nMaxNameLen   : Cardinal;

  //
  // temp. string
  psServiceName : PChar;
begin
  Result := '';

  // expect a service key
  // name shorter than 255
  // characters
  nMaxNameLen := 255;

  // connect to the service
  // control manager
  schm := OpenSCManager(
    PChar(sMachine),
    Nil,
    SC_MANAGER_CONNECT);

  // if successful...
  if(schm > 0)then
  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;

    // close service control
    // manager handle
    CloseServiceHandle(schm);
  end;
end;


//-------------------------------------
// Get the service display name that is
// associated with a specified
// service's display name
// ie: 'Computer Browser' is the
//     display name for 'Browser'
//
// sMachine:
//   machine name, ie: \\SERVER
//   empty = local machine
//
// sService
//   service key name,
//   ie: 'Browser'
//
function ServiceGetDisplayName(
  sMachine,
  sServiceKeyName : string ) : string;
var
  //
  // service control
  // manager handle
  schm          : SC_Handle;

  //
  // max display name len
  nMaxNameLen   : cardinal;

  //
  // temp. string
  psServiceName : PChar;
begin
  Result := '';

  // expect a service display
  // name shorter than 255
  // characters
  nMaxNameLen := 255;

  // connect to the service
  // control manager
  schm := OpenSCManager(
    PChar(sMachine),
    Nil,
    SC_MANAGER_CONNECT);

  // if successful...
  if(schm > 0)then
  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;

    // close service control
    // manager handle
    CloseServiceHandle(schm);
  end;
end;


//-------------------------------------
// 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;
  lv : TListView )
  : boolean;
const
  //
  // assume that the total number of
  // services is less than 4096.
  // increase if necessary
  cnMaxServices = 4096;

type
  TSvcA = array[0..cnMaxServices]
          of TEnumServiceStatus;
  PSvcA = ^TSvcA;
          
var
  //
  // temp. use
  j : integer;

  //
  // service control
  // manager handle
  schm          : SC_Handle;

  //
  // bytes needed for the
  // next buffer, if any
  nBytesNeeded,

  //
  // number of services
  nServices,

  //
  // pointer to the
  // next unread service entry
  nResumeHandle : DWord;

  //
  // service status array
  ssa : PSvcA;

  curListItem:TListItem;
  Buffer: PQueryServiceConfig;
  NeedBytes, ErrorCode : Cardinal;
  hService:SC_HANDLE;
  ss: TServiceStatus;
  SvcKeyName: String;
  path: string;
begin
  Result := false;

  // connect to the service
  // control manager
  schm := OpenSCManager(
    PChar(sMachine),
    Nil,
    SC_MANAGER_ALL_ACCESS);

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

    New(ssa);

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

    //
    // assume that our initial array
    // was large enough to hold all
    // entries. add code to enumerate
    // if necessary.
    //

    fileVer:=TGetVersionInfoFromFile.Create;
    fileVer.Init;

    for j := 0 to nServices-1 do
    begin
      Application.ProcessMessages;
      curListItem:=lv.Items.Add;
      curListItem.ImageIndex:=3;
      curListItem.Caption:=( StrPas(ssa^[j].lpDisplayName ) );
      SvcKeyName:=ServiceGetKeyName(sMachine, StrPas(ssa^[j].lpDisplayName ));
      hService:=OpenService(schm,Pchar(SvcKeyName),SERVICE_QUERY_CONFIG or SERVICE_QUERY_STATUS);
      if hService<>0 then
      begin
        if not QueryServiceConfig(hService,nil,0,NeedBytes) then
        begin
          ErrorCode := GetLastError;
          if ErrorCode = ERROR_INSUFFICIENT_BUFFER then
            begin                
              GetMem(Buffer,NeedBytes + 1);
              try
                if not QueryServiceConfig(hService,Buffer,NeedBytes,NeedBytes) then
                  curListItem.SubItems.Add('Can not query information!')
                else
                begin
                  curListItem.SubItems.Add(Buffer.lpBinaryPathName);

                  /////
                  path:=Buffer.lpBinaryPathName;
                  path:=trim(path);
                  if path<>'' then
                  begin
                    if path[1]='"' then begin path[1]:=' ';path:=trim(path);end;
                    if StrPos(Pchar(Path),'"')<>nil then
                      StrPos(Pchar(Path),'"')[0]:=#0
                    else if StrPos(Pchar(Path),'.exe')<>nil then
                      StrPos(Pchar(Path),'.exe')[4]:=#0
                    else if StrPos(Pchar(Path),' ')<>nil then
                    begin
                      StrPos(Pchar(Path),' ')[0]:=#0;
                      StrCat(Pchar(Path),'.exe');
                    end;
                    fileVer.fileName:=path;
                    if pos('Microsoft Corp',fileVer.getVersionSetting('LegalCopyright'))<=0 then
                    begin
                      curListItem.ImageIndex:=6;
                    end;
                  end;
                  /////

                  if QueryServiceStatus(hService,ss) then
                    if ss.dwCurrentState=SERVICE_PAUSED	then
                      curListItem.SubItems.Add('Paused')
                    else if ss.dwCurrentState=SERVICE_RUNNING	 then
                      curListItem.SubItems.Add('Running')
                    else
                      curListItem.SubItems.Add('Stopped')
                  else
                    curListItem.SubItems.Add('Unknown');
                  if Buffer.dwStartType=SERVICE_DISABLED then
                    curListItem.SubItems.Add('Disabled')
                  else if Buffer.dwStartType=SERVICE_DEMAND_START	then
                    curListItem.SubItems.Add('Manual')
                  else
                    curListItem.SubItems.Add('Automatic');
                end;
                curListItem.SubItems.Add(SvcKeyName);
              finally
                FreeMem(Buffer);
              end;
            end
            else
             curListItem.SubItems.Add('Can not get information!');
        end;
      end
      else
      begin
        curListItem.SubItems.Add('Can not open service!');
      end;
    end;{For}
    fileVer.Free;

    Result := true;

    Dispose(ssa);

    // close service control
    // manager handle
    CloseServiceHandle(schm);
  end;
end;


end.

⌨️ 快捷键说明

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