Click here to Skip to main content
15,904,503 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.5K   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

     
    GeneralRe: Difference between Delphi and VC++ apps? Pin
    Anonymous25-Sep-02 15:09
    Anonymous25-Sep-02 15:09 
    Generalgetting api call syntax Pin
    4-Nov-01 19:33
    suss4-Nov-01 19:33 
    GeneralRe: getting api call syntax Pin
    4-Nov-01 19:54
    suss4-Nov-01 19:54 
    GeneralRe: getting api call syntax Pin
    Anonymous30-Jul-02 20:08
    Anonymous30-Jul-02 20:08 
    GeneralRedirect *any* API call Pin
    31-Oct-01 20:12
    suss31-Oct-01 20:12 
    GeneralRe: Redirect *any* API call Pin
    4-Nov-01 20:03
    suss4-Nov-01 20:03 
    GeneralRe: Redirect *any* API call Pin
    22-Feb-02 16:51
    suss22-Feb-02 16:51 
    GeneralRe: Redirect *any* API call Pin
    alex.barylski18-Nov-03 22:32
    alex.barylski18-Nov-03 22:32 
    Thats an awesome idea actually...I never even thought of that...and here this whole time I thought it was impossible... Smile | :)



    The word of the day is legs, let's go back to my house and spread the word Poke tongue | ;-P
    GeneralPrototype problem Pin
    31-Oct-01 12:49
    suss31-Oct-01 12:49 
    GeneralRe: Prototype problem Pin
    4-Nov-01 19:59
    suss4-Nov-01 19:59 
    GeneralAPIHijack's bug Pin
    30-Oct-01 15:56
    suss30-Oct-01 15:56 
    GeneralRe: APIHijack's bug Pin
    30-Oct-01 16:10
    suss30-Oct-01 16:10 
    Generalglobal hook? Pin
    30-Oct-01 18:17
    suss30-Oct-01 18:17 
    GeneralRe: global hook? Pin
    4-Nov-01 19:57
    suss4-Nov-01 19:57 
    QuestionNon Imported Function? Pin
    17-Oct-01 17:04
    suss17-Oct-01 17:04 
    AnswerRe: Non Imported Function? Pin
    17-Oct-01 19:36
    suss17-Oct-01 19:36 
    GeneralRe: Non Imported Function? Pin
    30-Oct-01 17:01
    suss30-Oct-01 17:01 
    GeneralRe: Non Imported Function? Pin
    Nish Nishant6-Jan-03 15:22
    sitebuilderNish Nishant6-Jan-03 15:22 
    GeneralRe: Non Imported Function? Pin
    TiMBuS11-May-06 19:29
    TiMBuS11-May-06 19:29 
    GeneralHooking a None-Hooked Process Pin
    5-Oct-01 20:00
    suss5-Oct-01 20:00 
    QuestionHow must i code it? Pin
    29-Sep-01 11:19
    suss29-Sep-01 11:19 
    GeneralHook functions from GetProcAddress Pin
    Masud Alipour19-Jul-01 0:46
    Masud Alipour19-Jul-01 0:46 
    Generaldoes'nt work to some app Pin
    Ben16823-Jun-01 3:35
    Ben16823-Jun-01 3:35 
    GeneralRe: does'nt work to some app Pin
    24-Jul-01 20:20
    suss24-Jul-01 20:20 
    GeneralRe: does'nt work to some app Pin
    7-Sep-01 4:34
    suss7-Sep-01 4:34 

    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.