Click here to Skip to main content
13,347,168 members (57,141 online)
Rate this:
Please Sign up or sign in to vote.
See more:
In the MFC code for Visual Studio 6.0 there is a function

void AFXAPI AfxHookWindowCreate(CWnd* pWnd)

and it installs a windows hook to catch window creation.

if (pThreadState->m_hHookOldCbtFilter == NULL)
    pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT,
        _AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
    if (pThreadState->m_hHookOldCbtFilter == NULL)

Regrettably, even the debug version of the code throws the memory exception instead of printing out the last error code.

This is happening so close to application startup, and cannott be replicated as a problem if the application is being debugged. The circumstances to make it happen are so bizarre, that I really think just knowing the error code could help here.

Has anyone else run into the problem with this failing?

Any ideas on how to 'intercept' the hook installation and maybe find out what went wrong?
Posted 22-Jul-11 12:30pm
Updated 25-Apr-16 6:45am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

According to the documentation[^] AfxThrowMemoryException() occurs when you have run out of memory, so it looks like you have a bug somewhere that is consuming all your system resources.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You could try using a trapoline function to intercept the call to SetWindowsHookEx

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Thanks Roger Allen.

I used this:
HookImportFunctionByName v1.0[^]

and the trick was to set the module not to the EXE but to the module handle for MFC42?.DLL (obtained with GetModuleHandle() call) and to hook the function "SetWindowsHookExA" since I am building ANSI.

I can see the calls in the call stack to the interceptor function.

S_MySetWindowsHookEx(int 5, long (int, unsigned int, long)* 0x5f42f417 _AfxCbtFilterHook(int, unsigned int, long), HINSTANCE__ * 0x00000000, unsigned long 5204) line 140
AfxHookWindowCreate(CWnd * 0x003c7c30 {CMainFrame hWnd=0x00000000}) line 633 + 22 bytes
CWnd::CreateEx(unsigned long 0, const char * 0x001567d8, const char * 0x003c7a0c, unsigned long 13598720, int -2147483648, int -2147483648, int -2147483648, int -2147483648, HWND__ * 0x00000000, HMENU__ * 0x07e00667, void * 0x0012f8e8) line 705
CFrameWnd::Create(const char * 0x001567d8, const char * 0x003c7a0c, unsigned long 13598720, const tagRECT & {top=-2147483648 bottom=0 left=-2147483648 right=0}, CWnd * 0x00000000 {CWnd hWnd=???}, const char * 0x00000080, unsigned long 0, CCreateContext * 0x0012f8e8) line 568 + 79 bytes
CFrameWnd::LoadFrame(unsigned int 128, unsigned long 13598720, CWnd * 0x00000000 {CWnd hWnd=???}, CCreateContext * 0x0012f8e8) line 699 + 45 bytes
CDocTemplate::CreateNewFrame(CDocument * 0x003c6330 {CAlmPrtDoc}, CFrameWnd * 0x00000000 {CFrameWnd hWnd=???}) line 279 + 32 bytes
CSingleDocTemplate::OpenDocumentFile(const char * 0x0012fba8, int 1) line 132 + 17 bytes
CDocManager::OpenDocumentFile(const char * 0x003c624c) line 953
CWinApp::OpenDocumentFile(const char * 0x003c624c) line 93
CWinApp::ProcessShellCommand(CCommandLineInfo & {CCommandLineInfo}) line 40 + 26 bytes

Now I can capture the error code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 10 Jun 2016
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100