我想做的只是截获所有的WM_CREATE消息,然后告知我的主程序,可是我却只能监测到自己窗口的WM_CREATE消息,而其它程序的却不行。也放了共享段了呀。大家帮看看吧。
/////////////////////////////////////////////////////////////////////////////
// The one and only CNMWndMsgApp object
CNMWndMsgApp theApp;
#pragma data_seg("mysharedata")
static HHOOK g_myhook = NULL;
static HWND g_mylord = NULL;
static UINT g_msg = 0;
static HINSTANCE g_hinstance = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:mysharedata, rws")
NMWNDMSG_API LRESULT CALLBACK MessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0) // do not process message
return CallNextHookEx(g_myhook, nCode, wParam, lParam);
else
{
PCWPRETSTRUCT p = (PCWPRETSTRUCT) lParam;
if (WM_CREATE == p->message)
{
//PostMessage(g_mylord, g_msg, 1, (LPARAM) p->hwnd);
MessageBox(NULL, "a", "a", MB_OK);
TRACE("%d", wParam);
}
}
return CallNextHookEx(g_myhook, nCode, wParam, lParam);
}
NMWNDMSG_API BOOL InstallHook(HWND hwnd)
{
g_mylord = hwnd;
if (!g_msg)
{
g_msg = ::RegisterWindowMessage("_NMCLIENT19_PROCESS_COME");
if (!g_msg)
return FALSE;
}
if (!g_myhook)
{
g_myhook = SetWindowsHookEx(WH_CALLWNDPROCRET,
MessageProc, g_hinstance, 0);
if (!g_myhook)
return FALSE;
}
return TRUE;
}
NMWNDMSG_API void UninstallHook()
{
g_mylord = NULL;
g_msg = 0;
g_hinstance = NULL;
if (g_myhook)
{
UnhookWindowsHookEx(g_myhook);
g_myhook = 0;
}
}
BOOL CNMWndMsgApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
g_hinstance = AfxGetInstanceHandle();
return CWinApp::InitInstance();
}
int CNMWndMsgApp::ExitInstance()
{
UninstallHook();
return CWinApp::ExitInstance();
}
应用程序中怎么调用钩子的?