$theTitle=wp_title(" - ", false); if($theTitle != "") { ?>
About Virtualization, VDI, SBC, Application Compatibility and anything else I feel like
Sometimes you need to know the Process Id (PID) of a running service. Since Windows 2003 you can use the tasklist.exe tool with the /SVC switch. But how to do this programmatically?
The QueryServiceStatusEx API returns a SERVICE_STATUS_PROCESS structure that contains the PID.
The code is not very complicated:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | uses JwaWinSvc; function GetServicePid(const Servicename: String): DWORD; var hScm: THandle; hSvc: THandle; ssp: SERVICE_STATUS_PROCESS; dwSize: DWORD; begin hScm := OpenSCManager(nil, nil, SC_MANAGER_CONNECT); if hScm = 0 then Exit(0); try hSvc := OpenService(hScm, PChar(Servicename), SERVICE_QUERY_STATUS); if hSvc = 0 then Exit(0); try if not QueryServiceStatusEx(hSvc, SC_STATUS_PROCESS_INFO, @ssp, SizeOf(ssp), dwSize) then Exit(0); Result := ssp.dwProcessId; finally CloseServiceHandle(hSvc); end; finally CloseServiceHandle(hScm); end; end; |
Leave a reply