Click here to Skip to main content
15,904,817 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: 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 
    I have been experimenting with the APIhijack workspace alot. Its a reliable way of intercepting code from a post compiled program. I've done a few major mods to APIhijack and as long as you write proper clean code for it, testlaucher.exe will never cause the hijacked target EXE to crash.

    While traditional disassemblers allow you to do debugging, and 'patch' certain
    lines of assembly code of an EXE, APIhijack allows you to replace entire DLL imports with new custom functions written in C++. The cracker therefore doesn't need to worry about what language an EXE is written in.

    W32dasm is used by crackers who want to do things like remove
    shareware splash screens, disable advertisement windows, trigger program registration processes and bypass screens which ask for original authentic CDs. APIhijack has the potential to do all these things but in a much more stable way.

    I reckon API hijacking is a more feasible way of cracking a program. Hooks don't directly modify the code of the EXE, they use the features offered by the kernel to re-route them. APIhijack therefore has more leverage as a cracking tool. Instead of changing the EXE itself, you change the kernel which 'feeds' the EXE - an easier and more stable way of cracking. Cool | :cool:
    QuestionDifference between Delphi and VC++ apps? Pin
    User 66581-Jan-02 4:45
    User 66581-Jan-02 4:45 
    AnswerRe: Difference between Delphi and VC++ apps? Pin
    User 66583-Jan-02 8:22
    User 66583-Jan-02 8:22 
    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 
    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 

    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.