• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi获取系统服务描述信息

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
program Project1;

{$APPTYPE CONSOLE}

uses
  Windows,
  WinSvc;

type
  SERVICE_DESCRIPTION = packed record
    lpDescription: PWChar;
  end;
  PSERVICE_DESCRIPTION = ^SERVICE_DESCRIPTION;

function QueryServiceConfig2(hService: THandle; dwInfoLevel: DWORD; lpBuffer: Pointer; cbBufSize: DWORD; var pcbBytesNeeded: DWORD): LongBool; stdcall; external 'advapi32.dll' name 'QueryServiceConfig2W';

////////////////////////////////////////////////////////////////////////////////
// Procedure : SysErrorMessage
// Comment : Returns last error as formated string

function SysErrorMessage(ErrorCode: Integer): string;
var
  Len: Integer;
  Buffer: array[0..55]of Char;
begin
  Len := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ARGUMENT_ARRAY,
                       nil, ErrorCode, 0, Buffer, SizeOf(Buffer), nil);
  while (Len > 0)and(Buffer[Len-1] in [#0..#32, '.']) do Dec(Len);
  SetString(Result, Buffer, Len);
end;

function GetServiceDesciption(Computer, Servicename: PWChar; var Description: WideString): Boolean;
var
  sc: THandle;
  os: THandle;
  sd: Boolean;
  dwNeeded: DWORD;
  Buffer: Pointer;
begin
  dwNeeded := 0;
  Buffer := nil;
  sc := OpenSCManagerW(Computer, nil, SC_MANAGER_CONNECT);
  if sc <> 0 then
  begin
    os := OpenServiceW(sc, Servicename, SERVICE_QUERY_CONFIG);
    if os <> 0 then
    begin
      sd := QueryServiceConfig2(os, 1, nil, 0, dwNeeded);
      if (not sd) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
      begin
        try
          GetMem(Buffer, dwNeeded);
          result := QueryServiceConfig2(os, 1, Buffer, dwNeeded, dwNeeded);
          if result then
          begin
            Description := PSERVICE_DESCRIPTION(Buffer)^.lpDescription;
          end;
        finally
          FreeMem(Buffer, dwNeeded);
        end;
      end
      else
        result := False;
    end
    else
      result := False;
  end
  else
    result := False;
end;

var
  description : WideString;

begin
  if not GetServiceDesciption('PC-201212071904', 'AdobeFlashPlayerUpdateSvc', description) then
  begin
    Writeln(SysErrorMessage(GetLastError));
    Readln;
  end
  else
    MessageBoxW(0, PWideChar(description), 'Dienstbeschreibung', 0);
    
end.

http://www.lsworks.net/article/97.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi函数参数说明发布时间:2022-07-18
下一篇:
delphiXE的字符串处理发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap