How to use the Detour library in C++ properly for a simple hook of a function with known memory adress?

I am having trouble to get my first hook using detour to work. I am using Detour 3.0.

My code compiles fine and I can inject the DLL using Winject , however, the function which I am suppose to hook doesnt seem to be hooked. I am trying to hook the function InsertDateTime in notepad.

I have found the adress of the InsertDateTime in hex notation using IDA Pro Free.

Is there anything fundmatal misstakes in the code below or is the memory in the process not ceratinaly at the same time at every call?

My code for the injected DLL can be seen below:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

#include <windows.h>
#include "detours.h"
#pragma comment(lib, "detours.lib")

int(__stdcall* InsertDateTime)(int) = (int(__stdcall*)(int))(0x0100978A);
//int (__stdcall* InsertDateTime)(int x); //Function prototype
int MyInsertDateTime(int x) //Our function
MessageBox(NULL, TEXT("InsertDateTime Just Got Called"), TEXT("InsertDateTime"), MB_OK);
return InsertDateTime(x); //Return the origional function

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
long apa = NULL;
switch (ul_reason_for_call) //Decide what to do
case DLL_PROCESS_ATTACH: //On dll attach
apa = DetourAttach((PVOID*)(&InsertDateTime), (PVOID)MyInsertDateTime);
MessageBox(NULL, TEXT("InsertDateTime Just Got Called BAJS"), TEXT("CAPTION"), MB_OK);

case DLL_THREAD_ATTACH: //On thread attach
//DetourAttach((PVOID*)(&InsertDateTime), (PVOID)MyInsertDateTime);
case DLL_THREAD_DETACH: //On thread detach
case DLL_PROCESS_DETACH: //on process detach
DetourDetach((PVOID*)0x0100978A, InsertDateTime);
return TRUE;

Topic archived. No new replies allowed.