unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TlHelp32, PsAPI;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetPID(AppName: String): DWORD;
var
Snapshot: THandle;
ProcessEntry : TProcessEntry32;
begin
Result := 0;
AppName := UpperCase(AppName);
Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 );
If Snapshot <> 0 Then
try
ProcessEntry.dwSize := Sizeof(ProcessEntry);
if Process32First(Snapshot, ProcessEntry) then
repeat
if Pos(AppName, UpperCase(ExtractFilename(StrPas(ProcessEntry.szExeFile)))) > 0 then
begin
Result:= ProcessEntry.th32ProcessID;
Break;
end;
until not Process32Next(Snapshot, ProcessEntry);
finally
CloseHandle(Snapshot);
end;
end;
function GetPathFromPID(const PID: cardinal): string;
var
hProcess: THandle;
Path: Array[0..MAX_PATH - 1] of char;
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, PID);
if hProcess <> 0 then
try
if GetModuleFileNameEx(hProcess, 0, Path, SizeOf(Path)) <> 0 then
Result := Path;
finally
CloseHandle(hProcess)
end
else
raise Exception.Create('El Programa No Se Encuentra Activo en Memoria');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
PID : THandle;
begin
PID := GetPID('Notepad.exe');
ShowMessage(Format('El Path del Programa en Disco es: %s',[GetPathFromPID(PID)]));
end;
end.