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

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的对话框,但是我安装钩子后在金山词霸里按键盘却没有反映?
高手帮我看看
哪里有问题
...全文
60 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ppyy 2002-07-24
  • 打赏
  • 举报
回复
钩子函数根本没有执行

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧