Skip to content

Commit

Permalink
Win.Processes: +GetProcessModulesCount, small refact.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdp committed Aug 21, 2020
1 parent 072c178 commit cdea7d6
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions Base/JPL.Win.Processes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ interface
Windows, SysUtils, Classes, {$IFDEF FPC}JwaTlHelp32, JwaPsApi{$ELSE}TlHelp32, PsAPI{$ENDIF}, ShellAPI;


function GetProcessFileName(const pID: DWORD): string;
function GetProcessFileName(const ProcessID: DWORD): string;
function IsAppRunning(const FileName: string): Boolean;
function GetThreadFileName(const tID: DWORD): string;
procedure RunAsAdmin(hWnd: HWND; FileName, Parameters: string);
procedure RunAsAdmin(const hWnd: HWND; const FileName, Parameters: string);
function GetProcessModulesCount(const ProcessID: DWORD): integer;


{$ENDIF} // MSWINDOWS
Expand All @@ -25,7 +26,27 @@ implementation

{$IFDEF MSWINDOWS}

procedure RunAsAdmin(hWnd: HWND; FileName, Parameters: string);

function GetProcessModulesCount(const ProcessID: DWORD): integer;
var
h: THandle;
me32: TModuleEntry32;
begin
Result := 0;

h := CreateToolHelp32Snapshot(TH32CS_SNAPMODULE, ProcessID);
try
me32.dwSize := SizeOf(TModuleEntry32);
if Module32First(h, me32) then
repeat
if me32.th32ProcessID = ProcessID then Inc(Result);
until Integer(Module32Next(h, me32)) = 0;
finally
CloseHandle(h);
end;
end;

procedure RunAsAdmin(const hWnd: HWND; const FileName, Parameters: string);
var
{$IFDEF UNICODE}
sei: TShellExecuteInfoW;
Expand Down Expand Up @@ -105,7 +126,7 @@ function IsAppRunning(const FileName: string): Boolean;
pe32.dwSize := SizeOf(TProcessEntry32);
if integer(Process32First(snap, pe32)) <> 0 then
repeat
if UpperCase(FileName) = UpperCase(GetProcessFileName(pe32.th32ProcessID)) then
if AnsiUpperCase(FileName) = AnsiUpperCase(GetProcessFileName(pe32.th32ProcessID)) then
begin
Result := True;
Break;
Expand All @@ -114,7 +135,7 @@ function IsAppRunning(const FileName: string): Boolean;
CloseHandle(snap);
end;

function GetProcessFileName(const pID: DWORD): string;
function GetProcessFileName(const ProcessID: DWORD): string;
var
hProc: HWND;
buffer: array[0..1023] of Char;
Expand All @@ -125,7 +146,7 @@ function GetProcessFileName(const pID: DWORD): string;

if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 3) then
begin
hProc := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, pID);
hProc := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
if hProc <> 0 then
begin
FillChar(buffer{%H-}, SizeOf(buffer), 0);
Expand All @@ -140,7 +161,7 @@ function GetProcessFileName(const pID: DWORD): string;
pe32.dwSize := SizeOf(TProcessEntry32);
if integer(Process32First(snap, pe32)) <> 0 then
repeat
if integer(pID) = integer(pe32.th32ProcessID) then
if integer(ProcessID) = integer(pe32.th32ProcessID) then
begin
Result := pe32.szExeFile;
Break;
Expand Down

0 comments on commit cdea7d6

Please sign in to comment.