View previous topic :: View next topic |
Author |
Message |
h4c0r-BG Master Cheater
Reputation: 0
Joined: 29 Nov 2006 Posts: 449 Location: The yogurt country
|
Posted: Thu Apr 24, 2008 1:33 pm Post subject: [Delphi] Kill specific thread in process? |
|
|
How can i "get the last thread" & "kill it" within a running process (delphi code)?
For example this can be done manualy like that:
And for another example i found in google some interesting articles but they don't give me enough information. If somebody is interested can look right here:
http://www.delphicorner.f9.co.uk/articles/db1.htm
http://www.delphicorner.f9.co.uk/articles/op1.htm
Thank you very much for your time even reading that post.
I will be glad if somebody can help me with some "example of code". Thank you and have a nice day!
_________________
|
|
Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Thu Apr 24, 2008 2:30 pm Post subject: |
|
|
don't forget to include (i mean add to the uses list) Tlhelp32;
edit: irwin, what about GetWindowThreadProcessId ?
edit2: you'll have to declare OpenThread from kernel32.dll yourself cause it's not added in windows.pas
Last edited by DeletedUser14087 on Thu Apr 24, 2008 2:50 pm; edited 1 time in total |
|
Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Thu Apr 24, 2008 3:09 pm Post subject: |
|
|
so irwin, the code (from what i've tried now using MSDN) will look like this ?
Code: | const THREAD_TERMINATE = $0001;
Function OpenThread(
dwDesiredAccess:DWORD;
bInheritHandle:BOOL; //True or False, as in bool AKA Boolean
dwThreadId:DWORD):THandle;
external kernel32 name 'OpenThread';
Function GetExitCodeThread(
hThread:THandle;
lpExitCode:DWORD):BOOL;
external kernel32 name 'GetExitCodeThread';
procedure TForm1.Button1Click(Sender: TObject);
var h:HWND;
OT,OP,pID:cardinal;
EC:BOOL;
begin
h:=FindWindow(nil,'Minesweeper');
if (h<>0) then
begin
GetWindowThreadProcessId(h,pID);
ShowMessage('GWTPID');
OP:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pID);
ShowMessage('OP');
OT:=OpenThread(THREAD_TERMINATE,FALSE,pID);
ShowMessage('OT');
EC:=GetExitCodeThread(OP,OT);
TerminateThread(OT,Cardinal(EC));
end;
end; |
Edit: Holy crap, i just noticed the topic thread, i was blind again lol
Kill a specific THREAD in a process, -.-
|
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Apr 24, 2008 3:44 pm Post subject: |
|
|
Nah, kill the last thread in a process.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
h4c0r-BG Master Cheater
Reputation: 0
Joined: 29 Nov 2006 Posts: 449 Location: The yogurt country
|
Posted: Fri Apr 25, 2008 12:30 pm Post subject: |
|
|
Kaspersky, really nice code thought this happens @ TerminateThread(OT,Cardinal(EC));...
Code: | procedure TForm1.Button1Click(Sender: TObject);
var h:HWND;
OT,OP,pID:cardinal;
EC:BOOL;
begin
h:=FindWindow(nil,'MyAppCaption');
if (h<>0) then
begin
GetWindowThreadProcessId(h,pID);
ShowMessage('GettingWTPID');
OP:=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pID);
ShowMessage('Opening the process....');
OT:=OpenThread(THREAD_TERMINATE,FALSE,pID);
ShowMessage('Opening Thread...');
EC:=GetExitCodeThread(OP,OT);
TerminateThread(OT,Cardinal(EC));
end;
end; |
I think it gives that error because i have not specified which thread to be terminated or i am wrong?
Can somebody give more hints what to do and what needs to be fixed?
Thank you all.
_________________
|
|
Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Fri Apr 25, 2008 2:31 pm Post subject: |
|
|
h4c0r-BG, the reason i added those ShowMessage() is to check if it successfully passes these stages, the error is due to TerminateThread().
don't make the 2nd param Cardinal(), just point it to a variable.
remove GetWindowThreadPID() and FindWindow
|
|
Back to top |
|
 |
|