Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++MFC
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)
        AfxThrowMemoryException();
}
 
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 11:30am
Edited 22-Jul-11 23:54pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You could try using a trapoline function to intercept the call to SetWindowsHookEx
 
See http://research.microsoft.com/pubs/68568/huntusenixnt99.pdf[^]
  Permalink  
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Dnyaneshwar@Pune 1,300
1 Sergey Alexandrovich Kryukov 642
2 Tadit Dash 350
3 ravikhoda 300
4 Manas Bhardwaj 240
0 Sergey Alexandrovich Kryukov 11,271
1 OriginalGriff 6,865
2 Peter Leow 4,795
3 Abhinav S 3,838
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 3 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid