Click here to Skip to main content
15,920,438 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 807.8K   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

     
    GeneralIntercepting API calls from imported DLLs Pin
    24-May-01 16:44
    suss24-May-01 16:44 
    GeneralRe: Intercepting API calls from imported DLLs Pin
    24-May-01 18:26
    suss24-May-01 18:26 
    GeneralRe: Intercepting API calls from imported DLLs Pin
    25-May-01 3:18
    suss25-May-01 3:18 
    GeneralRe: Intercepting API calls from imported DLLs Pin
    11-Sep-01 3:37
    suss11-Sep-01 3:37 
    QuestionHow do I go from here on? Pin
    Sasha Djurovic5-May-01 16:22
    Sasha Djurovic5-May-01 16:22 
    AnswerRe: How do I go from here on? Pin
    Sasha Djurovic11-May-01 9:52
    Sasha Djurovic11-May-01 9:52 
    GeneralDll for hooking add/delete file i the system Pin
    19-Apr-01 8:51
    suss19-Apr-01 8:51 
    GeneralHooking d3d8.dll... Pin
    16-Apr-01 10:22
    suss16-Apr-01 10:22 
    Hi,

    I'm tring to hook Direct3D samples in Directx8 SDK(billboard.exe). However, ApiHijack does not work. It's strange because it works with DirectX7 for 6 samples.(hooking ddraw.dll)

    Using process viewer, I know the TestDll.dll is loaded into the target process. However, it just bypasses MyCreateDirect3d8(intercepted version of Direct3DCreate8).

    I found that d3d8.dll is not in Shared memory.(while ddraw.dll is in it). Is this a problem? Here is the code for hooking Direct3DCreate8. Please help me.

    // Function pointer types.
    typedef IDirect3D8* (WINAPI *Direct3DCreate8_Type)( UINT SDKVersion );
    // Function prototypes.
    IDirect3D8* WINAPI MyDirect3DCreate8( UINT SDKVersion );

    SDLLHook D3D8Hook =
    {
    "D3D8.DLL",
    false, NULL, // Default hook disabled, NULL function pointer.
    {
    { "Direct3DCreate8", MyDirect3DCreate8 },
    { NULL, NULL }
    }
    };

    // Hook function.
    IDirect3D8* WINAPI MyDirect3DCreate8( UINT SDKVersion )
    {
    // Let the world know we're working.
    MessageBeep( MB_ICONINFORMATION );

    Direct3DCreate8_Type OldFn =
    (Direct3DCreate8_Type)D3D8Hook.Functions[0].OrigFn;
    return OldFn( SDKVersion );
    }


    QuestionUs it possible to list extern TREE | LIST? Pin
    27-Mar-01 11:33
    suss27-Mar-01 11:33 
    GeneralRe: Us it possible to list extern TREE | LIST? Pin
    27-Mar-01 19:44
    suss27-Mar-01 19:44 
    GeneralProblem trapping an API call made by a DLL. Pin
    27-Jan-01 6:09
    suss27-Jan-01 6:09 
    Questionhooking GetProcAddress from Kernel32 ? Pin
    15-Jan-01 11:04
    suss15-Jan-01 11:04 
    AnswerRe: hooking GetProcAddress from Kernel32 ? Pin
    15-Jan-01 11:24
    suss15-Jan-01 11:24 
    GeneralRe: hooking GetProcAddress from Kernel32 ? Pin
    Member 44700157-Jul-08 20:45
    Member 44700157-Jul-08 20:45 
    GeneralInterception of Programms starts Pin
    23-Dec-00 11:01
    suss23-Dec-00 11:01 
    GeneralRe: Interception of Programms starts Pin
    27-Dec-00 7:52
    suss27-Dec-00 7:52 
    QuestionHijacking the TextOutA function? Pin
    5-Nov-00 14:27
    suss5-Nov-00 14:27 
    AnswerRe: Hijacking the TextOutA function? Pin
    23-Dec-00 12:55
    suss23-Dec-00 12:55 
    Questionhooking CopyFile ? Pin
    -11-Oct-00 3:17
    -11-Oct-00 3:17 
    QuestionWorks on internal calls too? Pin
    Tom Malcolmson3-Oct-00 12:14
    Tom Malcolmson3-Oct-00 12:14 
    AnswerRe: Works on internal calls too? Pin
    Wade Brainerd3-Oct-00 12:42
    Wade Brainerd3-Oct-00 12:42 
    GeneralHooking by ordinal number Pin
    Joe Celi29-Sep-00 5:48
    sussJoe Celi29-Sep-00 5:48 
    GeneralRe: Hooking by ordinal number Pin
    Wade Brainerd30-Sep-00 10:26
    Wade Brainerd30-Sep-00 10:26 
    GeneralRe: Hooking by ordinal number Pin
    15-Feb-01 10:06
    suss15-Feb-01 10:06 
    QuestionGlobal shared memory problem? Pin
    David28-Sep-00 14:05
    David28-Sep-00 14:05 

    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.