locked
How to extend an in-program function using Detours in c++ without going in an infinite loop? RRS feed

  • Question

  • 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 the function. The DLL is injected correctly and the function gets detoured to the new function that I have specified. 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 in an infinite loop. So the original function can never be called!

    How should I do this properly?

    The code can be seen below:

    // 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;
    }
    
    

    • Moved by Jaynet Zhang Thursday, June 13, 2013 7:45 AM wrong forum
    Monday, June 10, 2013 4:36 PM

Answers

  • Hi,

    this is the TechNet forum for MS Visio.

    For developers, instead, use the MSDN forums: http://social.msdn.microsoft.com/Forums/en-US/categories

    Thanks!


    Don
    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer", where applicable.
    This helps the community, keeps the forums tidy, and recognises useful contributions. Thanks!)

    • Proposed as answer by DonPick Monday, June 10, 2013 9:31 PM
    • Marked as answer by Just Karl Friday, October 4, 2013 3:52 PM
    Monday, June 10, 2013 9:31 PM

All replies

  • I'd start by posting it in a Visual Studio forum, rather than an office forum.

    al


    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Al Edlund Visio MVP

    Monday, June 10, 2013 6:41 PM
  • Hi,

    this is the TechNet forum for MS Visio.

    For developers, instead, use the MSDN forums: http://social.msdn.microsoft.com/Forums/en-US/categories

    Thanks!


    Don
    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer", where applicable.
    This helps the community, keeps the forums tidy, and recognises useful contributions. Thanks!)

    • Proposed as answer by DonPick Monday, June 10, 2013 9:31 PM
    • Marked as answer by Just Karl Friday, October 4, 2013 3:52 PM
    Monday, June 10, 2013 9:31 PM