钩子问题,大家来帮我看看

ppyy 2002-07-24 05:13:45
是一个键盘钩子,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的对话框,但是我安装钩子后在金山词霸里按键盘却没有反映?
高手帮我看看
哪里有问题
...全文
9 点赞 收藏 1
写回复
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ppyy 2002-07-24
钩子函数根本没有执行
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告