// HookDllDemo.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include <afxdllx.h>

#include <windows.h> //引入windows头文件

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// 建立数据段
#pragma data_seg("HookData")
HHOOK g_hHook = NULL;   // 钩子句柄
HINSTANCE g_hInstDLL = NULL; // 模块句柄
#pragma data_seg()
//设置数据段为可读可写可共享
#pragma comment(linker,"/SECTION:HookData,RWS")

static AFX_EXTENSION_MODULE HookDllDemoDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)//入口函数
{
 // Remove this if you use lpReserved
 UNREFERENCED_PARAMETER(lpReserved);

if (dwReason == DLL_PROCESS_ATTACH)
 {
  TRACE0("HOOKDLLDEMO.DLL Initializing!/n");
  
  // Extension DLL one-time initialization
  if (!AfxInitExtensionModule(HookDllDemoDLL, hInstance))
   return 0;

// Insert this DLL into the resource chain
  // NOTE: If this Extension DLL is being implicitly linked to by
  //  an MFC Regular DLL (such as an ActiveX Control)
  //  instead of an MFC application, then you will want to
  //  remove this line from DllMain and put it in a separate
  //  function exported from this Extension DLL.  The Regular DLL
  //  that uses this Extension DLL should then explicitly call that
  //  function to initialize this Extension DLL.  Otherwise,
  //  the CDynLinkLibrary object will not be attached to the
  //  Regular DLL's resource chain, and serious problems will
  //  result.

new CDynLinkLibrary(HookDllDemoDLL);

g_hInstDLL = hInstDLL; // 初始化模块句柄

}
 else if (dwReason == DLL_PROCESS_DETACH)
 {
  TRACE0("HOOKDLLDEMO.DLL Terminating!/n");
  // Terminate the library before destructors are called
  AfxTermExtensionModule(HookDllDemoDLL);
 }
 return 1;   // ok
}

// 钩子回调函数
LRESULT WINAPI HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 // TODO: 在这里添加响应动作
 
 // 将事件传递到下一个钩子
 return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

// 安装钩子
extern "C" __declspec(dllexport) DWORD InstallHook(void)
{
 DWORD dwRet = 0;
 /*加载钩子类别*/
 g_hHook = SetWindowsHookEx(WH_MOUSE/*WH_SHELL*/, HookProc, g_hInstDLL, 0); 
 if(g_hHook == NULL)
  dwRet = GetLastError();
 return dwRet;
}

// 卸载钩子
extern "C" __declspec(dllexport) DWORD KillHook(void)
{
 if(UnhookWindowsHookEx(g_hHook))
  return 0;
 else
  return GetLastError();
}

VC++钩子DLL框架代码(MFC Extension DLL using shared MFC DLL)相关推荐

  1. VC++动态链接库(DLL)编程(二)--非MFC DLL

    4.非MFC DLL 4.1一个简单的DLL 第2节给出了以静态链接库方式提供add函数接口的方法,接下来我们来看看怎样用动态链接库实现一个同样功能的add函数. 如图6,在VC++中new一个Win ...

  2. fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC sha

    调试程序时出现以下问题:d:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(24): fatal e ...

  3. Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version~~~~

    转自:http://www.byywee.com/page/M0/S530/530822.html 昨天编译文件时出现了Building MFC application with /MD[d] (CR ...

  4. Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version 错误解决

    Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version 错误解决 今天在VS201 ...

  5. afx.h(24): fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requi

    问题: 在c++/cli控制台项目中,因为要使用cstdiofile类,所有包含了afx.h,但是报如下错误: "afx.h(24): fatal error C1189: #error : ...

  6. 使用AheadLib生成DLL劫持代码

    程序加载DLL会有一个优先级顺序,其中,最先加载的是同一目录下的DLL,DLL劫持的原理是在程序的同目录下放一个自己写的DLL,让程序将我们伪造的DLL加载进去,然后在我们的DLL里调用真正DLL的函 ...

  7. Building MFC application with /MD[d] (CRT dll vers

    fatal error C1189: #error :  Building MFC application with /MD[d] (CRT dll version) requires MFC sha ...

  8. MFC编译错误:#error: Building MFC application with /MD[d\] (CRT dll version) requires MFC shared dll vers

    编译错误:#error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version ...

  9. VS2017 SLAM SDK开发踩坑系列(1)代码报错:libeay32.dll‘. Cannot find or open the PDB file. Ssleay32.dll Not Found

    项目场景: 提示:这里简述项目相关背景: 思岚激光雷达发布了vs2017 sdk开发包. 开发过程中,调用外部库函数时,会报错 Ssleay32.dll Not Found libeay32.dll' ...

最新文章

  1. 手把手教会你小程序登录鉴权
  2. 技术分享|单元测试推广与实战-在全新的DDD架构上进行单元测试
  3. 一个销售精英拜访客户的6大绝招,胜过10次培训,实用!
  4. CGO磁盘管理For流星无语
  5. 【Java定时任务调度工具】Timer
  6. 三、索引优化(5)索引设计指南
  7. Day01-计算机入门
  8. CSDN 文章自动显示全文
  9. linux常见的三种shell,几种常见的Shell
  10. 输入法辅助工具:自动切换输入法 for Mac
  11. 网络寻宝 v2.2 官网
  12. 计算机网络——常见协议
  13. 小米手机qq不读取相册照片_小米手机qq保存的图片找不到
  14. PQ分区魔术师调整硬盘分区大小
  15. 1467 acm 矩阵不同行列的和
  16. windowsXPsp3恢复桌面IE图标
  17. redis数据库创建
  18. 微信中的个性化广告怎么关闭的
  19. 【azkaban】开启进程秒退
  20. Ubuntu12.10 使用JLink连接开发板用arm-gdb调试ARM程序

热门文章

  1. java query接口_「软帝学院」Java零基础学习详解
  2. python request.get乱码_python的request中文乱码怎么办
  3. python 输出字符串编码_Python print 字符串编码问题
  4. python数字加密解密_Python对整形数字进行加密和解密
  5. python异常包_python异常处理与导入模块与导入包
  6. 大战设计模式【17】—— 建造者模式
  7. Lotus Sametime 服务器的安装和配置
  8. linux(centos)下mysql忘记root密码
  9. 和我一起学Windows Workflow Foundation(1)-----创建和调试一个WF实例(转)
  10. msm8953+android8.1接听电话时声音由默认听筒输出改为外放输出