Solved

Taskbar buttons

Posted on 2004-05-02
32
330 Views
Last Modified: 2010-04-04
I want to list apps that have Taskbar Buttons, I know it should be unowned apps.
But...
how do I get a message when an app loads (my app is the taskbar), and how do I check if its owned?
0
Comment
Question by:The_SnifF
[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
  • 12
  • 10
  • 6
  • +3
32 Comments
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 125 total points
ID: 10970735
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function EnumWindowsFunc (Handle: THandle; List: TStringList) : Boolean ; stdcall;
var
  Caption: array[0..256] of Char;
begin
  if (IsWindowVisible(Handle)) and (GetParent(Handle) = 0) then
    if GetWindowText(Handle, Caption, SizeOf(Caption)-1) <> 0 then
      begin
        List.Add(Caption);
      end;
  Result :=True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Clear;
  EnumWindows(@EnumWindowsFunc, LParam(ListBox1.Items));
end;

end.
0
 

Author Comment

by:The_SnifF
ID: 10970820
Thats great, but I want to be informed when an app loads.
that would be terrific :)
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10970836
you could add a timer and check old list and new list at intervals.
0
Independent Software Vendors: 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!

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10970838

   Put timer on the form and execute this in OnTimer event
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 10970849
Download Cool Tray Icon from here http://subsimple.com/delphi.asp. It's really a great component(which i personally prefer..) which supports animated icons too.

Cheers,

Oktay
0
 

Author Comment

by:The_SnifF
ID: 10970918
Hmm... knowing when an app is created is kinda important, so i won't habe to redrew it with the timer.
I'll use it, as last resort, but if I can find somthing better I'll use it :)
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 10970991
Ohh i'm so sorry about my comment. Maybe i didn't read your question completely and i thought that you are asking for a component or code which you can use for to create tray icons :)))

I'm sorry again..

Cheers,

Oktay
0
 

Author Comment

by:The_SnifF
ID: 10971082
np, BTW i'm working on a small shell for windows :)
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10971571

  I don't know another way to monitor the opened application except this - enumerating all windows...
0
 

Author Comment

by:The_SnifF
ID: 10971584
It helped, but I don't want to make it with a timer.
I heard smth about system wide hooks... Help would be appreciated :)
0
 

Expert Comment

by:vinifreis
ID: 10972546
I think you can do this using hooks WH_GETMESSAGE and wait for messages WM_CREATE..
I'll do an simple example and post here..;-)
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10975085
If ur Working on a shell application ..

use shell Hook ...

here is the link for details

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks.asp
0
 

Author Comment

by:The_SnifF
ID: 10978341
If using global Hook and a DLL, how do i send a message to my app?
or how I can have my app's Handle for use in the DLL?
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10978426
Here is an Example
'*****************************
library khook;
uses
  Windows,classes,
  Messages;

procedure SetHook; export; forward;
procedure UnSetHook; export; forward;
procedure SetMainHandle(Handle: HWND); export; forward;

exports
 SetHook ,
 UnSetHook ,
 SetMainHandle ;
const
   mGet_message = WM_user+6666;
   mSet_Focus   = WM_user+6667;
type
 PHookRec = ^THookRec;
 THookRec = record
   MainWindow: HWND;
   HookID: HHOOK;
 end;

var
 rHookRec: PHookRec = nil;
 hMapObject: THandle = 0;

procedure SetMainHandle(Handle: HWND);
begin
 rHookRec^.MainWindow := Handle;
end;

// WndProcRet hook proc
function WNDPROCRET_HookProc(nCode: Integer; iWPARAM: INTEGER; iLPARAM: INTEGER): integer; stdcall;
begin
 result := 0;
 if (nCode >= 0) And (nCode=HC_ACTION) And ((iLPARAM and (1 shl 31)) <> 0) then begin
   SendMessage(rHookRec^.MainWindow, mGet_message, iWPARAM,iLPARAM);
 end else
   Result:=CallNextHookEx(rHookRec^.HookID, nCode, iWPARAM,iLPARAM);
end;


procedure SetHook;
begin
 rHookRec^.HookID := SetWindowsHookEx(WH_KEYBOARD, @WNDPROCRET_HookProc, hInstance, 0);
end;

procedure UnSetHook;
begin
 UnHookWindowsHookEx(rHookRec^.HookID);
end;

procedure EntryPointProc(Reason: Integer);
begin

 case reason of
   DLL_PROCESS_ATTACH:
     begin
       hMapObject := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(rHookRec^), 'Keyb_Hook');
       rHookRec := MapViewOfFile(hMapObject, FILE_MAP_WRITE, 0, 0, sizeof (rHookRec^));
       sethook;
     end;

   DLL_PROCESS_DETACH:
     begin
       try
         unsethook;
         UnMapViewOfFile(rHookRec);
         CloseHandle(hMapObject);
      except
       end;
     end;

   DLL_THREAD_ATTACH:
     begin
     end;
   DLL_THREAD_DETACH:
     begin
     end;
 end;
end;

begin
 DllProc := @EntryPointProc;
 EntryPointProc(DLL_PROCESS_ATTACH);
end.

'*** End of Dll
**********************
'unit.pas
unit Unit1;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
const
   mGet_message = WM_user+6666;
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
     Procedure GetHook(var msg:TMessage); message mget_message;
    { Private declarations }
  public
    { Public declarations }
  end;
                                       
var
  Form1: TForm1;                                          

  mproc : procedure;
  mlib  : Cardinal;
  capt  : pAnsiChar;
  tmps  : string;
  mhnd  : cardinal;

procedure SetMainHandle(Handle: HWND);external 'khook.dll';
procedure SetHook;external 'khook.dll';
procedure UnSetHook;external 'khook.dll';
implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.GetHook(var msg: TMessage);
 var
    added : boolean;
begin
   added :=false;
   if (mhnd=msg.LParam)then
      begin
        tmps:=tmps+chr(msg.wParamlo);
        added:=true;
      end;
   if (msg.wParamlo <> 13) then
      begin
        tmps:=tmps+chr(msg.wParamlo);
        added:=true;
      end;
   if not added then
    begin
     Memo1.Lines.add('['+string(capt)+']');
     memo1.Lines.add(tmps);
     tmps:='';
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  mhnd := 0;
//  SetHook;
  SetMainHandle(Handle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  UnSetHook;
end;

end.


'End of unit1.pas



0
 

Author Comment

by:The_SnifF
ID: 10978997
GetHook never gets called
0
 

Author Comment

by:The_SnifF
ID: 10980701
Hmm, I ment it never gets the message though it is sent.
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10981671
its a working example
 that i am using as a keylogger
0
 

Author Comment

by:The_SnifF
ID: 10987007
But the app never gets the message, I have no idea why.
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10987075
btw... this gettes the key strokes
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10987077

  Because here SetHook is in comment

procedure TForm1.Button1Click(Sender: TObject);
begin
  mhnd := 0;
//  SetHook;
  SetMainHandle(Handle);
end;

0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10987121
Sethook proc is in Dll's entry..

when ever dll loaded.. sethook proc executed

And i ll copy paste the all source here.. hold on...
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10987315
first
Click Hook buttom to set the window handle of the application


then  if u wanna see keystrokes.. u must hit enter....


when u press enter.. chars  u did write ll be added in to the memo
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10987359

   non_zero, in the code you supplied, note that SetHook is in remark

procedure TForm1.Button1Click(Sender: TObject);
begin
  mhnd := 0;
//  SetHook;
  SetMainHandle(Handle);
end;
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10987390
yeah i know.. same in my application ..

But as u see

***
procedure EntryPointProc(Reason: Integer);
begin

 case reason of
   DLL_PROCESS_ATTACH:
     begin
       hMapObject := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(rHookRec^), 'Keyb_Hook');
       rHookRec := MapViewOfFile(hMapObject, FILE_MAP_WRITE, 0, 0, sizeof (rHookRec^));
       sethook;  // its called here when u load dll  with the code below
     end;
.
.
.
.

// Calling code of the Sethook

begin
 DllProc := @EntryPointProc;
 EntryPointProc(DLL_PROCESS_ATTACH);  // <-- this line calls the entrpoint proc.. and execute the sethook proc rite?
end.
0
 

Author Comment

by:The_SnifF
ID: 10987932
The thing is the app never gets GetHook called!
The dll gets the hook, sends a message, but the app never gets it :x
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10988035

Pls its working..

Do not tell that its not getting or something like that

copy paste the code and try pls..

other files ( unit1.dfm and testhook.dpr)
//******************  save this part as testhook.dpr
program testhook;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
//****************

//******************  save this as unit1.dfm
object Form1: TForm1
  Left = 504
  Top = 300
  Width = 358
  Height = 424
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 0
    Top = 8
    Width = 337
    Height = 297
    Lines.Strings = (
      'Memo1')
    TabOrder = 0
  end
  object Button1: TButton
    Left = 32
    Top = 320
    Width = 75
    Height = 25
    Caption = 'hook'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 32
    Top = 352
    Width = 75
    Height = 25
    Caption = 'Un Hook'
    TabOrder = 2
    OnClick = Button2Click
  end
end
//********************



save all code in a directory
and try it...

Press Hook on the main form ... ( dont delete //  before sethook; )

open notepad

type some chars
and press enter... u ll see ur line is in the memo field

( I ve just copy paste the code's that i send u above  and try it.. and again it works fine)


0
 

Author Comment

by:The_SnifF
ID: 10989451
But I don't want it to log keys, I wanted to know when an app is created.
And i'll get to it, but when setting a breakpoint inside GetHook, it never stops there.
ofcourse i've changed it a bit, but the hook itself is still the same.
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10993278
Ok..
Here is a code for Hooking new Window Creation ..
Change the units with this one.. Works fine and it gives u the handle of the new windows

//************** Main Unit ..
unit Unit1;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
const
   mGet_message = WM_user+6666;
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
     Procedure GetHook(var msg:TMessage); message mget_message;
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;

  mproc : procedure;
  mlib  : Cardinal;
  capt  : pAnsiChar;
  tmps  : string;
  mhnd  : cardinal;

procedure SetMainHandle(Handle: HWND);external 'khook.dll';
procedure SetHook;external 'khook.dll';
procedure UnSetHook;external 'khook.dll';
implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.GetHook(var msg: TMessage);
begin
      memo1.Lines.Add(' New Window Handle : '+inttostr(msg.WParam));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  mhnd := 0;
  SetHook;
  SetMainHandle(Handle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  UnSetHook;
end;
end.

// end of main unit..

// Start of khook.dll
library khook;
uses
  Windows,classes,
  Messages;

procedure SetHook; export; forward;
procedure UnSetHook; export; forward;
procedure SetMainHandle(Handle: HWND); export; forward;

exports
 SetHook ,
 UnSetHook ,
 SetMainHandle ;
const
   mGet_message = WM_user+6666;
   mSet_Focus   = WM_user+6667;
type
 PHookRec = ^THookRec;
 THookRec = record
   MainWindow: HWND;
   HookID: HHOOK;
 end;

var
 rHookRec: PHookRec = nil;
 hMapObject: THandle = 0;

procedure SetMainHandle(Handle: HWND);
begin
 rHookRec^.MainWindow := Handle;
end;

// WndProcRet hook proc
function WNDPROCRET_HookProc(nCode: Integer; iWPARAM: INTEGER; iLPARAM: INTEGER): integer; stdcall;
begin
 result := 0;
 if (nCode >= 0) And (nCode=HCBT_CREATEWND) then begin
   SendMessage(rHookRec^.MainWindow, mGet_message, iWPARAM,iLPARAM);
 end else
   Result:=CallNextHookEx(rHookRec^.HookID, nCode, iWPARAM,iLPARAM);
end;


procedure SetHook;
begin
 rHookRec^.HookID := SetWindowsHookEx(WH_CBT, @WNDPROCRET_HookProc, hInstance, 0);
end;

procedure UnSetHook;
begin
 UnHookWindowsHookEx(rHookRec^.HookID);
end;

procedure EntryPointProc(Reason: Integer);
begin

 case reason of
   DLL_PROCESS_ATTACH:
     begin
       hMapObject := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(rHookRec^), 'Keyb_Hook');
       rHookRec := MapViewOfFile(hMapObject, FILE_MAP_WRITE, 0, 0, sizeof (rHookRec^));
     end;

   DLL_PROCESS_DETACH:
     begin
       try
         unsethook;
         UnMapViewOfFile(rHookRec);
         CloseHandle(hMapObject);
      except
       end;
     end;

   DLL_THREAD_ATTACH:
     begin
     end;
   DLL_THREAD_DETACH:
     begin
     end;
 end;
end;

begin
 DllProc := @EntryPointProc;
 EntryPointProc(DLL_PROCESS_ATTACH);
end.
// End of dll
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10993418

   If you want to monitor messages you should not use WH_KEYBOARD in SetWindowsHookEx function. You have to look at the MSDN, but I think you need to use WH_CALLWNDPROC.

  WH_CALLWNDPROC      - Installs a hook procedure that monitors messages before the system sends them to the destination window procedure. For more information, see the CallWndProc hook procedure.
0
 

Author Comment

by:The_SnifF
ID: 11008935
@non_zero, It works, and I get the message, but i get it every second, even when no app was loaded.
And it eventually get windows stuck.
Have any solution?
0
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 11012323
H&#305;mmm..
it happens because, a hint is an window also..
An edit box is an window.. everything that is created is a window..

so u ave to select the main window using the handle..

Get the parent window
if handle(HWND) of parent  window is 0 (zero) it means that it s a new window.. not a child one ..

I hope it helps

Melih SARICA

And u can get detail about CBTPROC hook from the link below

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/CBTProc.asp
0
 

Author Comment

by:The_SnifF
ID: 11013452
I changed the func as so:

function WNDPROCRET_HookProc(nCode: Integer; iWPARAM: INTEGER; iLPARAM: INTEGER): integer; stdcall;
begin
 Result := 0;
 if (nCode >= 0) and (nCode=HCBT_CREATEWND) then
  begin
  if (IsWindowVisible(iWPARAM)) and (GetParent(iWPARAM) = 0) then
    if GetWindowText(iWPARAM, Caption, SizeOf(Caption)-1) <> 0 then
     SendMessage(rHookRec^.MainWindow, mGet_message, iWPARAM,iLPARAM);
  end
 else
  Result:=CallNextHookEx(rHookRec^.HookID, nCode, iWPARAM,iLPARAM);
end;

And I still get "flooded" by the messages and windows freezes.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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