🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Global window hook

#7263
From: roger
Date: Thu, 24 Feb 2005 12:01
301 lines
8922 bytes
this is the DLL contents;;

-------------------------------
#include <vcl.h>
#include <windows.h>
#pragma pack(1)
typedef struct tagMSGGLOBAL
{
    HWND hHandle;
} TMSGGLOBAL;
typedef TMSGGLOBAL* PMSGGLOBAL;
#pragma pack()

HINSTANCE hInst;
HANDLE hObjHandle;
PMSGGLOBAL lpMsgGlobal;
HHOOK hHook = NULL;
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam);

extern "C" {
    __declspec(dllexport) PMSGGLOBAL __stdcall GetGlobalPointer(void);
    bool __stdcall __declspec(dllexport) InstallHook(void);
    bool __stdcall __declspec(dllexport) RemoveHook(void);
}

void __fastcall MapFileMemory(DWORD dwAllocSize)
{
    // Create a process wide memory mapped variable
    if( hObjHandle == NULL )
    {
        hObjHandle = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL,
                                        PAGE_READWRITE, 0, dwAllocSize,
                                        "HookGlobalMemBlock" );

        if( hObjHandle == NULL )
        {
            MessageBox( 0, "Could not create file map object",
                        "Hook DLL", MB_OK );
            return;
        }
    }

    // Get a pointer to our process wide memory mapped variable
    if( lpMsgGlobal == NULL )
    {
        lpMsgGlobal = ( PMSGGLOBAL)MapViewOfFile( hObjHandle,
FILE_MAP_WRITE,
                                                  0, 0, dwAllocSize );

        if( lpMsgGlobal != NULL )
        {
            lpMsgGlobal->hHandle = NULL;
        }
        else
        {
            CloseHandle( hObjHandle );
            MessageBox( 0, "Could not map file", "Hook DLL", MB_OK );
            return;
        }
    }
}

void __fastcall UnMapFileMemory()
{
    // Delete our process wide memory mapped variable
    if( lpMsgGlobal != NULL )
    {
        UnmapViewOfFile( lpMsgGlobal );
        lpMsgGlobal = NULL;
    }

    if( hObjHandle > 0 )
    {
        CloseHandle( hObjHandle );
        hObjHandle = 0;
    }
}

PMSGGLOBAL __stdcall GetGlobalPointer(void)
{
    // Return a pointer to our process wide memory mapped variable
    return lpMsgGlobal;
}

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if( ( nCode >= 0 ) && ( nCode == HCBT_CREATEWND ) &&
lpMsgGlobal->hHandle != NULL  )
    {
        HWND hWnd = (HWND)wParam;
        {
            char buffer[256] = {0};
            COPYDATASTRUCT cds;

            cds.dwData = 10000; // some arbitrary value unique to your app
            cds.cbData = ::GetWindowText( hWnd, buffer, 256 );
            cds.lpData = buffer;

            ::SendMessage( lpMsgGlobal->hHandle, WM_COPYDATA,
0,(LPARAM)&cds );
        }
    }
    else
    if( ( nCode >= 0 ) && ( nCode == HCBT_DESTROYWND ) &&
lpMsgGlobal->hHandle != NULL  )
    {
        HWND hWnd = (HWND)wParam;
        {
            char buffer[256] = {0};
            COPYDATASTRUCT cds;

            cds.dwData = 10000; // some arbitrary value unique to your app
            cds.cbData = ::GetWindowText( hWnd, buffer, 256 );
            cds.lpData = buffer;

            ::SendMessage( lpMsgGlobal->hHandle, WM_COPYDATA,
0,(LPARAM)&cds );
        }
    }


    return CallNextHookEx( hHook, nCode, wParam, lParam );
}

bool __stdcall InstallHook()
{
    if( hHook == NULL )
    {
        if( ( hHook = SetWindowsHookEx( WH_CBT, (HOOKPROC)CBTProc,
hInst,0 ) ) == NULL )
            MessageBox( NULL, "Could not set hook", "Hook DLL", MB_OK );
    }

    return ( hHook != NULL );
}

bool __stdcall RemoveHook()
{
    if( hHook != NULL )
    {
        if( UnhookWindowsHookEx( hHook ) != FALSE )
        {
            hHook = NULL;
        }
        else
        {
            MessageBox( NULL, "Could not release hook", "Hook DLL", MB_OK );
        }
    }

    return ( hHook == NULL );
}

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*
lpReserved)
{
      hInst = hinst;
    switch(reason)
    {
        case DLL_PROCESS_ATTACH:
            hObjHandle = 0;
            lpMsgGlobal = NULL;
            MapFileMemory(sizeof(TMSGGLOBAL));
            break;
        case DLL_PROCESS_DETACH:
            UnMapFileMemory();
            break;
    }
  return 1;
}


-------------------------------

"roger" <rogersmail@ntlworld.com> wrote in message
news:421dc13d$1@newsgroups.borland.com...
> Hi REMY...
>
> I have found a snippet that you posted some while ago which i have created
> and added the lib to the below project, but i am not getting any response
> from the dll when a window opens or closes.
>
> =============================
> ******     Header File contents   *******
> =============================
>
> #ifndef mainUnitH
> #define mainUnitH
>
//--------------------------------------------------------------------------
> -
> #include <Classes.hpp>
> #include <Controls.hpp>
> #include <StdCtrls.hpp>
> #include <Forms.hpp>
>
>
//--------------------------------------------------------------------------
> -
> class TForm1 : public TForm
> {
> __published: // IDE-managed Components
>         TMemo *Memo1;
>     void __fastcall WMCopyData(TMessage &Message);
>         void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
> public:
>     BEGIN_MESSAGE_MAP
>         VCL_MESSAGE_HANDLER(WM_COPYDATA, TMessage, WMCopyData)
>     END_MESSAGE_MAP(TForm)
>         __fastcall TForm1(TComponent* Owner);
> };
>
//--------------------------------------------------------------------------
> -
> extern PACKAGE TForm1 *Form1;
>
//--------------------------------------------------------------------------
> -
> #endif
>
> ==========================
> *******  cpp file contents    *******
> ==========================
>
//--------------------------------------------------------------------------
> -
>
> #include <vcl.h>
> #pragma hdrstop
>
> #include "mainUnit.h"
>
//--------------------------------------------------------------------------
> -
> #pragma package(smart_init)
> #pragma resource "*.dfm"
> TForm1 *Form1;
> extern "C" {
>     bool __stdcall __declspec(dllexport) InstallHook(void);
>     bool __stdcall __declspec(dllexport) RemoveHook(void);
> }
>
//--------------------------------------------------------------------------
> -
> __fastcall TForm1::TForm1(TComponent* Owner)
>         : TForm(Owner)
> {
>     InstallHook();
> }
>
//--------------------------------------------------------------------------
> -
> void __fastcall TForm1::WMCopyData(TMessage &Message)
> {
>     COPYDATASTRUCT *cds = (COPYDATASTRUCT*)Message.LParam;
>     if(cds->dwData == 10000) // message we sent ourselves
>     {
>         char *buffer = new char[cds->cbData];
>         memcpy(buffer, cds->lpData, cds->cbData);
>         Memo1->Lines->Add(buffer);
>     }
>     else
>         // we're not interested in this message, pass to default handling
>         TForm::Dispatch(&Message);
> }
> void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
> {
>     RemoveHook();
> }
>
//--------------------------------------------------------------------------
> -
>
> *****  end  *****
>
> "roger" <rogersmail@ntlworld.com> wrote in message
> news:421d891c@newsgroups.borland.com...
> > Thanks
> >
> > "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
> > news:421d81d6@newsgroups.borland.com...
> > >
> > > "roger" <rogersmail@ntlworld.com> wrote in message
> > > news:421d76a8@newsgroups.borland.com...
> > >
> > > > Can someone please tell me the best way(if poss) to hook via
> > > > a DLL when windows opens or closes a new window of any
> > > > kind
> > >
> > > Look at the WH_CBT hook.  It has HCBT_CREATEWND and HCBT_DESTROYWND
> > > notifications.
> > >
> > >
> > > Gambit
> > >
> > >
> >
> >
>
>
Technical Details
Message-ID: <421dc1e6@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!news.glorb.com!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <421d76a8@newsgroups.borland.com> <421d81d6@newsgroups.borland.com> <421d891c@newsgroups.borland.com> <421dc13d$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,9b9eac270d6034d6
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "roger" <rogersmail@ntlworld.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <421d76a8@newsgroups.borland.com> <421d81d6@newsgroups.borland.com> <421d891c@newsgroups.borland.com> <421dc13d$1@newsgroups.borland.com>
Subject: Re: Global window hook
Date: Thu, 24 Feb 2005 12:01:01 -0000
Lines: 303
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2720.3000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2739.300
NNTP-Posting-Host: 81.103.71.81
Message-ID: <421dc1e6@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1109246438 81.103.71.81 (24 Feb 2005 04:00:38 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!news.glorb.com!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail