我仍在与钩子(Hook)作斗争。
我的目标是:
- 在 notepad.exe 中设置 Hook
- 对其进行子类化(我的最终目标是对 Edit 类进行子类化并在我自己的窗口中显示内容)
免责声明:我知道有更简单的方法可以从记事本获取文本/内容,但这是我学习 C、winapi、子类化和钩子(Hook)的一种方法。
我的问题是 SetWindowLongPtr始终返回 ERROR_ACCESS_DENIED 错误(代码 5)。
2013 年 5 月 22 日:此问题已修复! 问题是 SetWindowLongPtr 位置错误。它必须在 GetMsgProc 函数内部。
问题变得有点长和困惑,所以我重写了问题(使用更新的代码)
现在的问题是,当目标是 notepad.exe 时,不调用 GetMsgProc。如果我将目标更改为 simple.exe,GetMsgProc 就会被调用并起作用!
(Simple.exe 只是一个简单的 GUI):
代码如下所示:
exe.cpp
#include <windows.h>
#include "Resource.h"
#include <stdlib.h>
#include "stdafx.h"
#include <strsafe.h>
#include "C:\Users\Kristensen\Documents\Visual Studio 2012\Projects\Win32D\dll\dllHeader.h"
//---------------------------------------------------------------------------
HWND hWnd;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
hookNotepad();
return TRUE;
case IDCANCEL:
removeHook();
EndDialog(hWndDlg, 0);
}
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
dllHeader.h
#ifdef DLLAPI
#else
#define DLLAPI extern "C" __declspec(dllimport)
#endif
DLLAPI bool hookNotepad();
DLLAPI bool removeHook();
dll.cpp:
#include "stdafx.h"
#include <windows.h>
#define DLLAPI extern "C" __declspec(dllexport)
#include "dllHeader.h"
// shared variables
#pragma data_seg("Shared")
HHOOK g_hHook = NULL; // Hook for Notepad
HWND npHWND = NULL; // Notepad handle
#pragma data_seg()
#pragma comment(linker, "/section:Shared,rws")
// Forward references
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) ;
LRESULT CALLBACK NewWndProc(HWND Hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
//LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) ;
LONG OldWndProc;
DWORD pid;
HINSTANCE g_hInstDll = NULL; // DllMain entry (DLL_PROCESS_ATTACH)
DWORD npThreadId = NULL; // Notepad thread ID
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) //Testing with CBTProc - same issues as with GetMsgProc.
{
//If I hook notepad.exe, I never get called. (silence)
//If I hook simple.exe, I get called (Beep beep!)
// make some noise
static DWORD dwTickKeep = 0;
if ((GetTickCount()-dwTickKeep)>300)
{ dwTickKeep = GetTickCount();
Beep(2000, 100);
}
//Subclassing......
//For simple.exe: (working)
//HWND hwndEdit = ::FindWindowEx(npHWND,NULL,TEXT("WindowsForms10.RichEdit20W.app.0.2bf8098_r14_ad1"), NULL);
//For notepad.exe: (not working)
HWND hwndEdit = ::FindWindowEx(npHWND,NULL,TEXT("Edit"), NULL);
if (hwndEdit)
{
//Subclass it
OldWndProc = GetWindowLongPtr(hwndEdit, GWLP_WNDPROC);
SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)NewWndProc);
}
return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
}
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hInstDll = hModule;
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
bool hookNotepad ()
{
// If target is running
// if (npHWND = FindWindow(NULL, TEXT("simpleGUI")))
if (npHWND = FindWindow(TEXT("Notepad"), NULL))
{
// Finds the ThreadID for target. We use this in SetWindowsHookEx
npThreadId = GetWindowThreadProcessId(npHWND, &pid);
// Sets the hook in target
g_hHook = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, g_hInstDll, npThreadId);
//g_hHook = SetWindowsHookEx(WH_CBT, CBTProc, g_hInstDll, npThreadId);
// If the hook succesed
if (g_hHook)
{
////Add a menu in the notepad.exe, but not relevant for subclassing notepads edit class...
//HMENU hCurrent = GetMenu(npHWND); //Get the CURRENT menu of the window.
//HMENU hNew = CreateMenu(); //Create a new one.
//AppendMenu(hCurrent, MF_STRING | MF_POPUP, (unsigned int)hNew, TEXT("myMenu"));
//AppendMenu(hNew, MF_STRING, 2000, L"myButton"); //2000 is the ID of the new button.
//DrawMenuBar(npHWND); //redraw the Menu.
//Force a msg to the messagequeue, so that the hook function(GetMsgProc) gets called
PostThreadMessage(npThreadId, WM_NULL, 0, 0);
return 1;
}
return 0;
}
else
//Notepad is not running
return 0;
}
bool removeHook()
{
// Removes the hook
if (g_hHook != NULL)
{
UnhookWindowsHookEx(g_hHook);
g_hHook = NULL;
}
return 0;
}
LRESULT CALLBACK NewWndProc(HWND Hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
//We should come here and should be able to read the text from the Edit class...
return CallWindowProc((WNDPROC)OldWndProc, Hwnd, Message, wParam, lParam);
}
非常感谢任何提示、评论或技巧...
最佳答案
您的代码中有 3 个问题:
(1) 变量 HWND npHWND
旨在在主机 exe 和记事本 exe 之间共享,因此必须将其放置在共享数据段 block 内。该值目前在“hookNotepad”调用内评估,并且仅存在于主机 exe 中。此问题导致记事本 exe 中的 npHWND 句柄为空,因此 SetWindowLongPtr
调用失败。
(2) 有 2 个 SetWindowLongPtr
调用,其中一个是错误的。 GetMsgProc
中的内容是正确的,因为安装 Hook 后它将在 notepad-exe 上下文中执行。删除 hookNotepad
中另一个错误的。
(3) 即使 (1) 和 (2) 得到解决,SetWindowLongPtr
的最终行为也可能不是您所期望的,因为 notepad-exe 的主要 UI 交互元素是嵌入的编辑控件而不是主框架窗口。您应该枚举记事本框架的子窗口,并使用 Edit
类对唯一一个子窗口进行子类化。
编辑 #1 - 添加声音指示器代码以检查事件------------------------------------
将此代码块添加到 GetMsgProc
// make some noise
static DWORD dwTickKeep = 0;
if ((GetTickCount()-dwTickKeep)>300)
{ dwTickKeep = GetTickCount();
Beep(2000, 100);
}
关于c - SetWindowLongPtr 返回 ERROR_ACCESS_DENIED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16633992/