Como verificar se um processo esta rodando?
Excelente dica se da a possibilidade de testar se um processo está ou
não ativo no gerenciador de tarefas do Windows.
É necessário estar declarado TlHelp32, Windows e SysUtils na seção uses,
em versões unicode declare Winapi.TlHelp32, Winapi.Windows e
System.SysUtils.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| function ProcessExists(exeFileName: string): Boolean;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
begin
Result := True;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end; |
function ProcessExists(exeFileName: string): Boolean;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
begin
Result := True;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
Exemplo de uso:
Para testar o processo, basta passar o nome dele por parâmetro.
Se você quiser saber o nome de algum processo, você pode
olhar no gerenciador de tarefas, na aba processos.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| procedure TForm1.Button1Click(Sender: TObject);
begin
// Verificando se o bloco de notas está aberto.
if ProcessExists('notepad.exe') then
ShowMessage('O Processo está rodando!')
else
ShowMessage('O Processo não está rodando!');
// Verificando se a calculadora está aberta.
if ProcessExists('calc.exe') then
ShowMessage('O Processo está rodando!')
else
ShowMessage('O Processo não está rodando!');
// Testa se o nosso exemplo está rodando
if ProcessExists('Project1.exe') then
ShowMessage('O Processo está rodando!')
else
ShowMessage('O Processo não está rodando!');
end; |
procedure TForm1.Button1Click(Sender: TObject);
begin
// Verificando se o bloco de notas está aberto.
if ProcessExists('notepad.exe') then
ShowMessage('O Processo está rodando!')
else
ShowMessage('O Processo não está rodando!');
// Verificando se a calculadora está aberta.
if ProcessExists('calc.exe') then
ShowMessage('O Processo está rodando!')
else
ShowMessage('O Processo não está rodando!');
// Testa se o nosso exemplo está rodando
if ProcessExists('Project1.exe') then
ShowMessage('O Processo está rodando!')
else
ShowMessage('O Processo não está rodando!');
end;
A função roda perfeitamente tanto em 32 como em 64 bits.
-
InfusTec
-
20.235 views
- 0 comentários
- 3 de abril de 2015
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo