Hallo Christian Seehase...
habe das FreeMem gestern noch eingefügt, war mir selbst aufgefallen
Mit FreeMem er folgt eine Ungültige Zeigeroperation und ohne eine
AV 0x00000000
Hier nochmal der Code in seiner "aktuellen Fassung"
Delphi-Quellcode:
function GetServiceDescription(AServicename: PChar): String;
Const
SERVICE_CONFIG_DESCRIPTION = 1;
var
MgrHWND, SvcHWND: SC_Handle;
ServiceDesciption: PSERVICE_DESCRIPTION;
cbBufSize: DWORD;
pcbBytesNeeded: LPDWORD;
begin
MgrHWND := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if MgrHWND > 0 then begin
SvcHWND := WinSvc.OpenService(MgrHWND, AServicename, SERVICE_QUERY_CONFIG);
if SvcHWND > 0 then begin
cbBufSize := 512;
pcbBytesNeeded := nil;
GetMem(ServiceDesciption, cbBufSize);
if QueryServiceConfig2(SvcHWND, SERVICE_CONFIG_DESCRIPTION, @ServiceDesciption, cbBufSize, @pcbBytesNeeded) then
Result := ServiceDesciption.lpDescription // das kann ich problemslos compilieren jetzt
else
Result := SysErrorMessage(GetLastError);
FreeMem(ServiceDesciption);
end;
CloseServiceHandle(SvcHWND);
end;
CloseServiceHandle(MgrHWND);
end;
Und ein 2. Versuch der auch nicht recht glückte....
Delphi-Quellcode:
function QueryServiceConfig2A(hService: SC_HANDLE; dwInfoLevel: DWORD; lpBuffer: PBYTE;
cbBufSize: DWORD; pcbBytesNeeded: LPDWORD): LongBool; stdcall;
type
TServiceDescription = function(hService: SC_HANDLE; dwInfoLevel: DWORD; lpBuffer: PBYTE;
cbBufSize: DWORD; pcbBytesNeeded: LPDWORD): LongBool; stdcall;
var
LLibHandle: THandle;
LPtr: Pointer;
ServiceDesciption: TServiceDescription;
begin
LLibHandle:=LoadLibrary('advapi32.dll');
LPtr := GetProcAddress(LLibHandle,'QueryServiceConfig2A');
if LPtr <> nil then begin
ServiceDesciption := LPtr;
Result := ServiceDesciption(hService, dwInfoLevel, lpBuffer, cbBufSize, pcbBytesNeeded) // hier stimmt was net :(
end else
Result := False;
end;
function GetServiceDesciption2(AServicename: PChar): String;
Const
SERVICE_CONFIG_DESCRIPTION = 1;
var
MgrHWND, SvcHWND: SC_Handle;
ServiceDesciption: PSERVICE_DESCRIPTION;
cbBufSize: DWORD;
pcbBytesNeeded: LPDWORD;
begin
MgrHWND := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if MgrHWND > 0 then begin
SvcHWND := WinSvc.OpenService(MgrHWND, AServicename, SERVICE_QUERY_CONFIG);
if SvcHWND > 0 then begin
cbBufSize := 1024 * 8;
pcbBytesNeeded := nil;
GetMem(ServiceDesciption, cbBufSize);
if QueryServiceConfig2A(SvcHWND, SERVICE_CONFIG_DESCRIPTION, @ServiceDesciption, cbBufSize, @pcbBytesNeeded) then
Result := ServiceDesciption.lpDescription
else
Result := SysErrorMessage( GetLastError );
FreeMem(ServiceDesciption);
end;
end;
CloseServiceHandle(MgrHWND);
CloseServiceHandle(SvcHWND);
end;