Click here to Skip to main content
15,893,814 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 803.4K   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

     
    GeneralHooking dynamicly loaded DLLs functions Pin
    seybold23-May-05 22:33
    seybold23-May-05 22:33 
    GeneralRe: Hooking dynamicly loaded DLLs functions Pin
    dchris_med14-Feb-06 3:16
    dchris_med14-Feb-06 3:16 
    GeneralLooking like a atlternative of DsSubCls.dll Pin
    ThatsAlok3-Dec-04 19:10
    ThatsAlok3-Dec-04 19:10 
    GeneralHooked function address is replaced by Original one Pin
    samren23-Sep-04 20:07
    samren23-Sep-04 20:07 
    GeneralRe: Hooked function address is replaced by Original one Pin
    Member 108017311-Oct-04 2:32
    Member 108017311-Oct-04 2:32 
    GeneralRe: Hooked function address is replaced by Original one Pin
    sturlamolden12-Oct-06 12:58
    sturlamolden12-Oct-06 12:58 
    GeneralCallbacks from inside Pin
    K-ballo20-Aug-04 14:34
    K-ballo20-Aug-04 14:34 
    Generalcan't do it for . . . Pin
    gamitech8-Aug-04 10:36
    gamitech8-Aug-04 10:36 
    I cannont hijack the function
    int WSAAPI connect(...)

    can you tell me or send me the source code of the dll modified to hook this funciton
    thank you

    here is what I wrote:

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <shlwapi.h>
    #include <ddraw.h>#include "testdll.h"
    #include "..\apihijack.h"
    #include <winsock2.h>

    // Text buffer for sprintf
    char Work[256];

    HINSTANCE hDLL;

    // Function pointer types.
    typedef int (WSAAPI *connect_Type)
    (
    SOCKET s,
    const struct sockaddr FAR * name,
    int namelen
    );
    // Function prototypes.
    int WSAAPI Myconnect(
    SOCKET s,
    const struct sockaddr FAR * name,
    int namelen
    );

    // Hook structure.
    enum
    {
    D3DFN_connect=0
    };

    SDLLHook D3DHook =
    {
    "Ws2_32.DLL",
    false, NULL, // Default hook disabled, NULL function pointer.
    {
    { "connect", Myconnect },
    { NULL, NULL }
    }
    };

    // Hook function.

    int WSAAPI Myconnect
    (
    SOCKET s,
    const struct sockaddr FAR * name,
    int namelen
    )
    {
    MessageBeep( MB_ICONINFORMATION );
    MessageBox(NULL,"safdggsd","",MB_OK|MB_APPLMODAL);


    connect_Type OldFn =
    (connect_Type)D3DHook.Functions[D3DFN_connect].OrigFn;
    return OldFn( s, name, namelen );

    }

    // CBT Hook-style injection.
    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 Everquest proess.
    HookAPICalls( &D3DHook );
    }

    return TRUE;
    }

    // This segment must be defined as SHARED in the .DEF
    #pragma data_seg (".HookSection")
    // Shared instance for all processes.
    HHOOK hHook = NULL;
    #pragma data_seg ()

    TESTDLL_API LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    return CallNextHookEx( hHook, nCode, wParam, lParam);
    }

    TESTDLL_API void InstallHook()
    {
    OutputDebugString( "TESTDLL hook installed.\n" );
    hHook = SetWindowsHookEx( WH_CBT, HookProc, hDLL, 0 );
    }

    TESTDLL_API void RemoveHook()
    {
    OutputDebugString( "TESTDLL hook removed.\n" );
    UnhookWindowsHookEx( hHook );
    }


    gabby
    GeneralRe: can't do it for . . . Pin
    flaming_red_dingo2-Apr-05 3:38
    flaming_red_dingo2-Apr-05 3:38 
    Generallicense Pin
    SAITO,A21-Jul-04 3:02
    sussSAITO,A21-Jul-04 3:02 
    GeneralRe: license Pin
    Roey C29-Mar-08 18:15
    Roey C29-Mar-08 18:15 
    QuestionHow to hook DELAY IMPORT Address Table Pin
    kyo9721-Jun-04 4:06
    kyo9721-Jun-04 4:06 
    AnswerRe: How to hook DELAY IMPORT Address Table Pin
    yuvalaviguy24-Nov-04 23:05
    yuvalaviguy24-Nov-04 23:05 
    AnswerRe: How to hook DELAY IMPORT Address Table Pin
    flaming_red_dingo2-Apr-05 3:43
    flaming_red_dingo2-Apr-05 3:43 
    QuestionHow can i hook com methods Pin
    imranlodhi26-May-04 1:03
    imranlodhi26-May-04 1:03 
    AnswerRe: How can i hook com methods Pin
    autodebug19-Jul-04 17:23
    autodebug19-Jul-04 17:23 
    QuestionHow can I change the characters of a message after hooking ? Pin
    Rupom24-May-04 22:01
    Rupom24-May-04 22:01 
    GeneralCreating toolbar Pin
    pawan_ind_bly5-Apr-04 9:08
    pawan_ind_bly5-Apr-04 9:08 
    GeneralRe: Creating toolbar Pin
    dorutzu9-Jun-04 6:26
    dorutzu9-Jun-04 6:26 
    GeneralRe: Creating toolbar Pin
    ThatsAlok3-Dec-04 19:09
    ThatsAlok3-Dec-04 19:09 
    GeneralRe: Creating toolbar Pin
    Anonymous28-Dec-04 23:35
    Anonymous28-Dec-04 23:35 
    GeneralCapture Text in RichEdit and HTML Pin
    minhvc7-Mar-04 16:39
    minhvc7-Mar-04 16:39 
    QuestionHow to remove hook Pin
    minhvc19-Feb-04 15:53
    minhvc19-Feb-04 15:53 
    AnswerRe: How to remove hook Pin
    minhvc7-Mar-04 16:36
    minhvc7-Mar-04 16:36 
    GeneralRe: How to remove hook Pin
    Roey C29-Mar-08 19:01
    Roey C29-Mar-08 19:01 

    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.