Article View: borland.public.cppbuilder.nativeapi
Article #8073Re: Wierd CBTHook problem
From: Remy Lebeau (TeamB)
Date: Wed, 01 Dec 2004 22:14
Date: Wed, 01 Dec 2004 22:14
92 lines
3708 bytes
3708 bytes
"mike" <mfbrowne@earthlink.net> wrote in message
news:41ae3911@newsgroups.borland.com...
> while ( true )
Why are you looping? You should not be doing that.
> hFileMapHandle = OpenFileMap()
<snip>
> lpFileMapAddress = MapFile( hFileMapHandle )
Why are you retrieving the file mapping every time the hook is called? You
should be retrieving it only once, when the DLL is first loaded.
> cds.dwData = 10000
> cds.cbData = 0
> cds.lpData = NameVal.c_str()
You are speciying 0 for cbData when you should be specifying
NameVal.Length() instead. By setting it to 0, WM_COPYDATA wo't be able to
actually copy any data - you told it there isn't any data to copy.
BTW, you should give dwData a more unique value. You shouldn't hard-code
it, unless you can 100% guarantee that it will always be unique. 10000 is
not likely to always be unique. Something more like this will be, though:
cds.dwData = 0x4D696B65; // "Mike" in hex
Now, with all of that said, try this code instead:
HANDLE hFileMapHandle = NULL;
tagMemoryFileDefinition *MyMemoryFile = NULL;
BOOL WINAPI DllEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID
lpvReserved)
{
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
{
hFileMapHandle = OpenFileMap();
if( !hFileMapHandle )
return FALSE;
MyMemoryFile = static_cast<tagMemoryFileDefinition*>(
MapFile(hFileMapHandle) );
if( !MyMemoryFile )
{
CloseHandle(hFileMapHandle);
return FALSE;
}
break;
}
case DLL_PROCESS_DETACH:
{
if( MyMemoryFile ) UnmapViewOfFile(MyMemoryFile);
if( hFileMapHandle ) CloseHandle(hFileMapHandle);
break;
}
}
return TRUE;
}
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if( (nCode >= 0) && (nCode == HCBT_CREATEWND) )
{
HWND hWndCreate = reinterpret_cast<HWND>(wParam);
AnsiString ClassVal =
reinterpret_cast<LPCBT_CREATEWND>(lParam)->lpcs->lpszClass;
AnsiString DataVal = ("Class " + ClassVal);
COPYDATASTRUCT cds = {0};
cds.dwData = 0x4D696B65;
cds.cbData = DataVal.Length();
cds.lpData = DataVal.c_str();
::SendMessage(MyMemoryFile->hWindowForMessages, WM_COPYDATA, 0,
reinterpret_cast<LPARAM>(&cds));
}
return CallNextHookEx(MyMemoryFile->hMsgHook, nCode, wParam,
lParam);
}
Gambit
Technical Details
Message-ID:
<41ae425d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.linkpendium.com!news.linkpendium.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<41ae07e2$1@newsgroups.borland.com> <41ae2598$1@newsgroups.borland.com> <41ae3911@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,80a054b0854861c4 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: <41ae07e2$1@newsgroups.borland.com> <41ae2598$1@newsgroups.borland.com> <41ae3911@newsgroups.borland.com> Subject: Re: Wierd CBTHook problem Date: Wed, 1 Dec 2004 14:14:49 -0800 Lines: 94 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: <41ae425d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1101939293 68.69.17.41 (1 Dec 2004 14:14:53 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news.linkpendium.com!news.linkpendium.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail