Como saber o path (caminho) de um processo?

Delphi

15/07/2006

Olá amigos, já estou com essa dúvida a bastante tempo, inclusive já tinha postado ela (juntamente com outra dúvida) aqui anteriormente.

Alguém sabe como eu faço pra pegar o caminho de um processo? nem que tenha que reinventar a roda... eu preciso muito disso para finalizar um projeto.

Já tentei diversas formas mas não obtive sucesso...

Grato pela atenção ^^

Vlw... té mais...


Madcat

Madcat

Curtidas 0

Respostas

Ranyeryfip

Ranyeryfip

15/07/2006

Olá amigo, dah uma olhada no link abaixo, que acho que vai servir direitinho para o seu caso.

[url]http://www.activedelphi.com.br/modules.php?op=modload&name=News&file=article&sid=369&mode=thread&order=0&thold=0[/url]


GOSTEI 0
Martins

Martins

15/07/2006

Olá amigo, dah uma olhada no link abaixo, que acho que vai servir direitinho para o seu caso. [url]http://www.activedelphi.com.br/modules.php?op=modload&name=News&file=article&sid=369&mode=thread&order=0&thold=0[/url]


Esse link aqui é legal, o colega poderá aprender muito com o artigo.

Boa sorte

Valew RanyeryFip


GOSTEI 0
Madcat

Madcat

15/07/2006

Olá amigos obrigado aew pelo link, mas eu já tinha visto esse artigo, mas ele não me ajuda no que eu estou querendo (pelo menos eu acho que não), eu só quero saber como descobrir o path do processo, só isso :cry: ... o meu programa já lista os porcessos, faz o controle de processos abertos, e agora eu preciso fazer uma análise pra ´levantar alguns processos da minha lista padrão´, exemplo: se tivesse na minha lista de padrão winword.exe ele não poderia ficar ´desligado´, se estivesse o programa ia e executava ele.

Nesse caso eu poderia mandar varrer o HD e encontrar o nome do executavel e usar o comando winexec, mas não será muito aplicavel pois pode haver outro executavel com o msm nome e ser confundido na busca, sem contar que há alguns processos padrões do windows que tem o msm nome tb, fora que demora pra caramba tb até varrer o HD todo ´n´ vezes (n = Numero de processos). Se tivesse um jeito de extrair o path diretamente do processo (relacionado) iria ficar perfeito, mais eu procurei e nada até agora...

Peço ajuda de vcs para que eu possa terminar esse meu projeto,

Grato pela atenção ^^ vlw... té mais...


GOSTEI 0
Leitorbinario

Leitorbinario

15/07/2006

uses
PsAPI, TlHelp32;
// portions by Project Jedi www.delphi-jedi.org/
const
RsSystemIdleProcess = ´System Idle Process´;
RsSystemProcess = ´System Process´;

function IsWinXP: Boolean;
begin
Result := (Win32Platform = VER_PLATFORM_WIN32_NT) and
(Win32MajorVersion = 5) and (Win32MinorVersion = 1);
end;

function IsWin2k: Boolean;
begin
Result := (Win32MajorVersion >= 5) and
(Win32Platform = VER_PLATFORM_WIN32_NT);
end;

function IsWinNT4: Boolean;
begin
Result := Win32Platform = VER_PLATFORM_WIN32_NT;
Result := Result and (Win32MajorVersion = 4);
end;

function IsWin3X: Boolean;
begin
Result := Win32Platform = VER_PLATFORM_WIN32_NT;
Result := Result and (Win32MajorVersion = 3) and
((Win32MinorVersion = 1) or (Win32MinorVersion = 5) or
(Win32MinorVersion = 51));
end;

function RunningProcessesList(const List: TStrings; FullPath: Boolean): Boolean;

function ProcessFileName(PID: DWORD): string;
var
Handle: THandle;
begin
Result := ´´;
Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
if Handle <> 0 then
try
SetLength(Result, MAX_PATH);
if FullPath then
begin
if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
SetLength(Result, StrLen(PChar(Result)))
else
Result := ´´;
end
else
begin
if GetModuleBaseNameA(Handle, 0, PChar(Result), MAX_PATH) > 0 then
SetLength(Result, StrLen(PChar(Result)))
else
Result := ´´;
end;
finally
CloseHandle(Handle);
end;
end;

function BuildListTH: Boolean;
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
FileName: string;
begin
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
Result := (SnapProcHandle <> INVALID_HANDLE_VALUE);
if Result then
try
ProcEntry.dwSize := SizeOf(ProcEntry);
NextProc := Process32First(SnapProcHandle, ProcEntry);
while NextProc do
begin
if ProcEntry.th32ProcessID = 0 then
begin
// PID 0 is always the "System Idle Process" but this name cannot be
// retrieved from the system and has to be fabricated.
FileName := RsSystemIdleProcess;
end
else
begin
if IsWin2k or IsWinXP then
begin
FileName := ProcessFileName(ProcEntry.th32ProcessID);
if FileName = ´´ then
FileName := ProcEntry.szExeFile;
end
else
begin
FileName := ProcEntry.szExeFile;
if not FullPath then
FileName := ExtractFileName(FileName);
end;
end;
List.AddObject(FileName, Pointer(ProcEntry.th32ProcessID));
NextProc := Process32Next(SnapProcHandle, ProcEntry);
end;
finally
CloseHandle(SnapProcHandle);
end;
end;

function BuildListPS: Boolean;
var
PIDs: array [0..1024] of DWORD;
Needed: DWORD;
I: Integer;
FileName: string;
begin
Result := EnumProcesses(@PIDs, SizeOf(PIDs), Needed);
if Result then
begin
for I := 0 to (Needed div SizeOf(DWORD)) - 1 do
begin
case PIDs[i] of
0:
// PID 0 is always the "System Idle Process" but this name cannot be
// retrieved from the system and has to be fabricated.
FileName := RsSystemIdleProcess;
2:
// On NT 4 PID 2 is the "System Process" but this name cannot be
// retrieved from the system and has to be fabricated.
if IsWinNT4 then
FileName := RsSystemProcess
else
FileName := ProcessFileName(PIDs[i]);
8:
// On Win2K PID 8 is the "System Process" but this name cannot be
// retrieved from the system and has to be fabricated.
if IsWin2k or IsWinXP then
FileName := RsSystemProcess
else
FileName := ProcessFileName(PIDs[i]);
else
FileName := ProcessFileName(PIDs[i]);
end;
if FileName <> ´´ then
List.AddObject(FileName, Pointer(PIDs[i]));
end;
end;
end;
begin
if IsWin3X or IsWinNT4 then
Result := BuildListPS
else
Result := BuildListTH;
end;

function GetProcessNameFromWnd(Wnd: HWND): string;
var
List: TStringList;
PID: DWORD;
I: Integer;
begin
Result := ´´;
if IsWindow(Wnd) then
begin
PID := INVALID_HANDLE_VALUE;
GetWindowThreadProcessId(Wnd, @PID);
List := TStringList.Create;
try
if RunningProcessesList(List, True) then
begin
I := List.IndexOfObject(Pointer(PID));
if I > -1 then
Result := List[i];
end;
finally
List.Free;
end;
end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
RunningProcessesList(ListBox1.Items, True);
end;




Boa sorte. :shock:


GOSTEI 0
Madcat

Madcat

15/07/2006

vou dar uma analisada nesse codigo com calma... se mais alguem puder ajudar... ^^ vlw... té mais...


GOSTEI 0
POSTAR