Solved

Know Everything

Posted on 2004-10-12
7
229 Views
Last Modified: 2010-04-05
I used to program in VB, I used to use a dll ShellHook.dll
Is there anything similar in Delphi? I need to do exactly the same thing, need to know if anything is closed or opened (porgram or folder)
thanks
0
Comment
Question by:peritoX
  • 4
  • 2
7 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12290153
You can use this DLL with Delphi as well. Register the DLL, then Import the Type Library and it will
create a component that you can drop onto your form and hook up the events.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 100 total points
ID: 12290492
Here is the sample program included with the DLL converted to Delphi.
Note. I am getting a C000005 error when closing. Not sure what is happening.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    cShellHook1: TcShellHook;
    Memo1: TMemo;
    procedure cShellHook1WindowCreated(Sender: TObject; hwnd: Integer);
    procedure cShellHook1WindowDestroyed(Sender: TObject; hwnd: Integer);
    procedure cShellHook1WindowActivated(Sender: TObject; hwnd: Integer);
    procedure FormShow(Sender: TObject);
    procedure cShellHook1ShellWindowActivated(Sender: TObject);
    procedure cShellHook1TaskBarButtonRedraw(Sender: TObject;
      hwnd: Integer);
    procedure cShellHook1TaskManActivated(Sender: TObject; hwnd: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cShellHook1LocaleChanged(Sender: TObject; LocaleID: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetWndText(hwnd: LongInt):String;
var
  k: LongInt;
  sName: String;
begin
  SetLength(sName,128);
  k := GetWindowText(hwnd, PChar(sName), 128);
  if k > 0 then
    sName := Copy(sName, 1, k)
  else
    sName := 'No caption';
  Result := sName;
end;

function GetWndClass(hwnd: LongInt):String;
var
  k: LongInt;
  sName: String;
begin
  SetLength(sName,128);
  k := GetClassName(hwnd, PChar(sName), 128);
  if k > 0 then
    sName := Copy(sName, 1, k)
  else
    sName := 'No class';
  Result := sName;
end;

function GetLanguageInfo(const dwLocaleID: LongInt; const dwLCType: LongInt): String;
var
  sReturn: String;
  nRet: LongInt;
begin
  SetLength(sReturn,128);
  nRet := GetLocaleInfo(dwLocaleID, dwLCType, PChar(sReturn), 128);
  if nRet > 0 then
    Result := Copy(sReturn, 1, nRet - 1);
end;

procedure TForm1.cShellHook1WindowCreated(Sender: TObject; hwnd: Integer);
begin
  Memo1.Lines.Add('Window created, Caption: '+ GetWndText(hwnd) +
                  ', Class: ' + GetWndClass(hwnd) + ', hwnd = ' +IntToStr(hwnd));
end;

procedure TForm1.cShellHook1WindowDestroyed(Sender: TObject;
  hwnd: Integer);
begin
  Memo1.Lines.Add('Window destroyed, Caption: '+ GetWndText(hwnd) +
                  ', Class: ' + GetWndClass(hwnd) + ', hwnd = ' +IntToStr(hwnd));
end;

procedure TForm1.cShellHook1WindowActivated(Sender: TObject;
  hwnd: Integer);
begin
  Memo1.Lines.Add('Window activated, Caption: '+ GetWndText(hwnd) +
                  ', Class: ' + GetWndClass(hwnd) + ', hwnd = ' +IntToStr(hwnd));
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  cShellHook1.SetShellHook(Application.Handle, RSH_REGISTER_TASKMAN);
end;

procedure TForm1.cShellHook1ShellWindowActivated(Sender: TObject);
begin
  Memo1.Lines.Add('Main Shell Window activated');
end;

procedure TForm1.cShellHook1TaskBarButtonRedraw(Sender: TObject;
  hwnd: Integer);
begin
  Memo1.Lines.Add('Minimized button on taskbar was redrawn, Caption: '+ GetWndText(hwnd) +
                  ', Class: ' + GetWndClass(hwnd) + ', hwnd = ' +IntToStr(hwnd));
end;

procedure TForm1.cShellHook1TaskManActivated(Sender: TObject;
  hwnd: Integer);
begin
  Memo1.Lines.Add('Task manager activated, Caption: '+  GetWndText(hwnd) + ', Class: ' + GetWndClass(hwnd) + ', hwnd = ' + IntToStr(hwnd));
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  cShellHook1.RemoveShellHook;
end;

procedure TForm1.cShellHook1LocaleChanged(Sender: TObject;
  LocaleID: Integer);

  const LOCALE_SENGLANGUAGE = $1001;
var
  sLang: String;
begin
   sLang := GetLanguageInfo(LocaleID, LOCALE_SENGLANGUAGE);
   Memo1.Lines.Add('Language changed to '+ sLang);
end;

end.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12358679
Helllloooo, PeritoX, are you there???

Did my response help you solve your problem?
0
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.

 

Author Comment

by:peritoX
ID: 12372436
I am terribly sorry I didnt reply
truly I didnt try it yet because I dont have delphi installed, I will after like 5 days
anyway, the points are yours. If theres anything I want to ask you, you get an email
Thanks Alot & Sorry For The Delay.
0
 

Author Comment

by:peritoX
ID: 12452907
ok, I got delphi7 and I tried your code this is what I got(Im still newbie

so the problem may be very silly sorry):

Field Form1.cShellHook1 does not have a corresponding component. Remove the

dec?
The FormCreate method referenced by Form1.OnCreate does not exist. Remove

the ref?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12464612
You have to import the DLL using Import Type Library and then drop the cShellHook control on your form.

Then create the FormCreate by dbl-clicking it in the Object Inspector and copy/past the code from the FormCreae
above into it.
0
 

Expert Comment

by:instant_new
ID: 12680131
hello EddieShipman,
you seem good at VB and Delphi
please can u help me here:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21220862.html
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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