Click here to Skip to main content
6,630,586 members and growing! (18,211 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » General     Intermediate

HookImportFunctionByName v1.0

By PJ Naughter

A class to hook any imported function call made by your app.
VC6, Visual Studio, MFC, Dev
Posted:3 Mar 2000
Views:143,980
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.42 Rating: 4.32 out of 5

1

2
1 vote, 12.5%
3
2 votes, 25.0%
4
5 votes, 62.5%
5
  • 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.


    History
    API
    Planned Enhancements
    Contacting the Author


    History

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


    API

    The API consists of the single global function:

    HookImportFunctionsByName

    HookImportFunctionsByName

    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().

    Parameters:

    • 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".

    Remarks:
    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
    Email: pjn@indigo..ie
    Web: http://www.naughter.com
    24 November 1999


    License

    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


    Member

    Location: United States United States

    Other popular DLLs & Assemblies articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 42 (Total in Forum: 42) (Refresh)FirstPrevNext
    GeneralThanx for the article Pinmemberbnc20:58 2 May '09  
    QuestionSerial port monitor PinmemberMariano Capezzani11:52 30 Apr '08  
    NewsThanks for Post Pinmembersatish_abiram5:26 11 Jul '07  
    Generalneed help serial port hooking Pinmemberfionashrek033:22 28 Jun '06  
    GeneralMonitor WriteProcessMemory PinmemberVitoto7:50 12 Jan '06  
    GeneralWho can give me a example of use HookImportFunctionByName v1.0? Pinmemberdimu3:31 28 Mar '05  
    Generalkernel hooking pleas Pinmembereslamspider23:01 26 Mar '05  
    GeneralRe: kernel hooking pleas Pinmemberpjnaughter2:38 27 Mar '05  
    GeneralCAPIHook another Process Pinmemberneo26007777:01 28 May '04  
    GeneralRe: CAPIHook another Process Pinmemberpjnaughter7:33 28 May '04  
    GeneralRe: CAPIHook another Process Pinmemberneo26007779:06 28 May '04  
    GeneralRe: CAPIHook another Process Pinmemberpjnaughter10:42 28 May '04  
    GeneralRe: CAPIHook another Process Pinmemberneo260077712:29 28 May '04  
    GeneralCan't hook into the kernel under Win9x PinmemberHotFox17:29 8 Oct '03  
    GeneralRe: Can't hook into the kernel under Win9x Pinmemberpjnaughter3:30 9 Oct '03  
    GeneralRe: Can't hook into the kernel under Win9x Pinsussvawksel0:41 11 Oct '03  
    GeneralRe: Can't hook into the kernel under Win9x Pinsussvawksel0:43 11 Oct '03  
    GeneralRe: Can't hook into the kernel under Win9x Pinmemberpjnaughter7:04 11 Oct '03  
    GeneralRe: Can't hook into the kernel under Win9x Pinmembervawksel10:50 11 Oct '03  
    GeneralRe: Can't hook into the kernel under Win9x Pinmemberpjnaughter5:07 12 Oct '03  
    GeneralA Problem PinmemberKevinBai16:52 13 Jan '02  
    GeneralRe: A Problem PinmemberTom Daffin16:40 24 Apr '03  
    GeneralRe: A Problem Pinmemberespboy4:29 18 May '03  
    GeneralRe: A Problem Pinmemberpjnaughter6:54 18 May '03  
    GeneralRe: A Problem Pinmemberdimu3:24 28 Mar '05  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 3 Mar 2000
    Editor: Valerie Bradley
    Copyright 2000 by PJ Naughter
    Everything else Copyright © CodeProject, 1999-2009
    Web10 | Advertise on the Code Project