Delphi获取系统服务描述信息

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.


评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭