简体   繁体   English

在C ++程序(MFC)中查找加载dll的位置

[英]Find where dll is loaded from in C++ program (MFC)

I have large legacy C++ application (Visual Studio 2010), and I need to execute some code before a specific dll is loaded. 我有大型的遗留C ++应用程序(Visual Studio 2010),我需要在加载特定的dll之前执行一些代码。 The problem is the dll is getting loaded before I execute any code, so I'm trying to figure out what's triggering it to load. 问题是在我执行任何代码之前dll正在加载,所以我试图弄清楚是什么触发它加载。

I've specified /DELAYLOAD for the dll in the link options, which should stop the dll loading before it's needed. 我已经为链接选项中的dll指定了/ DELAYLOAD,它应该在需要之前停止dll加载。 But it's still getting loaded before I execute any code. 但是在我执行任何代码之前它仍然被加载。 The application is MFC, so my entry point is an override of CWinApp::InitApplication(). 该应用程序是MFC,所以我的入口点是CWinApp :: InitApplication()的重写。

I suspect it must be a global variable in the application that's referencing a type in the dll, but I'm not sure how to find the variable (the code base is large, and globals are not consistently named). 我怀疑它必须是应用程序中引用dll中的类型的全局变量,但我不确定如何找到变量(代码库很大,并且全局变量不一致地命名)。

Any ideas how to find what's triggering the dll load, or how to find the global variable? 任何想法如何找到触发dll加载的内容,或者如何找到全局变量?

I fixed the problem by setting a breakpoint on the delay-load helper function, __delayLoadHelper2 . 我通过在延迟加载辅助函数__delayLoadHelper2上设置断点来解决问题。 This function can be found in: 此功能可在以下位置找到:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\delayhlp.cpp

It's called when a dll marked for delay-load is loaded. 当加载标记为延迟加载的dll时调用它。

I set the breakpoint in __delayLoadHelper2 and viewed the call-stack when it was hit. 我在__delayLoadHelper2设置了断点,并在被命中时查看了调用堆栈。 This showed the function in my code that was triggering the dll-load. 这显示了我的代码中触发dll-load的函数。

It was triggered by a global singleton constructor, which created a type from the dll. 它是由全局单例构造函数触发的,它从dll创建了一个类型。 This code gets executed before CWinApp::InitApplication() . 此代码在CWinApp::InitApplication()之前执行。

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

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