Share via


Why LoadLibrary is much slower than GetModuleHandle for dll that is already loaded?

This question is asked in our internal discussion list. One of the kernel developer answered the question below:

<quote>

LoadLibrary(AlreadyLoadedModule) has to increment the reference count of AlreadyLoadedModule, plus all of that module's dependencies, and their dependencies, recursively. Since the dependencies are recorded by name, not by a pointer to the loader entry, finding each dependency requires applying the isolation/redirection info in the activation context. For a module with a lot of imports, or imports which have their own large trees, this makes a LoadLibrary call quite expensive, even if it's already loaded.

GetModuleHandle is a better alternative.

<quote>

This is a FYI post. I won't answer any question.

Comments

  • Anonymous
    March 18, 2005
    GetModuleHandle is a better alternative.

    ??????????????
  • Anonymous
    March 18, 2005
    Interesting finds this week
  • Anonymous
    March 20, 2005
    The comment has been removed
  • Anonymous
    March 20, 2005
    Everything has to be discussed in a context.

    The context here is about performance, not about reliability.
  • Anonymous
    March 21, 2005
    Right, like I said, I couldn't speak for the context of this discussion. But I was just warning people that this is not a general solution for a faster version of LoadLibrary.

    You'd need to add extra code to make sure the libraries are never unloaded until all your GetModuleHandle callers have finished with it (for example, you'd call LoadLibrary the first time, then use GetModuleHandle subsequent times, then when you're all done you can do a FreeLibrary).
  • Anonymous
    May 16, 2005
    RePost:
    http://www.yeyan.cn/Programming/LoadLibraryGetModuleHandle.aspx