Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6579
  • Last Modified:

[Delphi] How to terminate thread

Can somebody tell me how to terminate api thread (thread in process).
I have ThreaID and ProcessID but how to terminate  the thread ?


GETprocessID :
 
function GetProcessID(ProcessName : string ) : DWORD ;
var
  Handle:tHandle;
  Process:tProcessEntry32;
  GotProcess:Boolean;
begin
  Handle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL,0) ;
  Process.dwSize:=SizeOf(Process);
  GotProcess := Process32First(Handle,Process);
{$B-}
  if GotProcess and (Process.szExeFile<>ProcessName) then
      repeat
        GotProcess := Process32Next(Handle,Process);
      until (not GotProcess) or (Process.szExeFile=ProcessName);
{$B+}
  if GotProcess then Result := Process.th32ProcessID
    else Result := 0;
        CloseHandle(Handle);
end;
 
 
GETthreadID :(I have a list of them and select that i want to kill)
 
  index   := listBox1.ItemIndex;
 ThreadID := Cardinal(listBox1.Items[index]);
 
 
and now how to kill thread >?
 
 
  ProcessID := GetProcessID( 'process.exe' ) ;
    if ProcessID > 0 then
   here code for kill the tread
 
p.p. I need a simple code, because I can not do it alone
(and sorry for my bed english)

Open in new window

0
beBoss
Asked:
beBoss
  • 2
3 Solutions
 
TuebloCommented:
Hello beBoss,

I think you need to modify the repeat loop between {$B-} and {$B+}. The until command stops the loop as soon as it gets the first thread belonging to the given process. You need to put a counter there in order to get the right thread to terminate. I think it should look something about like that:

function GetThreadID(ProcessID : DWORD; threadNumber: DWORD ) : DWORD ;
:
    counter := 0;
    repeat
     GotThread := Thread32Next(Handle,ThreadEntry);
     if ( ThreadEntry.th32OwnerProcessID = ProcessID ) then
      INC(counter);
    until (not GotThread) or ( counter > threadNumber ) ;
:
end;

Giving 0 as thread number will return you the main thread of the process.

Regards,
Tueblo
0
 
venky011097Commented:
h'lo dear,
first u get the  thread id andthen based on the id u can terminate the process.
  step1: find the id for the current process.
  step2: which thread process working currenty
 step3: finds the current thread id and then kill using terminate or abort the thread
   example.  thread(1).terminiated:= true;
GETprocessID :
 
function GetProcessID(ProcessName : string ) : DWORD ;
var
  Handle:tHandle;
  Process:tProcessEntry32;
  GotProcess:Boolean;
begin
  Handle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL,0) ;
  Process.dwSize:=SizeOf(Process);
  GotProcess := Process32First(Handle,Process);
{$B-}
  if GotProcess and (Process.szExeFile<>ProcessName) then
      repeat
        GotProcess := Process32Next(Handle,Process);
      until (not GotProcess) or (Process.szExeFile=ProcessName);
{$B+}
  if GotProcess then Result := Process.th32ProcessID
    else Result := 0;
        CloseHandle(Handle);
end;
 
 
GETthreadID :(I have a list of them and select that i want to kill)
 
  index   := listBox1.ItemIndex;
 ThreadID := Cardinal(listBox1.Items[index]);
 
 
and now how to kill thread >?
 
 
  ProcessID := GetProcessID( 'process.exe' ) ;
    if ProcessID > 0 then
   here code for kill the tread

 
0
 
venky011097Commented:
dear this another solution which may help u get better solution.
 Function EnumThreadProc( wnd: HWND; Var appHwnd: HWND ): LongBool;
 stdcall;
Var  buf: array [0..128] of Char;
Begin  
Result := LongBool(1);
 if GetClassname( wnd, buf, sizeof(buf)) > 0 then        If StrComp( buf, 'TApplication' ) = 0 Then Begin        
 appHwnd := Wnd;        
 Result := False;        
End; { If }End;Function FindApplicationWindow( forThreadID: DWORD ): HWND;Begin  Result := 0;  EnumThreadWindows( forThreadID, @EnumThreadProc, lparam(@result));
End;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now