简体   繁体   English

Windows DLL在同一个应用程序中意外加载了多次

[英]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 wrote. 我编写的程序(在C#中)通过C ++ / CLI包装器库访问我也写过的本机(c ++)库。

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. 我注意到本机库的DllMain方法被多次调用,调试器认为多个内存位置连接到一些库调用。 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? 鉴于我的代码不使用LoadLibrary等函数,并且使用了包含库的.h文件和.lib的dll,为什么它不止一次初始化,我该怎么做才能避免它?

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. DLL的入口点在首次加载到进程时以及进程中的任何线程启动或停止时都会被调用。

If this is causing problem's in your DLLMain, then you need to check the reason for DLL entry and handle appropriately. 如果这导致DLLMain中出现问题,那么您需要检查DLL输入的原因并进行适当处理。

AFAIK DLL may not be loaded more than once into the same address space. AFAIK DLL可能不会多次加载到同一地址空间。 Even if you call LoadLibrary explicitly the DLL won't be loaded more than once, instead its reference counter is incremented. 即使您明确调用LoadLibrary ,DLL也不会被多次加载,而是会增加其引用计数器。

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. 在DLL绑定期间,单个导入的符号(函数或变量地址)仅由一个地址填充。

I believe you have one of the following: 我相信你有以下其中一种:

  • You may load simultaneously multiple versions of the same library (several DLL files). 您可以同时加载同一个库的多个版本(多个DLL文件)。 A typical error is to load debug and release DLL versions simultaneously. 典型的错误是同时加载调试和释放DLL版本。
  • Your DllMain is called several times, but it does not mean it's loaded several times. 您的DllMain被多次DllMain ,但这并不意味着它被加载了好几次。 Do you check the parameters supplied to the DllMain ? 你检查提供给DllMain的参数吗? Is it DLL_PROCESS_ATTACH , or perhaps it's just DLL_THREAD_ATTACH/DLL_THREAD_DETACH ? 它是DLL_PROCESS_ATTACH ,还是只是DLL_THREAD_ATTACH/DLL_THREAD_DETACH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM