Click here to Skip to main content
15,905,419 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

     
    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 
    I am a chinese boy ,my english is very poor.
    I use you programe,but not hook ws_32.dll

    I write code:
    SDLLHook D3DHook =
    {
    "ws2_32.dll",
    //"WSOCK32.DLL",
    false, NULL, // Default hook disabled, NULL function pointer.
    {
    { "send",hooksend },
    { "recv",hookrecv },
    { "closesocket",hookclosesocket},
    { NULL, NULL }
    }
    };

    then
    if ( stricmp( Work, "Iexplore.exe" ) == 0 )
    HookAPICalls( &D3DHook );
    Ok ,vc output:
    Loaded 'C:\Program Files\Internet Explorer\IEXPLORE.EXE', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\SHLWAPI.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\GDI32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\USER32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\KERNEL32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\ADVAPI32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\SHELL32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\COMCTL32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\SHDOCVW.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\IMM32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\OLE32.DLL', no matching symbolic information found.
    HOOKDLL checking process: IEXPLORE.EXE
    Loaded 'C:\WINDOWS\SYSTEM\MSVCRT.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\Downloaded Program Files\CnsMin.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\VERSION.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\INDICDLL.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\BROWSEUI.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\BROWSELC.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\WININET.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\SHFOLDER.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\MLANG.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\OLEAUT32.DLL', no matching symbolic information found.
    First-chance exception in Iexplore.exe (MSONSEXT.DLL): 0x006D007E: (no name).
    Loaded 'C:\WINDOWS\SYSTEM\MYDOCS.DLL', no matching symbolic information found.
    First-chance exception in Iexplore.exe (MSONSEXT.DLL): 0x006D007E: (no name).
    First-chance exception in Iexplore.exe (MSONSEXT.DLL): 0x006D007E: (no name).
    Loaded 'C:\Program Files\Adobe\Acrobat 5.0\Reader\ActiveX\AcroIEHelper.ocx', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\RPCRT4.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\URLMON.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\SHDOCLC.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\WSOCK32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\WS2HELP.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\WS2_32.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\MSWSOCK.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\MSAFD.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\RNR20.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\MSHTML.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\PDM.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\MSDBG.DLL', no matching symbolic information found.
    First-chance exception in Iexplore.exe (GDI32.DLL): 0xC0000005: Access Violation.
    First-chance exception in Iexplore.exe (GDI32.DLL): 0xC0000005: Access Violation.
    Loaded 'C:\WINDOWS\SYSTEM\JSCRIPT.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\MSLS31.DLL', no matching symbolic information found.
    First-chance exception in Iexplore.exe (GDI32.DLL): 0xC0000005: Access Loaded 'C:\WINDOWS\SYSTEM\MMUTILSE.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\MMEFXE.OCX', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\NTDLL.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\DDRAW.DLL', no matching symbolic information found.
    Loaded 'C:\WINDOWS\SYSTEM\DDRAWEX.DLL', no matching symbolic information found.
    First-chance exception in Iexplore.exe (GDI32.DLL): 0xC0000005: Access First-chance exception in Iexplore.exe (GDI32.DLL): 0xC0000005: Access Violation.
    The thread 0xFFF04275 has exited with code 0 (0x0).
    The thread 0xFFF76139 has exited with code 0 (0x0).
    The thread 0xFFF2CD65 has exited with code -1 (0xFFFFFFFF).
    The thread 0xFFF7201D has exited with code -1 (0xFFFFFFFF).
    The thread 0xFFF2C531 has exited with code -1 (0xFFFFFFFF).
    The thread 0xFFF2C691 has exited with code -1 (0xFFFFFFFF).
    The thread 0xFFF0BCF9 has exited with code -1 (0xFFFFFFFF).
    The thread 0xFFF0495D has exited with code -1 (0xFFFFFFFF).
    The thread 0xFFF00F79 has exited with code 0 (0x0).
    The program 'C:\Program Files\Internet Explorer\Iexplore.exe' has exited with code 0 (0x0).

    so programe not hook ws_32.dll,also programe cannot hook WSOCK32.DLL too,
    can you help me ,I very hurry, i wait you,
    my english is poor ,can you understand? thank you very much
    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 
    GeneralRe: Hooking object's method call Pin
    4-Apr-02 21:58
    suss4-Apr-02 21:58 
    GeneralRe: Hooking object's method call Pin
    4-Apr-02 22:12
    suss4-Apr-02 22:12 
    GeneralRe: Hooking object's method call Pin
    4-Apr-02 22:47
    suss4-Apr-02 22:47 
    GeneralHelp... Pin
    19-Feb-02 21:57
    suss19-Feb-02 21:57 
    GeneralRe: Help... Pin
    4-Mar-02 3:02
    suss4-Mar-02 3:02 
    QuestionHow to hook "CreateProcess" globally? Pin
    17-Feb-02 21:09
    suss17-Feb-02 21:09 
    GeneralProblem in Win98 Pin
    13-Feb-02 23:34
    suss13-Feb-02 23:34 
    GeneralDLL hooking - An inspiration to crackers Pin
    Vornez9-Feb-02 11:25
    Vornez9-Feb-02 11:25 

    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.