如何在mfc中加载动态dll的多个实例
我想知道是否有一种方法可以显式加载 DLL 的多个实例(即使用 LoadLibrary 调用)。通常,当调用 LoadLibrary 时,进程仅加载 DLL 一次(以便该库的单个副本驻留在进程的内存中),并且对同一库完成的所有后续的 LoadLibrary 调用将仅返回该库的相同句柄,由第一次调用返回。更具体地说,我希望能够为进程内创建的每个线程加载一个单独的 DLL 实例。目前,如果我在每个线程中执行 LoadLibrary,我只会获得指向驻留在进程地址空间中的库的单个实例的相同指针,并且所有线程实际上都引用库中的相同函数(这里发生了很大的混乱)在我的应用程序中)。相反,我希望每个线程在单独的内存空间中创建库的单独实例,以便线程在从库调用函数时不会相互干扰。
I was wondering is there a way to load multiple instances of a DLL explicitly (i.e. with LoadLibrary call). Normally, a process loads a DLL just once when LoadLibrary is called (so that a single copy of the library resides in the process' memory) and all consequent calls of LoadLibrary done for the same library would be returning just the same handle of the library, returned by the very first call. To be more specific, I want to be able to load a separate instance of a DLL for each thread created inside of a process. Currently, if I do LoadLibrary in each thread, I just get the same pointer to a single instance of the library residing in the process' address space, and all threads in fact refer to the same functions from the library (here a big mess occurs in my app). Instead, I want each thread to create an individual instance of the library in a separate memory space so that the threads don't interfere with each other when calling functions from the library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不可能的(直到您创建一个新进程并在该进程中加载 DLL)。 DLL 在进程级别加载(因此在进程控制块中有条目)。尽管您的 DLL 可以检测主机应用程序中是否创建了新线程,以便它可以在 DLLMain 方法中初始化 TLS 特定数据。
I don't think that is possible (until you create a new process and load the DLL in that process). DLLs are loaded at the process level (hence have entries in Process control block). Although your DLL can detect if new thread is created in the host application so that it can initialize TLS specific data in DLLMain method.