开发者

Windows DLL unexpectedly loading more than once in the same application

A program I wrote (in C#) accesses through a C++/CLI wrapper library a native (c++) library I also开发者_JAVA百科 wrote.

I noticed that the native library's DllMain method is called more than once, and the debugger thinks that multiple memory locations are connected to some of the library calls. From what I see, the library is initialized (and memory allocated) more than once.

Given that my code does not use functions such as LoadLibrary, and the dll is used including the library's .h files and .lib, why is it initialized more than once, and what can I do to avoid it?


AFAIK DLL may not be loaded more than once into the same address space. Even if you call LoadLibrary explicitly the DLL won't be loaded more than once, instead its reference counter is incremented.

Also I don't understand exactly what you mean by "debugger thinks that multiple memory locations are connected to some of the library calls". A single imported symbol (function or a variable address) is filled by just one address during DLL binding.

I believe you have one of the following:

  • You may load simultaneously multiple versions of the same library (several DLL files). A typical error is to load debug and release DLL versions simultaneously.
  • Your DllMain is called several times, but it does not mean it's loaded several times. Do you check the parameters supplied to the DllMain? Is it DLL_PROCESS_ATTACH, or perhaps it's just DLL_THREAD_ATTACH/DLL_THREAD_DETACH?


A DLL's entry point is called when it is first loaded into the process, as well as when any thread in the process is started or stopped.

If this is causing problem's in your DLLMain, then you need to check the reason for DLL entry and handle appropriately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜