钩子问提,一共120分,大家来看看
ppyy 2002-07-24 05:52:39 是一个键盘钩子,MFC标准动态连接库
#define DllExport __declspec(dllexport)
DllExport void WINAPI InstallHook(); //引出函数
///////////////////////////////////////////////////////
以下是DLL的CPP文件
//////////////////////////////////////////////////////
#include "stdafx.h"
#include "hookdll.h"
#include <tlhelp32.h>
#define targetFile "XDICT.EXE"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CHookdllApp
BEGIN_MESSAGE_MAP(CHookdllApp, CWinApp)
//{{AFX_MSG_MAP(CHookdllApp)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CHookdllApp construction
CHookdllApp::CHookdllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CHookdllApp object
CHookdllApp theApp;
HHOOK Hook;
DWORD GetProcessId()
{
DWORD Pid=-1;
HANDLE hSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0 PROCESSENTRY32 lPrs;
ZeroMemory(&lPrs,sizeof(lPrs));
lPrs.dwSize=sizeof(lPrs);
Process32First(hSnap,&lPrs);
if (strstr(targetFile,lPrs.szExeFile
{
Pid=lPrs.th32ProcessID;
return Pid;
}
while(1)
{
ZeroMemory(&lPrs,sizeof(lPrs));
lPrs.dwSize=(&lPrs,sizeof(lPrs));
if (!Process32Next(hSnap,&lPrs))//¼ÌÐøÃ¶¾Ù½ø³ÌÐÅÏ¢
{
Pid=-1;
break;
}
if (strstr(targetFile,lPrs.szExeFile))
{
Pid=lPrs.th32ProcessID;
break;
}
}
return Pid;
}
LRESULT CALLBACK MyHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
::MessageBox(NULL,"test","test",MB_ICONINFORMATION);/*这里没反应,这句没有执行*/
LRESULT Result=CallNextHookEx(Hook,nCode,wParam,lParam);
return Result;
}
DllExport void WINAPI InstallHook()
{
DWORD ThreadId=GetProcessId();
char tt[100];
ZeroMemory(tt,100);
itoa(ThreadId,tt,10);
::MessageBox(NULL,tt,"test",MB_ICONINFORMATION); /*这里正常,显示正确得到的ThreadID 正确*/
Hook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)MyHookProc, theApp.m_hInstance, ThreadId); //安装HOOK
}
按照我的程序,在金山词霸里输入按键盘输入的时候,应该弹出一个内容是test的对话框,但是我安装钩子后在金山词霸里按键盘却没有反映?
高手帮我看看
哪里有问题
钩子函数好像跟本没执行