Solved

Creating Threads? {newbie}

Posted on 2004-08-06
4
303 Views
Last Modified: 2010-04-05
Hello experts, i am making a program that will moniter users time on kazaa, but i want to improve this a little, so i want to create a thread that kills kazaalite.kpp process and another thread that kills taskmanager, i am making this as a console application.  help would be greatly appreciated.. this is what i have and i cant get it to work.

uses
  SysUtils,WINDOWS,TLHELP32,REGISTRY,Messages;

Function HWNDFromPath(FicheroExe:string):hwnd;
type
    TPrograma = record
    ElPath : string;
    ElHWND : hwnd;
end;
     PPrograma=^TPrograma;
var
     Programa: TPrograma;

Function SacaExe(MangoW:HWND):string;
var
   Datos    :TProcessEntry32;
   hID      :DWord;
   Snap    : Integer;
begin
       GetWindowThreadProcessId(MangoW,@hID);
       Snap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
       try
         Datos.dwSize:=SizeOf(Datos);
         if(Process32First(Snap,Datos))then
         begin
           repeat
             if Datos.th32ProcessID=hID then
             begin
               Result:=StrPas(Datos.szExeFile);
               Break;
             end;
           until not(Process32Next(Snap,Datos));
         end;
       finally
         Windows.CloseHandle(Snap);
     end;
end;

function ObtieneVentanas( Mango  : HWND;
                              UnPath : PPrograma): Boolean; stdcall;
begin
      Result := True;
      if SacaExe(Mango)=UpperCase( UnPath^.ElPath )then
      begin
        UnPath^.ElHWND:=Mango;
        Result:=FALSE;  {Stop enumeration}
      end;
end;
 begin {From HWNDFromPath}
     with Programa do
     begin
       ElPath:=FicheroExe;
       ElHWND:=0;
     end;
     EnumWindows( @ObtieneVentanas, LParam(@Programa) );
     Result:=Programa.ElHWND;
end;  {From HWNDFromPath}

function killtask1:integer;
label bucle1;
var Xx : Thandle;
begin
bucle1:
   Xx:=FindWindow('Windows Task Manager',nil);
       if Xx <> 0 Then
       begin
       SendMessage(Xx,WM_CLOSE,0,0);
       end;
Sleep(10000);
goto bucle1;
end;

function killtask2:integer;
label bucle2;
var Xx : Thandle;
begin
bucle2:
  Xx:=FindWindow(Pchar(HWNDFromPath('C:\WINDOWS\SYSTEM32\taskmgr.exe')),nil);
      if Xx <> 0 Then SendMessage(Xx,WM_CLOSE,0,0);
Sleep(200);
goto bucle2;
end;

function killtask3:integer;
label bucle2;
var Xx : Thandle;
begin
bucle2:
  Xx:=FindWindow(Pchar(HWNDFromPath('C:\Program Files\Kazaa Lite K++\kpp.exe')),nil);
      if Xx <> 0 Then SendMessage(Xx,WM_CLOSE,0,0);
Sleep(200);
goto bucle2;
end;

VAR
DW:dword;

begin
   CreateThread(nil,0,@killtask1,nil,0,DW);
   CreateThread(nil,0,@killtask2,nil,0,DW);
   CreateThread(nil,0,@killtask3,nil,0,DW);
end.
0
Comment
Question by:herd_bone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
el_rama86 earned 125 total points
ID: 11737754
To close another application you can use the PostMessage API declared in the Windows unit to send a WM_CLOSE message to its main form. This function requires the handle of the window to which you want to post the message. You can get this handle for example using the FindWindow API, also declared in the Windows unit.

Here is a source code example:

uses Windows, Messages;

procedure TForm1.Button1Click(Sender: TObject);
var
  h: HWND;
begin
  h := FindWindow(nil, 'Main Form Caption');
  if h <> 0 then PostMessage(h, WM_CLOSE, 0, 0);
end;
Instead of 'Main Form Caption' you should write the title (caption) of the main form of the application you want to close. Be sure this name is unique or you might end up closing another application (for example it wouldn't be good a name like 'Form1').

(http://www.latiumsoftware.com/en/delphi/00034.php)

If that doesn't works for u, then u can try:

To programmatically close another application, send to the application a WM_QUIT message. "Window caption" is the caption of the window that you are sending the message to.
Usage:
if not KillApp('Window caption') then ShowMessage('App not closed') ;

~~~~~~~~~~~~~~~~~~~~~~~~~
function KillApp(const sCapt: PChar) : boolean;
  var AppHandle:THandle;
begin
  AppHandle:=FindWindow(Nil, sCapt) ;
  Result:=PostMessage(AppHandle, WM_QUIT, 0, 0) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

(http://delphi.about.com/cs/adptips2000/a/bltip0100_2.htm)

Also:

http://help.madshi.net/Processes.htm

                       Let me know it that solved the problem. Good Luck, i2ahma.-
0
 

Expert Comment

by:el_rama86
ID: 11737842
Me parece que por las variables y demás, conocés el español mejor que el inglés... te recomiendo que visites también las siguientes páginas web:

www.clubdelphi.com
http://www.latiumsoftware.com/es/delphi/00034.php
http://www.elrinconcito.com/delphi/trucos/Delphi43.htm
http://www.q3.nu/trucomania/indicetrucos.cgi?esp

Ojalá te sirva de algo, Un abrazo y buena suerte. i2ahma.-
0
 

Author Comment

by:herd_bone
ID: 11741722
Thanks, could you look over my code and see if i'm creating seperate threads the right way.
0
 

Author Comment

by:herd_bone
ID: 11745460
nevermind i got it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi parse string to params 3 178
How to build JSON File in Delphi 6 3 95
Delphi and Access based Enumeration 9 90
Firemonkey Queue list 1 51
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question