Click here to Skip to main content
Licence 
First Posted 3 Mar 2000
Views 46,018
Bookmarked 19 times

NiftyLoadLibrary and Rebasing DLLs

By | 3 Mar 2000 | Article
The home of NiftyLoadLibrary - and some notes on rebasing dlls.
  • Download source files - 3 Kb
  • Introduction

    This code demonstrates one method to rebase DLL's on both Windows 9X and NT platforms to prevent base address collisions.

    More Information

    The method used in NiftyLoad is very simple, and probably prone to a number of flaws. The NiftyLoadLibrary() function first loades the library using the LoadLibrary() API.It then checks to see if the dll was successfully loaded at its normal base address. If the Dll's base address is the load address the function returns the HINSTANCE of the DLL and the calling application proceeds normally. If the base and load address are detected to be diffrent, then the dll is unloaded, and a memory mapped file is created. NiftyLoadLibrary() reads the dll into the memory mapped file and performs the fixups required if the dll was loaded at the memory mapped sections base address. The complete image is then written to the DLL, the memory section is closed and LoadLibrary() is again called to load the address. If everything went to plan, the dll should now load at the address previously occupied by the memory section without any relocation fixups being performed by the OS.

    See the Platform SDK documentation on ReBaseImage() for more information.

    The current version of NiftyLoadLibrary() suffers from a number of flaws:

    1. The base address is chosen the wrong way. Rebased dll's should start from the top of the address space and work down. (ReBaseImage suggests base addresses between 0x68000000 to 0x78000000). I don't know how to search for available slots in the virtual memory though.
    2. It only know how to deal with images that have the same file section alignement as the host PC's natrual page size. (usually 4K). Any dll that cannot be mapped directly into memory will probably get trashed.
    3. NiftyLoad is unsuitalbe for situations where dll's are unloaded and reloaded frequently as it will rebase colliding dll'sinto the lowest available memory location which more than likley will be the preferred laod address for a recently released dll.
    4. NiftyLoadLibrary() obviosuly cannot help with implicitl loaded DLL's, or DLL's loaded as inproc COM servers.

    Please send any comments or bug reports to me via email. For any updates to this article, check my site here.

    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

    Chris Becke



    United States United States

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralCheck out MSDN for memory functions PinmemberSlowByte6:50 5 Feb '01  
    GeneralRe: Check out MSDN for memory functions PinsussAnonymous6:26 13 Jul '02  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120517.1 | Last Updated 4 Mar 2000
    Article Copyright 2000 by Chris Becke
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid