Get Service Executable Path

posted 18 Sep 2010, 14:26 by Delphi Basics   [ updated 18 Sep 2010, 14:34 ]
This function returns the executable path of a specified service. 

strMachine: 
The name of the target computer. If the pointer is NULL or points to an empty string, the function connects to the service control manager on the local computer. 
strServiceName: 
The name of the service to which the path is required. 

uses
  WinSvc;

function GetServiceExecutablePath(strMachine: string; strServiceName: string): String;
var
  hSCManager,hSCService: SC_Handle;
  lpServiceConfig: PQueryServiceConfigA;
  nSize, nBytesNeeded: DWord;
begin
  Result := '';
  hSCManager := OpenSCManager(PChar(strMachine), nil, SC_MANAGER_CONNECT);
  if (hSCManager > 0) then
  begin
    hSCService := OpenService(hSCManager, PChar(strServiceName), SERVICE_QUERY_CONFIG);
    if (hSCService > 0) then
    begin
      QueryServiceConfig(hSCService, nil, 0, nSize);
      lpServiceConfig := AllocMem(nSize);
      try
        if not QueryServiceConfig(
          hSCService, lpServiceConfig, nSize, nBytesNeeded) Then Exit;
          Result := lpServiceConfig^.lpBinaryPathName;
      finally
        Dispose(lpServiceConfig);
      end;
      CloseServiceHandle(hSCService);
    end;
  end;
end;

Attached is the source code to an application which retrieve's the executable path for a specified service. 

Screen: 

Only Delphi source code is included in the archive. 
ċ
GetServiceExecutablePath.rar
(2k)
Delphi Basics,
18 Sep 2010, 14:33
Comments