Article View: borland.public.cppbuilder.nativeapi
Article #8074Re: Wierd CBTHook problem
From: Bob Gonder
Date: Wed, 01 Dec 2004 22:13
Date: Wed, 01 Dec 2004 22:13
115 lines
4459 bytes
4459 bytes
mike wrote:
>
>LRESULT CALLBACK CBTProc( int nCode, WPARAM wParam, LPARAM lParam)
>{
> bool ByPassKey = false;
> bool RetValue = true;
> HANDLE hFileMapHandle = NULL;
> LPVOID lpFileMapAddress = NULL;
> AnsiString NameVal;
> AnsiString WindowClass;
> COPYDATASTRUCT cds;
>
> char buffer[MAX_PATH];
>
> while ( true )
> {
> memset( buffer, 0x00, strlen( buffer ) )
This is almost always wrong. Use
memset( buffer, 0, sizeof( buffer ) )
> hFileMapHandle = OpenFileMap();
> if (hFileMapHandle == NULL)
> {
> RetValue = false;
You never use RetValue.
> break ;
> }
> lpFileMapAddress = MapFile( hFileMapHandle );
> if (lpFileMapAddress == NULL)
> {
> RetValue = false;
You never use RetValue.
You didn't close the Map handle.
> break ;
> }
> tagMemoryFileDefinition *MyMemoryFile = (tagMemoryFileDefinition*)((LPDWORD) lpFileMapAddress);
> if( nCode < 0 )
Why do you wait so long to check nCode ?
Why not check it before you even attempt to open the MapFile ?
> {
> UnmapViewOfFile( lpFileMapAddress );
> CloseHandle( hFileMapHandle );
> return CallNextHookEx( hMsgHook, nCode, wParam, lParam) ;
This should be a break instead of return.
> }
> HWND hWnd = MyMemoryFile->hWindowForMessages;
> hMsgHook = MyMemoryFile->hMessageHook;
> if ( ( nCode >= 0 ) && ( nCode == HCBT_CREATEWND ) )
Ummm, You already checked that nCode was >= 0. Besides it is redundant
in that "if", because if it is == HCBT_CREATEWND, then it will be >= 0
by definition (unless HCBT_CREATEWND works out to be negative, in
which case, the "if" will never fire).
> {
> HWND hWndCreate = ( HWND )wParam;
> LPCBT_CREATEWND pcbt = ( LPCBT_CREATEWND )lParam;
> CREATESTRUCT *pcs = (( CBT_CREATEWND * )lParam)->lpcs;
> NameVal = pcs->lpszName;
> ::GetClassName( hWndCreate, buffer, 256 );
That should be sizeof(buffer), not 256.
> WindowClass = AnsiString( buffer );
> NameVal = "Class ";
> NameVal += WindowClass;
> cds.dwData = 10000;
> cds.cbData = 0;
> cds.lpData = NameVal.c_str();
> ::SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&cds);
> break ;
Break bypasses the closing of the Map File.
> }
Unless you meant that } to be end-of-while instead of end-of-if.
In which case, the following code is executed even if the file was
never opened.
> UnmapViewOfFile( lpFileMapAddress );
> CloseHandle( hFileMapHandle );
End While should be here.
And there should be a break before it.
> if ( ByPassKey )
??? ByPassKey is never used (always false) ???
??? You never use RetValue ???
> {
> return 1 ;
> }
> else
> {
> return CallNextHookEx ( hMsgHook, nCode, wParam, lParam );
> }
>
Where is the end while ?
>
>}
>
Technical Details
Message-ID:
<kvesq0198ab6j38f86aac2pcv86m5274vk@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!newsread.com!newsprint.newsread.com!news.glorb.com!newshosting.com!nx01.iad01.newshosting.com!203.109.252.33.MISMATCH!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: Bob Gonder <notbg@notmindspring.invalid> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Wierd CBTHook problem Date: Wed, 01 Dec 2004 14:13:34 -0800 Organization: MindSpring Enterprises, Inc. Reply-To: notbg@notmindspring.invalid Message-ID: <kvesq0198ab6j38f86aac2pcv86m5274vk@4ax.com> References: <41ae07e2$1@newsgroups.borland.com> <41ae2598$1@newsgroups.borland.com> <41ae3911@newsgroups.borland.com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 67.77.38.23 X-Trace: newsgroups.borland.com 1101939209 67.77.38.23 (1 Dec 2004 14:13:29 -0700) Lines: 116 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!newsread.com!newsprint.newsread.com!news.glorb.com!newshosting.com!nx01.iad01.newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail