钩子问题,大家来帮我看看
是一个键盘钩子,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的对话框,但是我安装钩子后在金山词霸里按键盘却没有反映?
高手帮我看看
哪里有问题
#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的对话框,但是我安装钩子后在金山词霸里按键盘却没有反映?
高手帮我看看
哪里有问题
...全文
相关推荐
c++ hook 全局钩子 全局键盘钩子
正确地运用全局钩子,可以实现强大的功能,如键盘监听、输入过滤等,但同时也要注意其可能带来的性能影响和安全问题。
C++钩子技术深入学习与问题分析指南
本文还有配套的精品资源,点击获取 简介:C++中的"钩子"技术使程序员能够在系统或应用程序的关键点插入代码,用于监控、修改或处理特定事件。本指南深入讲解钩子的工作原理,包括系统级、线程级和DLL级钩子的设置方法,并分析代码执行两次的可能原因。文章通过"HookDLL"示例,指导读者如何定义、安装和卸载钩子函数,并解释了消息循环和钩子回调的实现细节。 1. 钩子技术...
编写钩子程序实例 钩子函数、安装钩子和卸载钩子。
【编写钩子程序实例】钩子函数、安装钩子和卸载钩子是Windows编程中用于拦截和处理系统事件的关键技术。
钩子查看器
钩子在计算机编程中是一种技术,允许程序拦截和处理特定事件,例如键盘输入、鼠标点击或者系统消息。通过使用钩子查看器,用户可以深入了解系统运行时的行为,识别潜在的安全问题或性能瓶颈。
解决Vue中mounted钩子函数获取节点高度出错问题
Vue 中 mounted 钩子函数获取节点高度出错问题解决方案在 Vue 中使用 mounted 钩子函数获取节点高度时,可能会遇到错误的 offsetTop 值的问题。


