Click here to Skip to main content
Click here to Skip to main content

NiftyLoadLibrary and Rebasing DLLs

By , 3 Mar 2000
 
  • 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
    No Biography provided

    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

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralCheck out MSDN for memory functionsmemberSlowByte5 Feb '01 - 6:50 
    GeneralRe: Check out MSDN for memory functionssussAnonymous13 Jul '02 - 6:26 

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

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