Solved

Creating Threads? {newbie}

Posted on 2004-08-06
4
301 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Comparing dates in Delphi. Greater than/ Less than 3 148
Help on project with Soap 10 53
Strange behavior when a form is closed 6 57
Delphi problems to abort a printjob 9 17
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now