🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10000

Re: Windows message hook

#10000
From: Mark Jacobs
Date: Fri, 28 May 2004 13:21
78 lines
4109 bytes
"Fractal Cat" <pjb@prime-projects.co.uk> wrote in message news:40b24ca0@newsgroups.borland.com...
> How can I intercept the creation of a window and perform my own processing?
> I want to know the dimensions of the window that is being created and I want
> to do some graphics before the window is displayed.

You need to write an EXE and a DLL that monitor system events and look for the WM_CREATE (create a window)
message. I have a program in BCB version 5 that intercepts and records any event on the system. However, it
would not be appropriate for me to lay out all that code here. However, I can divulge perhap a starting point
for the DLL and EXE files. Note that I write and read files in the root of the C:\ drive in order to pass info
from the EXE to the DLL and vice versa, rather than using the more standard shared DLL memory techniques. Here
is some code from the DLL :-

...
extern "C" __declspec(dllexport) HOOKPROC WatchHandle(int nCode,WPARAM processFlag,LPARAM msg);

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
  return 1;
}
//---------------------------------------------------------------------------
HHOOK getcurhk()
{
  FILE *fp=fopen("c:\\mjmsghk.txt","rb"); char bfr[80],*endptr;
  if (!fp) return (HHOOK)0;
  endptr=fgets(bfr,80,fp); fclose(fp);
  if (!endptr) return (HHOOK)0;
  return (HHOOK)strtoul(bfr,&endptr,0);
}
//---------------------------------------------------------------------------
HOOKPROC WatchHandle(int nCode,WPARAM processFlag,LPARAM msg)
{
  if (nCode<0) return (HOOKPROC)CallNextHookEx(getcurhk(),nCode,processFlag,msg);
  MSG *mjmsg=(MSG *)msg; FILE *fp; char lkfr[80],bfr[80]; bool fnd1=false; int hh;
  fp=fopen("c:\\mjmsgflt.txt","rb"); if (!fp) return 0;
  sprintf(bfr,"MSG=%X HDL=%lu W=",mjmsg->message,(unsigned long)mjmsg->hwnd);
  while (fgets(lkfr,80,fp))
  {
    hh=strlen(lkfr)-1;
    while (hh>=0) if (lkfr[hh]=='\r' || lkfr[hh]=='\n') lkfr[hh--]='\0'; else break;
    if (hh>=0 && strstr(bfr,lkfr)) { fnd1=true; break;}
  }
  fclose(fp);
  if (!fnd1) return 0;
  fp=fopen("c:\\mjmsgdmp.txt","ab"); if (!fp) return 0;
  fprintf(fp,"%s%lu L=%lu\r\n",bfr,mjmsg->wParam,mjmsg->lParam); fclose(fp);
  return 0;
}
...

and here is some from the EXE that uses the DLL :-

...
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
  static bool clld=false;
  if (clld) return;
  clld=true; mjmsghk=LoadLibrary("mjmsghook.dll");
  if (!mjmsghk)
  {
    ShowMessage("Could not load MJMSGHOOK.DLL - "+mjgetlasterror()); Timer2->Enabled=true; return;
  }
  mjhkproc=(LPFNDLLFUNC1)GetProcAddress(mjmsghk,"_WatchHandle");
  if (!mjhkproc)
  {
    ShowMessage("Error WatchHandle GetProcAddress - "+mjgetlasterror()); Timer2->Enabled=true; return;
  }
...

Timer2, when invoked, calls Close(); to kill the app, rather than trying to kill the app in the middle of it
first painting the main form which usually causes GPFs.

HTH,
-- 
Mark Jacobs
DK Computing
http://www.dkcomputing.co.uk
markj atty critical dotty co dotty uk
Technical Details
Message-ID: <40b73cc6$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40b24ca0@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,2c0d2128b33c6af
X-Google-Attributes: gid10cc67,public
Reply-To: "Mark Jacobs" <markj atty critical dotty co dotty uk>
From: "Mark Jacobs" <markj atty critical dotty co dotty uk>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <40b24ca0@newsgroups.borland.com>
Subject: Re: Windows message hook
Date: Fri, 28 May 2004 14:21:12 +0100
Lines: 80
Organization: DK Computing
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 81.144.157.8
Message-ID: <40b73cc6$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1085750470 81.144.157.8 (28 May 2004 06:21:10 -0700)
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail