WH_JOURNALRECORD钩子无法正常使用!

一名程序员 2008-11-26 10:48:08
我做了一个WH_JOURNALRECORD的钩子。是放在一个dll中的。一运行链接此dll的应用程序,程序就死掉。后来在msdn上看到这样一句话:
[Quote=msdn2001]Unlike most other global hook procedures, the JournalRecordProc and JournalPlaybackProc hook procedures are always called in the context of the thread that set the hook.[/Quote]
疑是我调用该钩子的上下文不对。但又搞不清到底怎么回事?
我的主要代码如下:
// HookDll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "HookDll.h"
#include "stdio.h"
0
struct {
HHOOK m_hHook_WH_JOURNALRECORD;
HANDLE m_hInstance;
}g_data;

FILE *g_logFile_WH_JOURNALRECORD;

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_data.m_hInstance = hModule;
g_logFile_WH_JOURNALRECORD = fopen("log_WH_JOURNALRECORD.txt", "w+t");
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
fclose(g_logFile_WH_JOURNALRECORD);
break;
}
return TRUE;
}


// This is an example of an exported variable
HOOKDLL_API int nHookDll=0;

// This is an example of an exported function.
HOOKDLL_API int fnHookDll(void)
{
return 42;
}

// This is the constructor of a class that has been exported.
// see HookDll.h for the class definition
CHookDll::CHookDll()
{
return;
}
LRESULT HOOKDLL_API CALLBACK MyHookProc_WH_JOURNALRECORD(
int nCode,
WPARAM wParam,
LPARAM lParam
);
void SetHook()
{
g_data.m_hHook_WH_JOURNALRECORD = SetWindowsHookEx(WH_JOURNALRECORD,
MyHookProc_WH_JOURNALRECORD,
(HINSTANCE)g_data.m_hInstance,
0);
}

void UnSetHook()
{
if ( g_data.m_hHook_WH_JOURNALRECORD )
{
UnhookWindowsHookEx(g_data.m_hHook_WH_JOURNALRECORD);
}
}

LRESULT HOOKDLL_API CALLBACK MyHookProc_WH_JOURNALRECORD(
int nCode,
WPARAM wParam,
LPARAM lParam
)
{
//TRACE("HOOK!%d %d %d\n",nCode, wParam, lParam);
if (nCode == HC_ACTION )
{

PEVENTMSG pEventMsg = (PEVENTMSG)lParam;
char buf[50] = "";
sprintf(buf,"%p %u %u %u %d\n",
pEventMsg->hwnd,
pEventMsg->message,
pEventMsg->paramH,
pEventMsg->paramL,
pEventMsg->time);

WM_PAINT;
fwrite(buf,sizeof(char),strlen(buf),g_logFile_WH_JOURNALRECORD);
}
return CallNextHookEx(g_data.m_hHook_WH_JOURNALRECORD, nCode,
wParam, lParam);
}

欢迎同行批评指正!
...全文
137 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

15,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧