APIHijack - A Library for Easy DLL Function Hooking.

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:

// Hook structure.
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 )
{
 // When initializing....
 if ( fdwReason == DLL_PROCESS_ATTACH )  
 {
  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 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. The MSDN has a few good articles on Windows hooks, which are the preferred way to get an arbitrary DLL loaded into a process:

http://msdn.microsoft.com/library/techart/msdn_hooks32.htm

Also, the article from which this code is based shows another way to do it, which involves loading the process to be hooked as a debug target:

http://msdn.microsoft.com/library/periodic/period00/hood0200.htm

Downloads

Download source code and demo project - 102 Kb


Comments

  • Als men verliest de eigen gevoelens, hij of zij lijken aanvaardbaar Beats by Dr Dre oordopjes

    Posted by mrswanzi on 06/06/2013 03:12am

    [url=http://koptelefoon-monsterbeats.manifo.com/]Beats by dre Goedkope[/url] De urBeats zijn wederom ontstaan uit een perfecte samenwerking tussen Monster en Dr. Dre. Zodoende zijn ook deze in-ears verschenen onder het Beats By Dr. Dre label, waar al meer succesnummers onder zijn uitgebracht. Deze voorzien allen in unieke audioprestaties en een stijlvol design. Het is ook daarom dat deze urBeats de titel met trots mogen dragen! [url=http://koptelefoon-monsterbeats.webs.com/]beats by dre[/url] Beats By Dr. Dre heeft de Beats Wireless aangekondigd, een draadloze koptelefoon met Bluetooth. Deze heeft het design van de populaire Solo HD en kan eenvoudig worden gekoppeld met een smartphone of tablet. Het nieuwe model is ook voorzien van ControlTalk, met een microfoon in de oorschelp en alle benodigde bedieningsknoppen. Beats By Dr. Dre introduceert de Beats Wireless bluetooth hoofdtelefoonBeats By Dr. Dre introduceert de Beats Wireless bluetooth hoofdtelefoon die de luisteraar volledige muzikale vrijheid biedt. [url=http://koptelefoon-monsterbeats.weebly.com/]beats by dre[/url] Beats By Dre Kopen doe je bij een geautoriseerde dealer. Bij een geautoriseerde dealer weer je zeker dat je een originele beats by dre koopt. Tevens heeft het kopen bij een geautoriseerde dealer het voordeel dat je ook weet dat je garantie goed zit. De dealer handelt samen met jouw garantiekwesties af. Onofficiele gaan dit soort dingen het liefst uit de weg.

    Reply
  • En anden vigtig ting er at et ghd glattejern ikke bliver sÃ¥ varmt som andre glattejern

    Posted by motherdhmm on 05/30/2013 11:03am

    [url=http://www.blog.cheapbeatsbydre.co.nz/monster-headphone]monster beats headphone[/url] Alle GHD IV Pink Styler er af høj kvalitet og prisen er meget reasonable.There er forskellige stilarter for dig at select.Key funktioner i GHD Hårudglatningsmiddel omfatter: mere lydhør, dybt affjedret keramiske plader, at varme op endnu hurtigere. Hørbar bip, der fortæller dig, jernet er varmt og klar til brug. Længere og mere holdbar Kablet har et forbedret design for at mindske potentielle skader, som du stil med jern. Auto-justering af spænding betyder, at du kan bruge dit jern i noget land. Sikkerhed funktioner, herunder automatisk slukker efter 15 minutter uden aktivitet, og en gysen funktion, der forhindrer fugt beskadige din strygejern, når de er blevet efterladt i et koldt sted natten over. [url=http://www.buy-beatsdrdre.com/category/2012-dr-dre-beats-outlet]dr dre beats[/url] De fleste af damerne blindt har særskilte fodtøj området inde i det vilde hår generelt ender forårsager god skade hovedsagelig blot fordi dybest set afhentning låser inden det vilde hår vedrørende hairstyling. Samt hjælp vedrørende GHD Frizzy hår hår-styling stål, men for at blive urolige denne form for besvær. Først og fremmest fordi de præcise god hår dage Straighener faktisk ekspert produkt, som er helt sikkert desuden nedtonet, der har tendens til at idéen let at have din krøllede hår direkte fundamentalt har tendens til at den høje temperatur i den nøjagtige samme tilgang, fordi det effektivt oplysninger med formindske hoved af hår. [url=http://www.buy-beatsdrdre.com/category/2012-dr-dre-beats-outlet]dr dre beats[/url] Spil i en travl dags arbejde, den første ting, vi ønsker at prøve at slappe af, så varmt vandbad er den bedste måde at låse en dag træt. Når du vasker dit hår ingen måde til straks at slippe af med, så du brug for hjælp med en hårtørrer til at tørre dit hår. Nej hårtørrer, så er du nødt til at vente på håret til at tørre langsomt. På denne måde, et spild af tid ikke kan lide at tørre hår, hvordan man skal gøre, hvis du fange op med arrangementer. Så du skal du bruge en ghd hårtørrer. Hårtørreren, så længe du køber vores ghd håret, kan du nemt få. Vores ghd glat hår med en hårtørrer tilbehør, kort, smukke spejl, og ghd fladjern så specielt pakker, faciliteter, ikke alene kan spare dig for en pose, kan blæse dit hår tørre, og derefter bruge ghd glattejern til at oprette en ny frisure . Virkelig tjene to formål.

    Reply
  • Jordan shoes mentioned Gene to go for the variety, a margin of Nike

    Posted by TaddyGaffic on 04/24/2013 05:03pm

    In focus groups conducted at 80 community-based organizations around the country, Motivational Educational Entertainment of Philadelphia [url=http://markwarren.org.uk/goodbuy.cfm]nike free[/url] identified disturbing trends among youths ages 16 to 20. The "Just Say No" message of abstinence-only campaigns has been lost on this group of young people, who grew up during an era when the hip-hop sensibility of getting cash and clothes did not incorporate values of conscientious sexual behavior or social responsibility. Rappers who mingle [url=http://fossilsdirect.co.uk/glossarey.cfm]nike huarache free[/url] with glamorous-looking half-naked women rarely mention contraception while they're listing the number of compromising sexual situations they've been in lately. There are number of good brands in the market like Adidas, Nike, Reebok, Asics, Brooks, Puma etc. Most of these brands have shoes tailored for professional and amateur tennis players. New advanced technologies have made these shoes more player-friendly. After that, I eventually like to try to get down to the 150-160 range. I not entirely sure if that a healthy number for a gal my height or not, and I haven looked into it. But, I know that when I was 180 I still didn feel that [url=http://markwarren.org.uk/goodbuy.cfm]nike free run uk[/url] fit, so I think an extra 20 or so will help with that.. Lotto sneakers selling price are generally liable along with using the form of sneakers you choose on. Your Lotto sneakers established fact because of their style along with good quality plus the create. That they create sneakers coming from all varieties much like the loafers,Puma II Shoes, new sandals, sneakers along with task tennis shoes or anything else

    Reply
  • problem in hooking ntshrui.dll apis

    Posted by sameen_f on 07/01/2009 06:35am

    hi, i have tried this code it is working fine but when i tired to hook other apis then it showed no output...i need help in how to hook apis from ntshrui.dll....or is there any need for any kind of driver with the given source code...

    Reply
  • APIHijack for IDirect3D8::CreateDevice ????

    Posted by boonchait on 12/04/2004 03:08pm

    How can i set APIHijack for IDirect3D8::CreateDevice.

    Reply
  • can't highjack TextOut well from gdi32.dll

    Posted by Legacy on 02/12/2004 12:00am

    Originally posted by: Benjamin Gao

    when i highjack TextOut function in notepad.exe or wordpad.exe,nothing happened.
    not TextOut you wnat to TextOutA and TextOutW

    Reply
  • how can I hook Com ports in Win2000,WinXP?

    Posted by Legacy on 02/04/2004 12:00am

    Originally posted by: Gokay Gursoy

    Dear Sir/Madam
    How can I hook com ports (Com1/Com2) in Win2000/WinXP?
    I want to see data incoming and data outgoing from my com1
    and com2.I would be very pleased if you send me some delphi
    source about this.
    Best Regards
    Gokay Gursoy

    Reply
  • How do i Hook WM_CHAR in WinXP?

    Posted by Legacy on 01/30/2004 12:00am

    Originally posted by: uqtur

    Hello !
    I write a hook with VC 6.0 in Win2K and it worked .
    but in Win XP It dosn't worked functionally.
    my purpose is display some UNICODE characters in
    correctly with this HOOK.can any one help me?

    thank you .
    ////

    static LRESULT WINAPI MyApp ( int nCode, WPARAM wParam, LPARAM lParam)
    {
    ...

    pwszKLID=malloc(10);
    msg=(LPMSG)lParam;
    //
    switch (msg->message)
    {
    case WM_KEYDOWN:
    My6=msg->wParam;
    GetKeyboardState(st);
    break;
    case WM_CHAR:
    {
    if ((msg->wParam)==63) //can't display correctly ,replace with '?'
    {
    switch (My6)
    {
    case 71://g
    SendMessage(msg->hwnd,WM_CHAR,234,0);
    PostMessage(msg->hwnd,WM_KEYDOWN,8,0);
    break;
    case 73://i
    SendMessage(msg->hwnd,WM_CHAR,188,0);
    PostMessage(msg->hwnd,WM_KEYDOWN,8,0);
    break;
    ....
    }
    }
    break;
    }
    }
    //
    lResult = CallNextHookEx(g_hhook, nCode, wParam, lParam);
    return(lResult);
    }

    ////

    Reply
  • Error when compile

    Posted by Legacy on 01/28/2004 12:00am

    Originally posted by: BigWizard

    Deleting intermediate files and output files for project 'TestDLL - Win32 Debug'.
    --------------------Configuration: TestDLL - Win32 Debug--------------------
    Compiling...
    apihijack.cpp
    c:\temp\3\apihijack.cpp(158) : error C2440: '=' : cannot convert from 'unsigned long' to 'void *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    c:\temp\3\apihijack.cpp(196) : error C2440: '=' : cannot convert from 'unsigned long *' to 'unsigned long'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\temp\3\apihijack.cpp(203) : error C2446: '>' : no conversion from 'unsigned long *' to 'unsigned long'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\temp\3\apihijack.cpp(203) : error C2040: '>' : 'unsigned long' differs in levels of indirection from 'unsigned long *'
    c:\temp\3\apihijack.cpp(204) : error C2440: '=' : cannot convert from 'unsigned long *' to 'unsigned long'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    dllmain.cpp
    Error executing cl.exe.

    TestDLL.dll - 5 error(s), 0 warning(s)

    Reply
  • How do i hook a API that exported with ordinal rather than Name,certenly,I know it's protype!

    Posted by Legacy on 01/23/2004 12:00am

    Originally posted by: Hellow

    How do i hook a API that exported with ordinal rather than Name,certenly,I know it's protype!
    

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds