Click here to Skip to main content
15,890,995 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 802.9K   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

     
    GeneralUnHook!!!!! Pin
    minhvc15-Feb-04 15:04
    minhvc15-Feb-04 15:04 
    GeneralEncrypted Pin
    khossy14-Feb-04 23:50
    susskhossy14-Feb-04 23:50 
    GeneralError compile Pin
    bigwizard28-Jan-04 5:55
    bigwizard28-Jan-04 5:55 
    GeneralRe: Error compile Pin
    Rupom / RUET,Bangladesh21-Jul-04 19:37
    sussRupom / RUET,Bangladesh21-Jul-04 19:37 
    GeneralProblem hooking TextOut Pin
    BobAshforth19-Nov-03 6:07
    BobAshforth19-Nov-03 6:07 
    GeneralHookApi SourceCode Pin
    xiamy8-Nov-03 6:31
    xiamy8-Nov-03 6:31 
    QuestionWhy can not hook the function that its return is pointer? Pin
    doxuanhuyen4-Nov-03 3:50
    doxuanhuyen4-Nov-03 3:50 
    AnswerRe: Why can not hook the function that its return is pointer? Pin
    autodebug12-Nov-03 17:16
    autodebug12-Nov-03 17:16 
    GeneralNice article Pin
    Kentamanos6-Oct-03 16:16
    Kentamanos6-Oct-03 16:16 
    GeneralDon't work for win98/95!! Pin
    chn1-Sep-03 17:48
    chn1-Sep-03 17:48 
    GeneralWarning: VIRUS FOUND!!! Pin
    andrewgs7324-Aug-03 1:05
    andrewgs7324-Aug-03 1:05 
    GeneralRe: Warning: VIRUS FOUND!!! Pin
    Kentamanos6-Oct-03 16:18
    Kentamanos6-Oct-03 16:18 
    GeneralRe: Warning: VIRUS FOUND!!! Pin
    Miguel Lopes11-May-04 2:36
    Miguel Lopes11-May-04 2:36 
    GeneralCall a VB DLL from ACUCOBOL Pin
    sant18-Dec-02 19:56
    sant18-Dec-02 19:56 
    Generalreplace functions of msvfw32.dll Pin
    fishie4-Oct-02 0:03
    fishie4-Oct-02 0:03 
    GeneralNot hook ws2_32.dll,Please help me! Pin
    gamehorse20-Aug-02 2:20
    gamehorse20-Aug-02 2:20 
    GeneralHELP PLEASE Pin
    Anonymous30-Jul-02 12:38
    Anonymous30-Jul-02 12:38 
    Generalunhook api calls to avoid crash Pin
    8-Jul-02 22:06
    suss8-Jul-02 22:06 
    GeneralOptimizer seems to cause problems Pin
    Larry Michalewicz20-Jun-02 19:29
    Larry Michalewicz20-Jun-02 19:29 
    QuestionHow to call a C++ dll in VB (DLL file name supplied at runtime) Pin
    19-Jun-02 7:51
    suss19-Jun-02 7:51 
    AnswerRe: How to call a C++ dll in VB (DLL file name supplied at runtime) Pin
    5-Jul-02 7:49
    suss5-Jul-02 7:49 
    GeneralProblem:Restoring when unloading dll Pin
    21-Apr-02 14:31
    suss21-Apr-02 14:31 
    GeneralCatching any "LoadLibrary" Pin
    15-Apr-02 3:09
    suss15-Apr-02 3:09 
    GeneralRe: Catching any "LoadLibrary" Pin
    mahesh kumar s3-Jul-07 1:50
    mahesh kumar s3-Jul-07 1:50 
    GeneralHooking object's method call Pin
    4-Apr-02 21:41
    suss4-Apr-02 21:41 

    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.