Article View: borland.public.cppbuilder.nativeapi
Article #6492Re: [Remy] SendMessage() from a DLL to APP
From: Vladimir Stefanovic
Date: Wed, 04 May 2005 11:37
Date: Wed, 04 May 2005 11:37
175 lines
6081 bytes
6081 bytes
Hi Remy,
Following your instructions I completely redesigned my DLL, and
now I have placed HWND in the shared memory, at least I think I did.
The code now works as I expect, and so far I haven't noticed crashes
of other VCL programs on their termination like it was the case when
I used FindWindow (from a DLL) to get the App HWND.
Anyway, can you please take a short look at the code and see if
I should make any (maybe code security) corrections to it.
Also, I have few questions:
1) Is there any sigifficant difference if I use SendMessage() or
PostMessage() from my DLL, in my case?
2) In the API CreateFileMapping(), I named a last argument "DiHook",
and I never used it after that. Is that OK? Should I use NULL?
3) Is that normal that I haven't any chages (because of shared memory
changes in y DLL) at my App side, except that I passed the HWND
argument to install a DLL?
--- DLL CODE ---
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#define WM_KEYHOOK (WM_USER + 1002)
HINSTANCE ghInst;
typedef struct _HookInfo
{
HHOOK ghhookKB;
HWND hAppWnd;
} HOOKINFO, *PHOOKINFO;
HANDLE hFileMapping = NULL;
PHOOKINFO pHookInfo = NULL;
#pragma argsused
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) __stdcall void SetHook(HWND);
extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);
extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,LONG);
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void
*lpReserved)
{
ghInst = hinst;
switch ( reason )
{
case DLL_PROCESS_ATTACH:
hFileMapping = CreateFileMapping( INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(HOOKINFO),
"DiHook" );
if ( hFileMapping )
pHookInfo = (PHOOKINFO)MapViewOfFile( hFileMapping,
FILE_MAP_WRITE,
0,
0,
0 );
break;
case DLL_PROCESS_DETACH:
UnmapViewOfFile( pHookInfo );
pHookInfo = NULL;
CloseHandle( hFileMapping );
hFileMapping = NULL;
break;
}
return ( 1 );
}
void __stdcall SetHook( HWND hWnd )
{
if ( pHookInfo )
{
if( pHookInfo->ghhookKB == NULL )
pHookInfo->ghhookKB = SetWindowsHookEx( WH_KEYBOARD,
(HOOKPROC)CheckKey, ghInst, NULL );
pHookInfo->hAppWnd = hWnd;
}
}
void __stdcall RemoveHook( void )
{
if ( pHookInfo )
{
if ( pHookInfo->ghhookKB )
{
UnhookWindowsHookEx( pHookInfo->ghhookKB );
pHookInfo->ghhookKB = NULL;
}
}
}
DWORD __stdcall CheckKey( int nCode, WORD wParam, LONG lParam )
{
if ( ( nCode < 0 ) || ( nCode == HC_NOREMOVE ) )
return CallNextHookEx( pHookInfo->ghhookKB, nCode, wParam, lParam );
// Skip if it's a repeat
if ( lParam & 0x40000000 )
return CallNextHookEx( pHookInfo->ghhookKB, nCode, wParam, lParam );
// Check if Alt is pressed
if ( !( lParam & ( 1<<29 ) ) )
return CallNextHookEx( pHookInfo->ghhookKB, nCode, wParam, lParam );
// Send key information to the main window
if ( pHookInfo && pHookInfo->hAppWnd )
PostMessage( pHookInfo->hAppWnd, WM_KEYHOOK, 0, lParam );
return CallNextHookEx( pHookInfo->ghhookKB, nCode, wParam, lParam );
}
--
Best regards,
Vladimir Stefanovic
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:4277c28c$1@newsgroups.borland.com...
>
> "Vladimir Stefanovic" <antivari@po.sbb.co.yu> wrote in message
> news:427748eb@newsgroups.borland.com...
>
>> HWND hAppWnd;
>
> You should always initialize your variables, especially when used
> globally:
>
> HWND hAppWnd = NULL;;
>
>> ghhookKB = SetWindowsHookEx( WH_KEYBOARD, (HOOKPROC)CheckKey, ghInst,
> NULL)
>
> You did not specify whether the HHOOK was in shared memory or not. It
> must
> be. Otherwise, the hook will only work in your own application and not in
> others. When you place your icon in the System Tray and hide your window,
> your window won't be focused anymore, and won't be the window receiving
> keyboard input. If you do not set up your hook properly to begin with,
> then
> you will not be hooking the keyboard input of other applications properly.
>
>> // Send key information to the main window
>> SendMessage( hAppWnd, WM_KEYHOOK, 0, lParam );
>
> Check the HWND for NULL before calling SendMessage():
>
> if( hAppWnd )
> SendMessage( hAppWnd, WM_KEYHOOK, 0, lParam );
>
>
> Gambit
>
>
Technical Details
Message-ID:
<4278b3f0@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<427748eb@newsgroups.borland.com> <4277c28c$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,94b1d46dd644a85c X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Newsgroups: borland.public.cppbuilder.nativeapi References: <427748eb@newsgroups.borland.com> <4277c28c$1@newsgroups.borland.com> Subject: Re: [Remy] SendMessage() from a DLL to APP Date: Wed, 4 May 2005 13:37:29 +0200 Lines: 177 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 82.117.212.65 Message-ID: <4278b3f0@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1115206640 82.117.212.65 (4 May 2005 04:37:20 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail