Solved

Creating Threads? {newbie}

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
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 we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

632 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