Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating Threads? {newbie}

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 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