Thread: Detours 2.1

Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    pushedx's Avatar
    Join Date
    Nov 2009
    Gender
    male
    Posts
    4
    Reputation
    10
    Thanks
    4
    Here's a short tutorial on using Detours 2.1 with Visual Studio 2008 via the IDE.

    1. Download and install "DetoursExpress.msi" from their page. The default folder is: "C:\Program Files\Microsoft Research\Detours Express 2.1".

    2. Open Visual Studio and go to "Tools->Options". In the new dialog go to "Projects and Solutions->VC++ Directories". Change the "Show directories for:" combobox in the top right corner to "Include files". Add the path to the 'src' folder of the default detours install directory. In the default case, this will be "C:\Program Files\Microsoft Research\Detours Express 2.1\src". Hit Ok to close the dialog.

    3. Create a new project for testing. I just made a simple empty Win32 project named DetoursTest. Add a new source file as well.
    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	return 0;
    }
    4. Go to "Project->Add Existing Item". Paste in the path to the 'src' folder of the Detours directory. Once again, by default this is "C:\Program Files\Microsoft Research\Detours Express 2.1\src". Highlight all the CPP and H files except for "detoured.cpp" and hit Add. You can just delete "detoured.cpp" if you wish or move it into another directory.

    5. Now if you try to compile, you will see a few errors:
    1>c:\program files\microsoft research\detours express 2.1\src\detours.cpp(22) : fatal error C1189: #error : Must define one of DETOURS_X86, DETOURS_X64, or DETOURS_IA64
    1>c:\program files\microsoft research\detours express 2.1\src\disasm.cpp(19) : fatal error C1189: #error : Must define one of DETOURS_X86, DETOURS_X64, or DETOURS_IA64
    Go back to "Project->Properties". Choose "Configuration Properties->C/C++->Preprocessor" and add "DETOURS_X86" to the end of the list, making sure to separate the entry with a ';'. The final result should look like: "WIN32;_DEBUG;_WINDOWS;DETOURS_X86". Click Apply and then change the current Configuration via the top left combobox to Release and repeat the step. The final result should look like: "WIN32;NDEBUG;_WINDOWS;DETOURS_X86".

    6. Now you should be able to compile and link successfully. Time for the actual detour code. The "Character Set" of the project is set to "Use Unicode Character Set" by default, so keep that in mind if you use API functions without specifying the A/W version manually. You will be hooking only the W versions or if you change the Character Set to "Use Multi-Byte Character Set" you will be hooking the A versions. You can change the setting via the "General" section in the project properties, 3rd to last setting.
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <detours.h>
    
    // We must track this modules instance handle for detours
    HINSTANCE gInstance;
    
    // Wrapping detour functions in a namespace to keep things neat
    namespace nsDetours
    {
    	// We must always make sure to use the correct calling convention for the
    	// API functions we are wishing to hook.
    	extern "C" int (WINAPI * Real_MessageBoxA)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) = MessageBoxA;
    	int WINAPI Fake_MessageBoxA(HWND hWnd, LPSTR lpText, LPSTR lpCaption, UINT uType)
    	{
    		// For this detour, we want to modify the title of any MessageBoxA
    		// call that has a MB_OK uType specified (0).
    		if(uType == 0)
    		{
    			return Real_MessageBoxA(hWnd, lpText, "Detoured MessageBoxA!", uType);
    		}
    		// Otherwise, returned the original call with no modifications
    		return Real_MessageBoxA(hWnd, lpText, lpCaption, uType);
    	}
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	gInstance = hInstance;
    
    	// Detour our API functions, no error checking
    	DetourRestoreAfterWith();
    	DetourTransactionBegin();
    	DetourUpdateThread(GetCurrentThread());
    	// DetourAttach all functions to hook
    	DetourAttach(&(PVOID&)nsDetours::Real_MessageBoxA, nsDetours::Fake_MessageBoxA);
    	DetourTransactionCommit();
    
    	MessageBoxW(0, L"Hello World", L"MessageBoxW from WinMain", 0);
    	MessageBoxA(0, "Hello World", "MessageBoxA from WinMain", 0);
    	MessageBoxA(0, "Hello World", "MessageBoxA from WinMain", MB_ICONINFORMATION);
    
    	// Undetour our API functions, no error checking shown
    	DetourTransactionBegin();
    	DetourUpdateThread(GetCurrentThread());
    	// DetourDetach all DetourAttach'ed functions
    	DetourDetach(&(PVOID&)nsDetours::Real_MessageBoxA, nsDetours::Fake_MessageBoxA);
    	DetourTransactionCommit();
    
    	return 0;
    }
    
    // We must always have this function linked for detours. In a DLL, we
    // return the HINSTANCE from DLLMain. In EXEs we use the HINSTANCE
    // of WinMain.
    HMODULE WINAPI Detoured()
    {
    	return gInstance;
    }
    Now you have the ability to detour functions as you need. I prefer using the source files in my project each time than building the lib as it makes it easier for people to look through the code in redistributed projects. Others might prefer the LIB, your call.

    I showed an example using an EXE to control the detours, most people will instead use an injected DLL. The steps are pretty much the same, except the code for the DLL has to be mindful of the Best Practices for Creating DLLs!

    Lastly for reference, I used this blog post way back when I started using detours 2.1 to help get a jump start after having troubles with it when migrating from 1.5.

  2. The Following 2 Users Say Thank You to pushedx For This Useful Post:

    Hell_Demon (11-05-2009),why06 (11-05-2009)

  3. #17
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,204
    My Mood
    Flirty
    Quote Originally Posted by pushedx View Post
    Here's a short tutorial on using Detours 2.1 with Visual Studio 2008 via the IDE.
    Lol. short.. hey thanks a lot. I will read this when I get back from class. Haha I like this guy already. Hope you stick around.

    EDIT:

    OMG. Thanks so much. I've been trying to figure out how to get this thing installed forever. NOw that that is finally over with I can focus on the code. A thousand kudos to you good sir!
    Last edited by why06; 11-05-2009 at 03:02 PM.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  4. The Following User Says Thank You to why06 For This Useful Post:

    Hell_Demon (11-05-2009)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Detouring MPGH Filters Will Result In A Ban.
    By radnomguywfq3 in forum Combat Arms Hacks & Cheats
    Replies: 0
    Last Post: 11-09-2008, 06:02 PM
  2. Detour problem
    By juppeli in forum WarRock - International Hacks
    Replies: 4
    Last Post: 07-16-2008, 03:56 AM
  3. [Realease-Test]Detours Test
    By Kung Fu Penguin31 in forum WarRock - International Hacks
    Replies: 16
    Last Post: 06-29-2008, 05:35 AM
  4. Detour
    By HackingIsMyLife in forum Programming Tutorial Requests
    Replies: 0
    Last Post: 05-20-2008, 07:17 AM
  5. coding detour?
    By laserdude45 in forum C++/C Programming
    Replies: 3
    Last Post: 01-20-2008, 03:11 PM

Tags for this Thread