Click here to Skip to main content
11,921,643 members (67,716 online)
Click here to Skip to main content
Add your own
alternative version


66 bookmarked

HookImportFunctionByName v1.0

, 3 Mar 2000
Rate this:
Please Sign up or sign in to vote.
A class to hook any imported function call made by your app.
  • Download source files - 6 Kb
  • Introduction

    Enclosed is MFC source code for a function which can be used to hook any imported function call which your application makes. Since most of the Win32 API is implemented using import functions in dlls, this means that you hook Win32 API calls. This is useful when for example you want to be called for every call to the file system (::CreateFile() & CloseHandle()) which your app makes. This example of hooking the file system calls your app makes could form the basis of code to ensure you do not have any handle leaks in your application. You could also use this code to spy on COM port activity in remote processes by injecting the DLL into the remote process.

    The code is based on the code developed by John Robbins for his "BugSlayer" articles in the MSJ magazine. I have removed the dependencies on his other DLL functions, converted the code to MFC and addition of numerous ASSERTs.

    Planned Enhancements
    Contacting the Author


    V1.0 (24 December 1999)
    • Initial Public Release.


    The API consists of the single global function:



    BOOL HookImportFunctionsByName(HMODULE hModule, LPCSTR szImportMod, UINT uiCount, LPHOOKFUNCDESC paHookArray, PROC* paOrigFuncs, UINT* puiHooked);

    Return Value:
    TRUE if the specified API call(s) were hooked, therwise FALSE. To get extended error information, call ::GetLastError().


    • hModule -- This is the instance handle of the process calling the function. Normally in MFC you can obtain this from the function AfxGetInstanceHandle().
    • szImportMod -- This is the name of the module which contains the functions which you want to hook. e.g. for hooking file system calls, this would be KERNEL32.DLL.
    • uiCount -- This is the size of the paHookArray parameter.
    • paHookArray -- This is an array which specifies what functions to hook. The members of the HOOKFUNCDESC are "szFunc", which is the name of your function to hook, and "pProc", which is a function pointer to the function which you want to have called instead of normal unhooked case.
    • paOrigFuncs -- Upon successful return this will contain the original unhooked function pointers. These would be useful if you want to pass the request onto the original function after your hook function has been called.
    • puiHooked -- Upon return this will contain the number of functions which were hooked. This will be less than or equal to "uiCount".

    If you are hooked standard Win32 API calls then make sure that your hook function is using the right calling convention namely STDCALL. This is one of the most common problems encountered when using the hooking function.

    Planned Enhancements

    • Provide a sample app. I started using this code for a number of private contracts which I have been working on. Some ideas for sample apps would be a serial port monitor, a file system monitor to do the same as the Filemon application on the System Internals Web site.
    • If you have any other suggested improvements, please let me know so that I can incorporate them into the next release.

    Contacting the Author

    PJ Naughter
    24 November 1999


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    PJ Naughter
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralRe: A Problem Pin
    pjnaughter28-Mar-05 3:34
    memberpjnaughter28-Mar-05 3:34 
    QuestionPl Help!! Can I hook functions like BitBlt and CopyRect? Pin
    Daniel Kumar9-Oct-01 2:56
    memberDaniel Kumar9-Oct-01 2:56 
    AnswerRe: Pl Help!! Can I hook functions like BitBlt and CopyRect? Pin
    P J Naughter5-Nov-01 9:59
    memberP J Naughter5-Nov-01 9:59 
    GeneralWorks fine Pin
    Naden18-Sep-01 7:48
    memberNaden18-Sep-01 7:48 
    GeneralRe: Works fine Pin
    P J Naughter5-Nov-01 10:00
    memberP J Naughter5-Nov-01 10:00 
    GeneralHelp help help... Pin
    RG1-Aug-01 18:21
    memberRG1-Aug-01 18:21 
    GeneralThis is flawed by design! Pin
    Rezmond28-May-01 8:26
    memberRezmond28-May-01 8:26 
    GeneralRe: This is flawed by design! Pin
    Sven Wiegand25-Sep-01 21:51
    memberSven Wiegand25-Sep-01 21:51 
    GeneralRe: This is flawed by design! Pin
    P J Naughter5-Nov-01 10:03
    memberP J Naughter5-Nov-01 10:03 
    GeneralRe: This is flawed by design! Pin
    eMKa8-Nov-04 0:07
    membereMKa8-Nov-04 0:07 
    GeneralWow cool! Pin
    C4-Apr-01 9:11
    memberC4-Apr-01 9:11 
    GeneralRe: Wow cool! Pin
    P J Naughter5-Nov-01 10:06
    memberP J Naughter5-Nov-01 10:06 
    Generalsuggestion Pin
    anonymous18-Apr-00 7:43
    sussanonymous18-Apr-00 7:43 
    GeneralRe: suggestion Pin
    PJ Naughter18-Apr-00 12:38
    sussPJ Naughter18-Apr-00 12:38 
    GeneralRe: suggestion Pin
    anonymous19-Apr-00 7:46
    sussanonymous19-Apr-00 7:46 
    GeneralNeeds more detail example! Pin
    park sung hoon19-Mar-00 15:16
    susspark sung hoon19-Mar-00 15:16 
    GeneralRe: Needs more detail example! Pin
    Anonymous7-Aug-02 13:49
    sussAnonymous7-Aug-02 13:49 

    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.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.151120.1 | Last Updated 4 Mar 2000
    Article Copyright 2000 by PJ Naughter
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid