仅供参考解决了这个问题。要查看哪些进程是 32 位的,只需 ctrl-alt-delete 并转到任务管理器;列出了 32 位进程,旁边带有 *32。也让我的钩子工作了;这是代码。我放弃了 CreateRemoteThread 方法,只使用了一个系统范围的钩子。我将代码拼接在一起:
如何使用 SetWindowsHookEx 和 WH_KEYBOARD 挂钩外部进程
http://www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-4
http://www.codingthewheel.com/archives/我如何建立一个工作在线扑克机器人 7
该程序只是在 32 位进程中反转文本(如上面最后一个链接所示)。例如。打开文本板并将鼠标悬停在菜单上;他们的文字应该被颠倒。
DLL:
#include <windows.h>
#include <detours.h>
#include <stdio.h>
#include <iostream>
using namespace std;
// Initial stuff
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#pragma comment( lib, "Ws2_32.lib" )
#pragma comment( lib, "detours.lib" )
#pragma data_seg("Shared")
HHOOK g_hHook = NULL;
#pragma data_seg()
// Globals
HINSTANCE g_hInstance = NULL;
// ExtTextOut - original
BOOL (WINAPI * Real_ExtTextOut)(HDC hdc, int X, int Y, UINT options, const RECT* lprc, LPCTSTR text, UINT cbCount, const INT* lpSpacingValues) = ExtTextOut;
// ExtTextOut - overridden
BOOL WINAPI Mine_ExtTextOut(HDC hdc, int X, int Y, UINT options, const RECT* lprc, LPCTSTR text, UINT cbCount, const INT* lpSpacingValues)
{
if (!text)
return TRUE;
// Make a copy of the supplied string..safely
LPWSTR szTemp = (LPWSTR)LocalAlloc(0, (cbCount+1) * 2);
memcpy(szTemp, text, cbCount*2); // can't use strcpy here
szTemp[cbCount] = L'\0'; // append terminating null
// Reverse it..
wcsrev(szTemp);
// Pass it on to windows...
BOOL rv = Real_ExtTextOut(hdc, X, Y, options, lprc, szTemp, cbCount, lpSpacingValues);
// Cleanup
LocalFree(szTemp);
return TRUE;
}
// DLLMain
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hInstance = (HINSTANCE) hModule;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut); // <- magic
DetourTransactionCommit();
break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut);
DetourTransactionCommit();
break;
}
return TRUE;
}
// CBT Hook - dll is hooked into all processes (only 32 bit processes on my machine)
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
// Return 0 to allow window creation/destruction/activation to proceed as normal.
return 0;
}
// Install hook
extern "C" __declspec(dllexport) bool install()
{
g_hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC) CBTProc, g_hInstance, 0);
return g_hHook != NULL;
}
// Uninstall hook
extern "C" __declspec(dllexport) void uninstall()
{
if (g_hHook)
{
UnhookWindowsHookEx(g_hHook);
g_hHook = NULL;
}
}
主程序:
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
// Main
int _tmain(int argc, _TCHAR* argv[])
{
// Load dll
HINSTANCE hinst = LoadLibrary(_T("C:\\Users\\PM\\Documents\\Programs\\C Code\\Test\\DLLTesterFinal\\Debug\\DLLTesterFinal.dll"));
if (hinst)
{
// Get functions
typedef bool (*Install)();
typedef void (*Uninstall)();
Install install = (Install) GetProcAddress(hinst, "install");
Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall");
cout << "GetLastError1: " << GetLastError () << endl << endl;
// Install hook
bool hookInstalledSuccessfully = install ();
cout << "GetLastError2: " << GetLastError () << endl;
cout << "Hook installed successfully? " << hookInstalledSuccessfully << endl << endl;
// At this point, go to a 32-bit process (eg. textpad, chrome) and hover over menus; their text should get reversed
cout << "Text should now be reversed in 32-bit processes" << endl;
system ("Pause");
// Uninstall hook
uninstall();
cout << endl << "GetLastError3: " << GetLastError () << endl;
cout << "Done" << endl;
system ("Pause");
}
return 0;
}
但是,在尝试绕过 Java 应用程序中的 ExtTextOut 时,Java 应用程序崩溃;需要对此进行调查。