在MFC DLL代码中,SetWindowsHookEx失败。 [英] SetWindowsHookEx fails inside MFC DLL code.

查看:156
本文介绍了在MFC DLL代码中,SetWindowsHookEx失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 6.0的MFC代码中有一个函数



  void  AFXAPI AfxHookWindowCreate(CWnd * pWnd)





并安装一个Windows钩子来捕捉窗口创建。



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





遗憾的是,即使代码的调试版本也会抛出内存异常,而不是打印出最后一个错误代码。



这种情况​​发生在如此接近应用程序启动的情况下,如果正在调试应用程序,就会将cannott复制为问题。实现它的情况是如此奇怪,以至于我真的认为只知道错误代码可以帮助到这里。



还有其他人遇到这个失败的问题吗?



关于如何拦截钩子安装并找出问题所在的任何想法?

解决方案

< blockquote>根据文档 [ ^ ] AfxThrowMemoryException()<当你内存不足时会发生/ code>,所以看起来你的某个地方有一个消耗所有系统资源的bug。


你可以尝试使用trapoline函数截取对SetWindowsHookEx的调用



请参阅 http:/ /research.microsoft.com/pubs/68568/h untusenixnt99.pdf [ ^ ]


感谢Roger Allen。



我用过这个:

HookImportFunctionByName v1.0 [ ^ ]



,诀窍是将模块设置为EXE而不是模块句柄MFC42?.DLL(通过GetModuleHandle()调用获得)并挂钩函数SetWindowsHookExA,因为我正在构建ANSI。



我可以看到通话中的呼叫堆栈到拦截器函数。



 S_MySetWindowsHookEx(int 5,long(int,unsigned int,long)* 0x5f42f417 _AfxCbtFilterHook(int,unsigned int ,长),HINSTANCE__ * 0x00000000,无符号长5204)140行
AfxHookWindowCreate(CWnd * 0x003c7c30 {CMainFrame hWnd = 0 x00000000})行633 + 22字节
CWnd :: CreateEx(无符号长0,const字符* 0x001567d8,const字符* 0x003c7a0c,无符号长13598720,int -2147483648,int -2147483648,int -2147483648,int -2147483648 ,HWND__ * 0x00000000,HMENU__ * 0x07e00667,void * 0x0012f8e8)第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 = ???})第279行+ 32字节
CSingleDocTemplate :: OpenDocumentFile(const char * 0x0012fba8,int 1)第132行+17字节
CDocManager :: OpenDocumentFile(const char * 0x003c624c)第953行
CWinApp :: OpenDocumentFile(const char * 0x003c624c)第93行
CWinApp :: ProcessShellCommand(CCommandLineInfo& {CCommandLineInfo})第40 + 26字节





现在我可以捕获错误代码。


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?

解决方案

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.


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

See http://research.microsoft.com/pubs/68568/huntusenixnt99.pdf[^]


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.


这篇关于在MFC DLL代码中,SetWindowsHookEx失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆