How to extend an in-program function using Detours in C++ without going in an infinite loop?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to learn to use detours to modify and extend functions in a program. In this case I am trying to modify the InsertDateTime function in Windows Notepad 32-bit.
I am using Winject to inject the DLL which I create to modify/extend the function. The DLL is injected correctly and the function gets detoured to the new function that I have specified when I use the function InsertDateTime in Notepad. However, the new function to which I detour InsertDateTime() is supposed to call the original function InsertDateTime() at the end of the new function called MyInsertDateTime().
However, the problem is that when the new function gets called and in the end tries to call the old function, this one, of course, also gets redirected/detoured in turn and thus creates an infinite loop as I see it. So the original function will never be called!

How should I handle and do this correctly?

The code:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include "detours.h"
#pragma comment(lib, "detours.lib")
//
using namespace std;
int(__stdcall* InsertDateTime)(int x);
int MyInsertDateTime(int x) //Our function
{
//Messagebox
MessageBox(NULL, TEXT("InsertDateTime Just Got Called"), TEXT("Min funktion"), MB_OK);
return InsertDateTime(x); //Return the origional function
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
InsertDateTime = (int(__stdcall*)(int))(reinterpret_cast<DWORD>(GetModuleHandleA("notepad.exe")) + 0x978A);
switch (ul_reason_for_call) //Decide what to do
{
case DLL_PROCESS_ATTACH: //On dll attach
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)(InsertDateTime), (PVOID)MyInsertDateTime);
DetourTransactionCommit();
break;
case DLL_THREAD_ATTACH: //On thread attach
break;
case DLL_THREAD_DETACH: //On thread detach
break;
case DLL_PROCESS_DETACH: //on process detach
DetourDetach((PVOID*)(reinterpret_cast<DWORD>(GetModuleHandleA("notepad.exe")) + 0x978A), InsertDateTime);
break;
}
return TRUE;
}

View the full article
 
Back
Top