Delphi如何获取系统服务(即Service服务程序)列表

procedure TDpModel.ServiceNames(Names: TStrings; DisplayNames: TStrings = nil;
  const Service_Type: integer = $30; const Computer: PChar = nil);
{
  返回系统服务列表,Names:用于接收返回的服务名称列表,
  DisplayName:用于接收返回的对应显示名称列表,
  uType:需要返回那些类型服务名称列表,可以为SERVICE_DRIVER,SERVICE_WIN32,SERVICE_ALL
}
type
  TEnumServices = array[0..0] of TEnumServiceStatus;  //windows API结构体
  PEnumServices = ^TEnumServices;  //声明类指针
var
  SCM: SC_Handle;
  Services: PEnumServices; //定义结构体
  Len: Cardinal; //无符号32位整数 ,取值0到4294967295范围。
  ServiceCount, ResumeHandle, i: Cardinal;
begin
  ResumeHandle := 0;

  //建立了一个连接到服务控制管理器的句柄,并打开指定的数据库。
  SCM := OpenSCManager(Computer, nil, SC_MANAGER_ALL_ACCESS);
  {
     Computer:计算机标示指针,如果该指针为NULL ,或者如果它指向一个空字符串,
      则连接到本地计算机上的服务控制管理器。
     nil:如果该指针为NULL ,默认打开ServicesActive数据库。
     SC_MANAGER_ALL_ACCESS:指定访问服务的权限。
  }
  Len := 0;
  ServiceCount := 0;
  Services := nil;
  try
    Names.BeginUpdate;
    //判断DisplayNames是否为空
    if Assigned(DisplayNames) then DisplayNames.BeginUpdate;

    if SCM <> 0 then
    begin
      //枚举当前系统服务,详见MSDN
      EnumServicesStatus(SCM, SERVICE_DRIVER or SERVICE_WIN32, SERVICE_STATE_ALL,
        Services[0], 0, Len, ServiceCount, ResumeHandle);
      GetMem(Services, Len);
      EnumServicesStatus(SCM, SERVICE_DRIVER or SERVICE_WIN32, SERVICE_STATE_ALL,
        Services[0], Len, Len, ServiceCount, ResumeHandle);

      //Tstring赋值
      Names.Add(IntToStr(ServiceCount));

      //循环遍历服务
      for i := 0 to ServiceCount - 1 do
      begin
        Names.Add(Services[i].lpServiceName);
        if Assigned(DisplayNames) then
        begin
          DisplayNames.Add(Services[i].lpDisplayName);
        end;

      end;
      FreeMem(Services);
    end;
  finally
    Names.EndUpdate;
    if Assigned(DisplayNames) then DisplayNames.EndUpdate;
    CloseServiceHandle(SCM);
  end;
end;

转载于:https://www.cnblogs.com/xieyunc/p/9126528.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值