Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating Threads? {newbie}

Posted on 2004-08-06
4
Medium Priority
?
306 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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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