🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: [Remy] SendMessage() from a DLL to APP

#6498
From: Remy Lebeau (TeamB)
Date: Wed, 04 May 2005 18:26
80 lines
3986 bytes
"Vladimir Stefanovic" <antivari@po.sbb.co.yu> wrote in message
news:4278b3f0@newsgroups.borland.com...

> Anyway, can you please take a short look at the code and see if
> I should make any (maybe code security) corrections to it.

> 1) Is there any sigifficant difference if I use SendMessage() or
> PostMessage() from my DLL, in my case?

That entirely depends on your actual needs.  SendMessage() waits for the
message to be processed.  PostMessage() does not.

> 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?

No, do not use NULL, or else the memory won't be shared at all.  The name is
what makes it sharable to begin with.

> 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?


> extern "C" __declspec(dllexport) __stdcall void SetHook(HWND);
> extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);

I would recommend adding return values to those functions so that the
application can know whether or not the hooking/unhooking actuall succeeds.

>            if ( hFileMapping )
>               pHookInfo = (PHOOKINFO)MapViewOfFile( hFileMapping,
>                                                     FILE_MAP_WRITE,
>                                                     0,
>                                                     0,
>                                                     0 );

If either CreateFileMapping() or MapViewOfFile() fail, I prefer to release
the file mapping and make DllEntryPoint() return FALSE.  That way, the
application knows that the DLL was unable to initialize itself, and thus the
hooks won't work properly.

>            UnmapViewOfFile( pHookInfo );
>            pHookInfo = NULL;
>            CloseHandle( hFileMapping );
>            hFileMapping = NULL;
>            break;

Make sure to check for NULL pointers before calling the functions:

    if( pHookInfo  )
        UnmapViewOfFile( pHookInfo );
    if( hFileMapping )
        CloseHandle( hFileMapping );

> DWORD __stdcall CheckKey( int nCode, WORD wParam, LONG lParam )

You can simplify that code to the following:

    DWORD __stdcall CheckKey( int nCode, WORD wParam, LONG lParam )
    {
        if( pHookInfo )
        {
            if( ((nCode >= 0) && (nCode != HC_NOREMOVE))
                && ((lParam & 0x40000000) == 0) // Skip if it's a repeat
                && (lParam & 0x20000000) ) // Check if Alt is pressed
            {
                // Send key information to the main window
                if( pHookInfo->hAppWnd )
                    PostMessage(pHookInfo->hAppWnd, WM_KEYHOOK, 0, lParam);
            }
            return CallNextHookEx(pHookInfo->ghhookKB, nCode, wParam,
lParam);
        }
        return 0;
    }


Gambit
Technical Details
Message-ID: <42791382$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!proxad.net!news.wiretrip.org!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <427748eb@newsgroups.borland.com> <4277c28c$1@newsgroups.borland.com> <4278b3f0@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: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <427748eb@newsgroups.borland.com> <4277c28c$1@newsgroups.borland.com> <4278b3f0@newsgroups.borland.com>
Subject: Re: [Remy] SendMessage() from a DLL to APP
Date: Wed, 4 May 2005 11:26:46 -0700
Lines: 82
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: 68.69.17.41
Message-ID: <42791382$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1115231106 68.69.17.41 (4 May 2005 11:25:06 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!proxad.net!news.wiretrip.org!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail