请问高手:(200分)调试著名的画红色R钩子程序时,说m_hook.start未定义。郁闷。
(1).选择MFC AppWizard(DLL)创建项目ActiveKey,并选择MFC Extension DLL(共享MFC拷贝)类型(中文版第3项)。
(2).插入新文件ActiveKey.h,在其中输入如下代码:
#ifndef _KEYDLL_H
#define _KEYDLL_H
class AFX_EXT_CLASS CKeyHook:public CObject
{
public:
CKeyHook();
~CKeyHook();
HHOOK Start(); //安装钩子
BOOL Stop(); //卸载钩子
};
#endif
(3).在ActiveKey.cpp文件中加入声明"#include ActiveKey.h"。
(4).在ActiveKey.cpp文件中加入共享数据段,代码如下:
//Shared data section
#pragma data_seg("sharedata")
HHOOK glhHook=NULL; //钩子句柄。
HINSTANCE glhInstance=NULL; //DLL实例句柄。
#pragma data_seg()
(5).在ActiveKey.def文件中设置共享数据段属性,代码如下:
SETCTIONS
shareddata READ WRITE SHARED
(6).在ActiveKey.cpp文件中加入CkeyHook类的实现代码和钩子函数代码:
//键盘钩子处理函数。
extern "C" LRESULT WINAPI KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if( nCode >= 0 )
{
if( wParam == 0X79 )//当按下F10键时,激活外挂。
{
//外挂实现代码。
CPoint newPoint,oldPoint;
GetCursorPos(&oldPoint);
newPoint.x = oldPoint.x+40;
newPoint.y = oldPoint.y+10;
SetCursorPos(newPoint.x,newPoint.y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//模拟按下鼠标左键。
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//模拟放开鼠标左键。
keybd_event(VK_SHIFT,MapVirtualKey(VK_SHIFT,0),0,0); //按下SHIFT键。
keybd_event(0x52,MapVirtualKey(0x52,0),0,0);//按下R键。
keybd_event(0x52,MapVirtualKey(0x52,0),KEYEVENTF_KEYUP,0);//放开R键。
keybd_event(VK_SHIFT,MapVirtualKey(VK_SHIFT,0),KEYEVENTF_KEYUP,0);//放开SHIFT键。
SetCursorPos(oldPoint.x,oldPoint.y);
}
}
return CallNextHookEx(glhHook,nCode,wParam,lParam);
}
CKeyHook::CKeyHook(){}
CKeyHook::~CKeyHook()
{
if( glhHook )
Stop();
}
//安装全局钩子。
HHOOK CKeyHook::Start()
{
glhHook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,glhInstance,0);//设置键盘钩子。
return glhHook;
}
//卸载全局钩子。
BOOL CKeyHook::Stop()
{
BOOL bResult = TRUE;
if( glhHook )
bResult = UnhookWindowsHookEx(glhHook);//卸载键盘钩子。
return bResult;
}
(7).修改DllMain函数,代码如下:
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
//如果使用lpReserved参数则删除下面这行
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("NOtePadHOOK.DLL Initializing!\n");
//扩展DLL仅初始化一次
if (!AfxInitExtensionModule(ActiveKeyDLL, hInstance))
return 0;
new CDynLinkLibrary(ActiveKeyDLL);
//把DLL加入动态MFC类库中
glhInstance = hInstance;
//插入保存DLL实例句柄
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("NotePadHOOK.DLL Terminating!\n");
//终止这个链接库前调用它
AfxTermExtensionModule(ActiveKeyDLL);
}
return 1;
}
(8).编译项目ActiveKey,生成ActiveKey.DLL和ActiveKey.lib。
接着,我们还需要创建一个外壳程序将全局钩子安装了Windows系统中,这个外壳程序编写步骤如下:
(1).创建一个对话框模式的应用程序,项目名为Simulate。
(2).在主对话框中加入一个按钮,使用ClassWizard为其创建CLICK事件。
(3).将ActiveKey项目Debug目录下的ActiveKey.DLL和ActiveKey.lib拷贝到Simulate项目目录下。
(4).从“工程”菜单中选择“设置”,弹出Project Setting对话框,选择Link标签,在“对象/库模块”中输入ActiveKey.lib。
(5).将ActiveKey项目中的ActiveKey.h头文件加入到Simulate项目中,并在Stdafx.h中加入#include ActiveKey.h。
(6).在按钮单击事件函数输入如下代码:
void CSimulateDlg::OnButton1()
{
// TODO: Add your control notification handler code here
if( !bSetup )
{
m_hook.Start();//激活全局钩子。
}
else
{
m_hook.Stop();//撤消全局钩子。
}
bSetup = !bSetup;
}
--------------------------------------------------------------------------------------------------------------------------------------------------
E:\vc++\MyProjects\Simulate\SimulateDlg.cpp(179) : error C2065: 'm_hook' : undeclared identifier
E:\vc++\MyProjects\Simulate\SimulateDlg.cpp(179) : error C2228: left of '.Start' must have class/struct/union type
E:\vc++\MyProjects\Simulate\SimulateDlg.cpp(183) : error C2228: left of '.Stop' must have class/struct/union type
Generating Code...
Error executing cl.exe.
Simulate.exe - 3 error(s), 0 warning(s)
-------------------------------------------------------------------------------------------------------------------------------------------------
加入
class CSimulateDlg : public CDialog
{
private:
//CActiveKey m_hook;//加入钩子类作为数据成员
时出现9个错误。
问题点数:0、回复次数:11Top
1 楼enoloo(在水一方)回复于 2004-04-04 17:20:37 得分 0
关注。Top
2 楼enoloo(在水一方)回复于 2004-04-04 17:25:08 得分 0
SimulateDlg.h中加 Stdafx.h
9个什么错误?Top
3 楼rivershan(阿门)回复于 2004-04-04 17:51:28 得分 0
著名的画红色R钩子程序
没见过~Top
4 楼tan4(tan4)回复于 2004-04-04 18:34:31 得分 0
谢谢SimulateDlg.h中加 Stdafx.h
但加了也不行。
e:\vc++\myprojects\simulate\simulatedlg.h(18) : error C2146: syntax error : missing ';' before identifier 'm_hook'
e:\vc++\myprojects\simulate\simulatedlg.h(18) : error C2501: 'CActiveKey' : missing storage-class or type specifiers
e:\vc++\myprojects\simulate\simulatedlg.h(18) : error C2501: 'm_hook' : missing storage-class or type specifiers
E:\vc++\MyProjects\Simulate\SimulateDlg.cpp(179) : error C2065: 'm_hook' : undeclared identifier
E:\vc++\MyProjects\Simulate\SimulateDlg.cpp(179) : error C2228: left of '.Start' must have class/struct/union type
E:\vc++\MyProjects\Simulate\SimulateDlg.cpp(183) : error C2228: left of '.Stop' must have class/struct/union type
Generating Code...
Compiling...
Simulate.cpp
e:\vc++\myprojects\simulate\simulatedlg.h(18) : error C2146: syntax error : missing ';' before identifier 'm_hook'
e:\vc++\myprojects\simulate\simulatedlg.h(18) : error C2501: 'CActiveKey' : missing storage-class or type specifiers
e:\vc++\myprojects\simulate\simulatedlg.h(18) : error C2501: 'm_hook' : missing storage-class or type specifiers
Generating Code...
Error executing cl.exe.
Simulate.exe - 9 error(s), 0 warning(s)
Top
5 楼fzd999(花差花差)回复于 2004-04-06 17:32:30 得分 0
1 #include "ActiveKey.h" //加到你的对话框头文件里面去
2 AFX_EXT_CLASS是否是正确的使用?Top
6 楼fzd999(花差花差)回复于 2004-04-06 17:33:25 得分 0
你的类并没有正确的导出,而且包含头文件的位置也不正确,所以导致这样的错误Top
7 楼icetears(冰咖啡)回复于 2004-04-06 19:55:02 得分 0
学习Top
8 楼tan4(tan4)回复于 2004-04-07 11:08:22 得分 0
To: fzd999(花差花差)
应该怎么做呢?请指教。贡献您我仅有的400分。谢谢了。Top
9 楼tan4(tan4)回复于 2004-04-07 13:47:52 得分 0
看看我的全部代码:
1:Activekey.cpp
// ActiveKey.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include <afxdllx.h>
#include "ActiveKey.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Shared data section
#pragma data_seg("sharedata")
HHOOK glhHook=NULL; //钩子句柄。
HINSTANCE glhInstance=NULL; //DLL实例句柄。
#pragma data_seg()
#pragma comment(linker, "/section:sharedata,rws")
static AFX_EXTENSION_MODULE ActiveKeyDLL = { NULL, NULL };
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("NotepadHook.DLL Initializing!\n");
//TRACE0("ACTIVEKEY.DLL Initializing!\n");
//原来的
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(ActiveKeyDLL, hInstance))
return 0;
// Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
// an MFC Regular DLL (such as an ActiveX Control)
// instead of an MFC application, then you will want to
// remove this line from DllMain and put it in a separate
// function exported from this Extension DLL. The Regular DLL
// that uses this Extension DLL should then explicitly call that
// function to initialize this Extension DLL. Otherwise,
// the CDynLinkLibrary object will not be attached to the
// Regular DLL's resource chain, and serious problems will
// result.
new CDynLinkLibrary(ActiveKeyDLL);
//把DLL加入动态MFC类库中
glhInstance = hInstance;
//插入保存DLL实例句柄
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("NotePadHook.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(ActiveKeyDLL);
}
return 1; // ok
}
extern "C" LRESULT WINAPI KeyboardProc(int nCode,WPARAM wParam,LPARAM
lParam)
{
//1
if( nCode >= 0 )
{
//2
if( wParam == 0X79 )//当按下F10键时,激活外挂。
{
//3
//外挂实现代码。
CPoint newPoint,oldPoint;
GetCursorPos(&oldPoint);
newPoint.x = oldPoint.x+40;
newPoint.y = oldPoint.y+10;
SetCursorPos(newPoint.x,newPoint.y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//模拟按下鼠标左键。
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//模拟放开鼠标左键。
keybd_event(VK_SHIFT,MapVirtualKey(VK_SHIFT,0),0,0); //按下SHIFT键。
keybd_event(0x52,MapVirtualKey(0x52,0),0,0);//按下R键。
keybd_event(0x52,MapVirtualKey(0x52,0),KEYEVENTF_KEYUP,0);//放开R键。
keybd_event(VK_SHIFT,MapVirtualKey(VK_SHIFT,0),KEYEVENTF_KEYUP,0);//放开SHIFT键。
SetCursorPos(oldPoint.x,oldPoint.y);
}
//3
}
//2
return CallNextHookEx(glhHook,nCode,wParam,lParam);
}
//1
CKeyHook::CKeyHook()
{}
CKeyHook::~CKeyHook()
{ if( glhHook )
Stop();
}
//安装全局钩子。
HHOOK CKeyHook::Start()
{
glhHook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,glhInstance,0);//设置键盘钩子。
return glhHook;
}
//卸载全局钩子。
BOOL CKeyHook::Stop()
{
BOOL bResult = TRUE;
if( glhHook )
bResult = UnhookWindowsHookEx(glhHook);//卸载键盘钩子。
return bResult;
}
2: .h
//#ifndef _KEYDLL_H
//#define _KEYDLL_H
class AFX_EXT_CLASS CKeyHook:public CObject
{
public:
CKeyHook();
~CKeyHook();
HHOOK Start();
//安装钩子
BOOL Stop(); //卸载钩子
};
//#endif
3: .def
; ActiveKey.def : Declares the module parameters for the DLL.
LIBRARY "ActiveKey"
DESCRIPTION 'ActiveKey Windows Dynamic Link Library'
SECTIONS
Shareddata READ WRITE SHARED
EXPORTS
; Explicit exports can go here
4:编译
Linking...
ActiveKey.dll - 0 error(s), 0 warning(s)
5:copy .dll .h .lib到Simulate的目录
6:建立Simulate的MFC(EXE)---DIG---删除原有的3个控件----添加一个按钮
7:建立单击事件::OnButton1()
8:全部code---.cpp
// SimulateDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Simulate.h"
Top
10 楼tan4(tan4)回复于 2004-04-07 13:48:36 得分 0
接上:
#include "SimulateDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimulateDlg dialog
CSimulateDlg::CSimulateDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimulateDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSimulateDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSimulateDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSimulateDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSimulateDlg, CDialog)
//{{AFX_MSG_MAP(CSimulateDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimulateDlg message handlers
BOOL CSimulateDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CSimulateDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSimulateDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSimulateDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSimulateDlg::OnButton1()
{
// TODO: Add your control notification handler code here
if(!bSetup)
{
m_hook.Start();//激活全局钩子。
}
else
{
m_hook.Stop();//撤消全局钩子。
}
bSetup = !bSetup;
}
}
9:从“工程”菜单中选择“设置”,弹出Project Setting对话框,选择Link标签,在“对象/库模块”中输入ActiveKey.lib。
10:#include "ActiveKey.h" //加到我的对话框头文件里面去
11:编译Simulate.exe - 9 error(s), 0 warning(s)
12:加入private:
CActiveKey m_hook;//加入钩子类作为数据成员
//在class CSimulateDlg : public CDialog中
:\program files\microsoft visual studio\myprojects\simulate\activekey.h(3) : error C2011: 'CKeyHook' : 'class' type redefinition
c:\program files\microsoft visual studio\myprojects\simulate\simulatedlg.h(18) : error C2146: syntax error : missing ';' before identifier 'm_hook'
c:\program files\microsoft visual studio\myprojects\simulate\simulatedlg.h(18) : error C2501: 'CActiveKey' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\simulate\simulatedlg.h(18) : error C2501: 'm_hook' : missing storage-class or type specifiers
SimulateDlg.cpp
c:\program files\microsoft visual studio\myprojects\simulate\activekey.h(3) : error C2011: 'CKeyHook' : 'class' type redefinition
c:\program files\microsoft visual studio\myprojects\simulate\simulatedlg.h(18) : error C2146: syntax error : missing ';' before identifier 'm_hook'
c:\program files\microsoft visual studio\myprojects\simulate\simulatedlg.h(18) : error C2501: 'CActiveKey' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\simulate\simulatedlg.h(18) : error C2501: 'm_hook' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\MyProjects\Simulate\SimulateDlg.cpp(176) : error C2065: 'bSetup' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Simulate\SimulateDlg.cpp(178) : error C2065: 'm_hook' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Simulate\SimulateDlg.cpp(178) : error C2228: left of '.Start' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\Simulate\SimulateDlg.cpp(182) : error C2228: left of '.Stop' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\Simulate\SimulateDlg.cpp(188) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\Simulate\SimulateDlg.cpp(188) : error C2143: syntax error : missing ';' before '}'
C:\Program Files\Microsoft Visual Studio\MyProjects\Simulate\SimulateDlg.cpp(188) : error C2143: syntax error : missing ';' before '}'
Generating Code...
Error executing cl.exe.
Simulate.exe - 15 error(s), 0 warning(s)
Top
11 楼fzd999(花差花差)回复于 2004-04-07 20:25:40 得分 0
这么多代码,实际上问题可能不是代码导致的。
看起来,应该是你的头文件包含的次序和位置出了问题,你可以尝试着变化ActiveKey.h的包含位置,假如问题依然存在,那么把工程发给我来看看,fzd999@jxfw.comTop




