Como matar um processo do Windows?
Com este código podemos finalizar os processos pelo nosso programa
criado pelo Delphi.
Diferente de outros códigos que também finalizam o processo, este
é compatível com a compilação para 64 bits.
Necessário estar declarado PsAPI, Windows na seção uses,
em versões unicode declare Winapi.PsAPI, Winapi.Windows
Abaixo segue o código fonte:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| function TForm1.FindProcesses (aKey : String; aList : TList = nil) : Integer;
var
szProcessName : Array [0..1024] of Char;
ProcessName : String;
hProcess : Integer;
aProcesses : Array [0..1024] of DWORD;
cProcesses : DWORD;
cbNeeded : Cardinal;
i : UINT;
hMod : HMODULE;
begin
Result:= 0;
aKey:= lowercase(akey);
if not (EnumProcesses(@aProcesses, sizeof(aProcesses), cbNeeded)) then
exit;
// Calculate how many process identifiers were returned.
cProcesses := cbNeeded div sizeof(DWORD);
// Print the name and process identifier for each process.
for i:= 0 to cProcesses - 1 do
begin
szProcessName := 'unknown';
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
FALSE, aProcesses[i]);
// Get the process name.
if (hProcess <> 0) then
if (EnumProcessModules(hProcess, @hMod, sizeof(hMod),cbNeeded)) then
GetModuleBaseName (hProcess, hMod, szProcessName, sizeof(szProcessName));
ProcessName:= lowercase (szProcessName);
CloseHandle(hProcess);
if pos (aKey, ProcessName) <> 0 then
begin
result:=aProcesses[i];
if aList <> nil then
aList.add(Pointer (aProcesses[i]))
else
exit;
end;
end;
if aList <> nil then
Result:= aList.count
else
Result:=0;
end;
function TForm1.KillProcess (aProcessId : Cardinal) : boolean;
var
hProcess : integer;
begin
hProcess:= OpenProcess(PROCESS_ALL_ACCESS, TRUE, aProcessId);
Result:= false;
if (hProcess <>0 ) then
begin
Result:= TerminateProcess(hProcess, 0);
exit;
end;
end; |
function TForm1.FindProcesses (aKey : String; aList : TList = nil) : Integer;
var
szProcessName : Array [0..1024] of Char;
ProcessName : String;
hProcess : Integer;
aProcesses : Array [0..1024] of DWORD;
cProcesses : DWORD;
cbNeeded : Cardinal;
i : UINT;
hMod : HMODULE;
begin
Result:= 0;
aKey:= lowercase(akey);
if not (EnumProcesses(@aProcesses, sizeof(aProcesses), cbNeeded)) then
exit;
// Calculate how many process identifiers were returned.
cProcesses := cbNeeded div sizeof(DWORD);
// Print the name and process identifier for each process.
for i:= 0 to cProcesses - 1 do
begin
szProcessName := 'unknown';
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
FALSE, aProcesses[i]);
// Get the process name.
if (hProcess <> 0) then
if (EnumProcessModules(hProcess, @hMod, sizeof(hMod),cbNeeded)) then
GetModuleBaseName (hProcess, hMod, szProcessName, sizeof(szProcessName));
ProcessName:= lowercase (szProcessName);
CloseHandle(hProcess);
if pos (aKey, ProcessName) <> 0 then
begin
result:=aProcesses[i];
if aList <> nil then
aList.add(Pointer (aProcesses[i]))
else
exit;
end;
end;
if aList <> nil then
Result:= aList.count
else
Result:=0;
end;
function TForm1.KillProcess (aProcessId : Cardinal) : boolean;
var
hProcess : integer;
begin
hProcess:= OpenProcess(PROCESS_ALL_ACCESS, TRUE, aProcessId);
Result:= false;
if (hProcess <>0 ) then
begin
Result:= TerminateProcess(hProcess, 0);
exit;
end;
end;
Exemplo do uso:
1
2
3
4
| if not KillProcess(FindProcesses('notepad.exe')) then
ShowMessage('Processo não finalizado.')
else
ShowMessage('Processo finalizado com sucesso!.'); |
if not KillProcess(FindProcesses('notepad.exe')) then
ShowMessage('Processo não finalizado.')
else
ShowMessage('Processo finalizado com sucesso!.');
Fonte: http://cc.embarcadero.com/item/24320
Dúvidas ou sugestões deixe nos comentários do post.
-
Giovani Da Cruz
-
4.609 views
- 0 comentários
- 19 de junho de 2015
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo