Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please note I have only accepted this solution so that a full delphi code solution is accepted, all due thanks to Jk though for pointing me on the correct path.</p> <p>--</p> <p>Ok, I've been able to figure out how to use the answer by Jk and have come up with this solution in delphi.</p> <p>For reference, this is the link provided by Jk:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms684941(VS.85).aspx" rel="nofollow noreferrer">QueryServiceStatusEx</a></p> <p>My Solution:</p> <pre><code>unit Demo; interface uses Windows, Forms, SysUtils, StdCtrls, WinSvc, Controls, Classes; type //Form for basic demo usage TForm6 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); end; //Record defined for use as return buffer _SERVICE_STATUS_PROCESS = record dwServiceType: DWORD; dwCurrentState: DWORD; dwControlsAccepted: DWORD; dwWin32ExitCode: DWORD; dwServiceSpecificExitCode: DWORD; dwCheckPoint: DWORD; dwWaitHint: DWORD; dwProcessId: DWORD; dwServiceFlags: DWORD; end; //Function Prototype function QueryServiceStatusEx( SC_HANDLE: SC_Handle; SC_STATUS_TYPE: Cardinal; out lpBuffer: _SERVICE_STATUS_PROCESS; cbBufSize: DWORD; out pcbBytesNeeded: LPDWORD ): BOOL; stdcall; //internal setup function function GetPid(sService: String; sMachine: String = '') : Cardinal; var Form6: TForm6; implementation {$R *.dfm} const // windows api library advapi32 = 'advapi32.dll'; //define the api call function QueryServiceStatusEx; external advapi32 name 'QueryServiceStatusEx'; //for demo usage procedure TForm6.Button1Click(Sender: TObject); begin Memo1.Lines.Add(IntToStr(Integer(GetPid('Service')))) end; function GetPid(sService: String; sMachine: String = '') : Cardinal; var schm, schs: SC_Handle; SC_STATUS_TYPE: Cardinal; lpBuffer: _SERVICE_STATUS_PROCESS; cbBufSize: DWORD; pcbBytesNeeded: LPDWORD; begin //open the service manager (defined in WinSvc) schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT); //set the status type to SC_STATUS_PROCESS_INFO //this is currently the only value supported SC_STATUS_TYPE := $00000000; //set the buffer size to the size of the record cbBufSize := sizeof(_SERVICE_STATUS_PROCESS); if (schm&gt;0) then begin //grab the service handle schs := OpenService(schm, PChar(sService), SERVICE_QUERY_STATUS); if (schs&gt;0) then begin //call the function QueryServiceStatusEx( schs, SC_STATUS_TYPE, lpBuffer, cbBufSize, pcbBytesNeeded); CloseServiceHandle(schs); end; CloseServiceHandle(schm); end; Result := lpBuffer.dwProcessId; end; end. </code></pre> <p>Please note that not all external naming and other necessities are included.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload