Einzelnen Beitrag anzeigen

Benutzerbild von Harry M.
Harry M.

Registriert seit: 29. Okt 2004
Ort: Halle
462 Beiträge
 
#6

Re: QueryServiceConfig2 (gelöst)

  Alt 25. Okt 2006, 08:40
Delphi-Quellcode:
program Project2;

{$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.dllname 'QueryServiceConfig2W';

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

        function SysErrorMessage(ErrorCode: Integer): string;
var
  Len : Integer;
  Buffer : array[0..255] 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): WideString;
var
  sc : THandle;
  os : THandle;
  sd : Boolean;
  dwNeeded : DWORD;
  Buffer : Pointer;
begin
  result := '';
  dwNeeded := 0;
  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
        GetMem(Buffer, dwNeeded);
        sd := QueryServiceConfig2(os, 1, Buffer, dwNeeded, dwNeeded);
        if sd then
        begin
          result := PSERVICE_DESCRIPTION(Buffer)^.lpDescription;
        end
        else
          Writeln(SysErrorMessage(GetLastError));
      end;
    end
    else
      Writeln(SysErrorMessage(GetLastError));
  end
  else
    Writeln(SysErrorMessage(GetLastError));
end;

var
  description : WideString;

begin
  description := GetServiceDesciption('hal9000', 'ClipSrv');
  MessageBoxW(0, PWideChar(description), 'Dienstbeschreibung', 0);
end.
Danke Luckie
Harry
Gruß Harry
www.H-Soft.info
  Mit Zitat antworten Zitat