Я уже некоторое время бьюсь об это головой. Я успешно использую Microsoft Detours, за исключением одного случая.
Я пытаюсь перехватить WINBASEAPI DWORD WINAPI GetTickCount(VOID);
из Kernel32.dll. DetourAttach всегда возвращает ERROR_INVALID_BLOCK.
Из документов:
ERROR_INVALID_BLOCK
The function referenced is too small to be detoured.
Я видел, как многие другие люди успешно подключали эту функцию с помощью Detours, но я просто не могу ее получить. Меня не волнует вызов исходной функции после обхода.
Я использую Detours Express 3.0 с 32-битными приложениями в Windows 7 x64.
У кого-нибудь есть идеи?
Полный код:
#include <windows.h>
#include <stdio.h>
#include "include\detours.h"
#pragma comment( lib, "detours.lib" )
BOOL(WINAPI *Orig_QueryPerformanceCounter)(LARGE_INTEGER *lpPerformanceCount) = QueryPerformanceCounter;
BOOL WINAPI New_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount) {
printf("QueryPerformanceCounter()\n");
return 0;
}
DWORD(WINAPI * Orig_GetTickCount)() = GetTickCount;
DWORD WINAPI New_GetTickCount() {
printf("GetTickCount()\n");
return 0;
}
BOOL WINAPI DllMain(HINSTANCE, DWORD dwReason, LPVOID) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
{
LONG error = DetourTransactionBegin();
if (error != NO_ERROR) {
printf("DetourTransactionBegin failed with error: %d.\n", error);
return FALSE;
}
error = DetourUpdateThread(::GetCurrentThread());
if (error != NO_ERROR) {
printf("DetourUpdateThread failed with error: %d.\n", error);
return FALSE;
}
//DetourSetIgnoreTooSmall(TRUE); // Doesn't help
// Works fine
error = DetourAttach(&(PVOID &)Orig_QueryPerformanceCounter, New_QueryPerformanceCounter);
if (error != NO_ERROR) {
printf("DetourAttach QueryPerformanceCounter failed with error: %d.\n", error);
return FALSE;
}
// Fails here, with error = 9
error = DetourAttach(&(PVOID &)Orig_GetTickCount, New_GetTickCount);
if (error != NO_ERROR) {
printf("DetourAttach GetTickCount failed with error: %d.\n", error);
return FALSE;
}
error = DetourTransactionCommit();
if (error != NO_ERROR) {
printf("DetourTransactionCommit failed with error: %d.\n", error);
return FALSE;
}
break;
}
case DLL_PROCESS_DETACH:
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID &)Orig_QueryPerformanceCounter, New_QueryPerformanceCounter);
DetourDetach(&(PVOID &)Orig_GetTickCount, New_GetTickCount);
DetourTransactionCommit();
break;
}
}
return TRUE;
}
I've seen numerous other people hook this function successfully with Detours
, у вас есть примеры? - person theB   schedule 16.09.2015GetTickCount
не имеет необходимой преамбулы для успешной работы Detours. - person Jonathan Potter   schedule 16.09.2015GetTickCount
? Может оказаться невозможным переопределить функциональностьGetTickCount
, но могут быть лучшие способы добиться конечного результата. - person theB   schedule 16.09.2015