新建了一个MFC Extension DLL,然后在其中创建了一个对话框并为其新建了一个类CMyDlg.然后在Globals中设置全局钩子,想要在任何时候按下F3键后,跳出我所创建的对话框.代码如下:// AAA.cpp : Defines the initialization routines for the DLL.
//#include "stdafx.h"
#include <afxdllx.h>
#include "MyDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifHHOOK g_hKeyboard=NULL;此外在MyDlg.h中加入了#include "Resource.h".
编译通过.接下来新建一个基于对话框 的测试程序,发现两个问题:
1.当测试对话框在前台运行时(处于激活状态),按下F3键后会一次跳出两个我所建立的对话框
2.当测试对话框在前台运行时(处于非激活状态),按下F3键后程序跳红框报错,VC被强行关闭请问这是什么原因呢.此外想要实现这样的功能还有什么好办法呢???
HWND g_hWnd=NULL;extern "C" __declspec(dllexport) void SetHook(HWND hwnd);//导出函数
static AFX_EXTENSION_MODULE AAADLL = { 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("AAA.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(AAADLL, 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(AAADLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("AAA.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(AAADLL);
}
return 1; // ok
}
LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
if(wParam==VK_F3)
{
CMyDlg dlg;
dlg.DoModal();
return true;
}
return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
}void SetHook(HWND hwnd)
{
g_hWnd=hwnd;
g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("AAA"),0);
}
//#include "stdafx.h"
#include <afxdllx.h>
#include "MyDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifHHOOK g_hKeyboard=NULL;此外在MyDlg.h中加入了#include "Resource.h".
编译通过.接下来新建一个基于对话框 的测试程序,发现两个问题:
1.当测试对话框在前台运行时(处于激活状态),按下F3键后会一次跳出两个我所建立的对话框
2.当测试对话框在前台运行时(处于非激活状态),按下F3键后程序跳红框报错,VC被强行关闭请问这是什么原因呢.此外想要实现这样的功能还有什么好办法呢???
HWND g_hWnd=NULL;extern "C" __declspec(dllexport) void SetHook(HWND hwnd);//导出函数
static AFX_EXTENSION_MODULE AAADLL = { 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("AAA.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(AAADLL, 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(AAADLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("AAA.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(AAADLL);
}
return 1; // ok
}
LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
if(wParam==VK_F3)
{
CMyDlg dlg;
dlg.DoModal();
return true;
}
return CallNextHookEx(g_hKeyboard,code,wParam,lParam);
}void SetHook(HWND hwnd)
{
g_hWnd=hwnd;
g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("AAA"),0);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货