关于钩子的问题
刚刚使用钩子编程,很多地方需要学习。
刚编写了一个钩子,是用来获得鼠标的当前位置的(代码如下)。可是发现一些问题。
鼠标在加载钩子的exe上时可以显示鼠标的位置,否则就没有捕获到鼠标的问题。希望高手指导一下:)
DLL代码:使用MFC Static DLL
// Hook.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "Hook.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHookApp
BEGIN_MESSAGE_MAP(CHookApp, CWinApp)
//{{AFX_MSG_MAP(CHookApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHookApp construction
CHookApp::CHookApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CHookApp object
#pragma data_seg("mydata")
CHookApp theApp;
POINT m_sPoint;
HHOOK m_sHook;
HWND m_sWnd;
#pragma data_seg()
static LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(wParam==WM_MOUSEMOVE||wParam==WM_NCMOUSEMOVE)
{
MOUSEHOOKSTRUCT* pMsg = (MOUSEHOOKSTRUCT*)lParam;
TRACE("%X, %X\n", m_sWnd, pMsg->hwnd);
m_sPoint = ((MOUSEHOOKSTRUCT*)lParam)->pt;
//取鼠标信息
::SendMessage(m_sWnd, WM_USER+1, m_sPoint.x, m_sPoint.y);
}
return CallNextHookEx(m_sHook,nCode,wParam,lParam);
}
void __declspec(dllexport) WINAPI Install(HWND hWnd)
{
m_sWnd = hWnd;
m_sHook = SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandle("hook.dll"), NULL);
//m_sHook = SetWindowsHookEx(WH_MOUSE, MouseProc, theApp.m_hInstance, 0);
}
void __declspec(dllexport) WINAPI UnInstall()
{
if(m_sHook)
UnhookWindowsHookEx(m_sHook);
}
//.h
// Hook.h : main header file for the HOOK DLL
//
#if !defined(AFX_HOOK_H__51B0782C_A177_4BA0_B380_C50AD56ED9A3__INCLUDED_)
#define AFX_HOOK_H__51B0782C_A177_4BA0_B380_C50AD56ED9A3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CHookApp
// See Hook.cpp for the implementation of this class
//
class CHookApp : public CWinApp
{
public:
CHookApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHookApp)
//}}AFX_VIRTUAL
//{{AFX_MSG(CHookApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
void __declspec(dllexport) WINAPI Install(HWND hWnd);
void __declspec(dllexport) WINAPI UnInstall();
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_HOOK_H__51B0782C_A177_4BA0_B380_C50AD56ED9A3__INCLUDED_)
//.DEF
; Hook.def : Declares the module parameters for the DLL.
LIBRARY "Hook"
DESCRIPTION 'Hook Windows Dynamic Link Library'
SETCTIONS
mydata READ WRITE SHARED
EXPORTS
Install
UnInstall
; Explicit exports can go here
//测试部分.exe 使用SDI,只有CView修改了代码。工程中加入hook.lib和对应的hook.h文件
#include "Hook.h"
int CTestHookView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
Install(m_hWnd);
return 0;
}
void CTestHookView::OnDestroy()
{
UnInstall();
CView::OnDestroy();
}
UINT CTestHookView::OnHookMsg(WPARAM wParam, LPARAM lParam)//对应的消息:
//#define WM_HOOKMSG (WM_USER+1)
//
//ON_MESSAGE(WM_HOOKMSG, OnHookMsg)
{
m_sPoint.x = wParam;
m_sPoint.y = lParam;
Invalidate();
return 0;
}
void CTestHookView::OnDraw(CDC* pDC)
{
CTestHookDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString strText;
strText.Format("x = %d, y = %d", m_sPoint.x, m_sPoint.y);
pDC->TextOut(10,10, strText);
}