Click here to Skip to main content
15,905,508 members
Articles / Programming Languages / C++
Article

APIHijack - A Library for easy DLL function hooking.

Rate me:
Please Sign up or sign in to vote.
4.79/5 (30 votes)
15 Sep 2000CPOL 805.7K   10.5K   173   156
This library allows you to replace functions in other DLLs with functions from your own DLL.
  • Download source files and demo project - 102 Kb

    Introduction

    Based on DelayLoadProfileDLL.CPP, by Matt Pietrek for MSJ February 2000. This code is intended to be included in a DLL inserted through a global Windows Hook (CBT hook for example). It will replace functions from other DLLs (e.g. DDRAW.DLL) with functions from your DLL.

    Functions are hooked by passing a parameter structure to the HookAPICalls() function as follows:

    SDLLHook D3DHook = 
    {
        "DDRAW.DLL",
        false, NULL,    // Default hook disabled, NULL function pointer.
        {
            { "DirectDrawCreate", MyDirectDrawCreate },
            { NULL, NULL }
        }
    };
    
    BOOL APIENTRY DllMain( HINSTANCE hModule, DWORD fdwReason, LPVOID lpReserved)
    {
        if ( fdwReason == DLL_PROCESS_ATTACH )  // When initializing....
        {
            hDLL = hModule;
    
            // We don't need thread notifications for what we're doing.  Thus, 
            // get rid of them, thereby eliminating some of the overhead of 
            // this DLL
            DisableThreadLibraryCalls( hModule );
    
            // Only hook the APIs if this is the right process.
            GetModuleFileName( GetModuleHandle( NULL ), Work, sizeof(Work) );
            PathStripPath( Work );
    
            if ( stricmp( Work, "myhooktarget.exe" ) == 0 )
                HookAPICalls( &D3DHook );
        }
    
        return TRUE;
    }

    Now all that remains is to get your DLL loaded into the target process.

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralUsing this on different dlls Pin
    john34zqwz22-Sep-00 14:57
    john34zqwz22-Sep-00 14:57 
    GeneralUsing this on different dlls Pin
    john34zqwz22-Sep-00 14:51
    john34zqwz22-Sep-00 14:51 
    GeneralRe: Using this on different dlls Pin
    Wade Brainerd24-Sep-00 14:01
    Wade Brainerd24-Sep-00 14:01 
    GeneralRe: Using this on different dlls Pin
    mahesh kumar s3-Jul-07 20:48
    mahesh kumar s3-Jul-07 20:48 
    QuestionDoesn't compile under win2000 ? Pin
    Anonymous22-Sep-00 14:09
    Anonymous22-Sep-00 14:09 
    AnswerRe: Doesn't compile under win2000 ? Pin
    Wade Brainerd22-Sep-00 14:14
    Wade Brainerd22-Sep-00 14:14 
    GeneralRe: Doesn't compile under win2000 ? Pin
    john34zqwz22-Sep-00 15:12
    john34zqwz22-Sep-00 15:12 
    GeneralRe: Doesn't compile under win2000 ? Pin
    Wade Brainerd24-Sep-00 13:57
    Wade Brainerd24-Sep-00 13:57 
    If you look at the hooked DirectDrawCreate, it calls MessageBeep. This causes a sound to play, which tells you that the hook was installed and is working properly.

    It's up to you to do something interesting with it.

    Is anyone else experiencing the same compiler errors? DWORD is a standard Windows type, it should be available whenever <windows.h> is included. Perhaps I forgot to include a stdafx.h or something.

    -Wad
    GeneralRe: Doesn't compile under win2000 ? Pin
    johncattech22-Apr-06 13:29
    johncattech22-Apr-06 13:29 
    GeneralObject Method Calls Pin
    XUnleashed22-Sep-00 4:45
    XUnleashed22-Sep-00 4:45 
    GeneralRe: Object Method Calls Pin
    Wade Brainerd22-Sep-00 8:33
    Wade Brainerd22-Sep-00 8:33 
    GeneralRe: Object Method Calls Pin
    XUnleashed22-Sep-00 17:13
    XUnleashed22-Sep-00 17:13 
    GeneralRe: Object Method Calls Pin
    XUnleashed22-Sep-00 18:38
    XUnleashed22-Sep-00 18:38 
    GeneralRe: Object Method Calls Pin
    Wade Brainerd24-Sep-00 13:55
    Wade Brainerd24-Sep-00 13:55 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.