僅供參考,已經解決了。要查看哪些進程是32位的,只需按Ctrl-Alt-Delete並轉到任務管理器; 32位進程在其旁邊以* 32列出。也讓我的鉤子工作;這裏是代碼。我放棄了CreateRemoteThread方法,只使用了系統範圍的鉤子。
How to hook external process with SetWindowsHookEx and WH_KEYBOARD http://www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-4 http://www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-7
這個程序簡單地反轉以32位處理文本(如圖中最後一個環節以上):我從縫合的代碼在一起。例如。打開textpad並將鼠標懸停在菜單上;他們的文字應該顛倒過來。
該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;
}
但是在試圖迂迴ExtTextOut在Java應用程序時,Java應用程序崩潰;需要調查一下。
使用的是缺少所有錯誤檢查的示例代碼。你將無法診斷失敗。您需要修復該問題,始終檢查函數返回值,並在函數失敗時使用GetLastError()來獲取錯誤代碼。 – 2011-12-24 15:01:26
嘿,謝謝你的擡頭。我用調用GetLastError()來調用主代碼,它看起來像我調用CreateRemoteThread()時,它正在退出,出現錯誤代碼5 - 「訪問被拒絕」。我嘗試以管理員身份運行visual studio,但這並沒有幫助;在一些其他論壇上發現了一篇關於同一問題的舊帖子: – 2011-12-25 05:21:07
「這是一項非常特權的操作,因爲它具有特權,使用Technet論壇來解決安全問題。試圖將32位代碼注入64位進程?不行。「我怎麼知道過程是32/64位,如果我注入的DLL是32/64位?我認爲一些程序,例如。在程序x86文件夾下安裝自己的文本板是32位的...是真的嗎?感謝您的進一步協助。 – 2011-12-25 05:24:26