MFC 的 wWinMain 如何最终出现在可执行文件中?

发布于 2024-11-26 20:28:56 字数 235 浏览 6 评论 0原文

在 MFC 中,wWinMain 是在 appmodul.cpp 中定义的。据我所知,该文件内置于 mfc90ud.dll 中。但是,当我运行应用程序时,调用堆栈显示 MyApplication.exe!wWinMain。它是如何获取在 appmodul.obj 中导出的 wWinMain 函数并将其放置在我的应用程序中的?

In MFC, wWinMain is defined in appmodul.cpp. This file is built into mfc90ud.dll from what I can see. However, when I run my application, the call stack shows MyApplication.exe!wWinMain. How has it taken the wWinMain function that was exported in appmodul.obj and placed it in my application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

菩提树下叶撕阳。 2024-12-03 20:28:56

在“解决方案资源管理器”窗口、“属性”、“链接器”、“命令行”中右键单击您的项目。在“其他选项”框中键入 /verbose。重建您的项目。输出窗口现在显示链接器找到符号的位置的跟踪。搜索“winmain”可以找到这个:

1>    Searching c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\lib\mfcs90ud.lib:
1>      Found _wWinMain@16
1>        Referenced in msvcrtd.lib(wcrtexew.obj)
1>        Loaded mfcs90ud.lib(appmodul.obj)

注意库名,mfcs90ud.lib是一个静态链接库。如果您搜索“mfcs90ud.lib”,那么您还可以看到如何引用该库:

1>Starting pass 1
1>Processed /DEFAULTLIB:mfc90ud.lib
1>Processed /DEFAULTLIB:mfcs90ud.lib
1>Processed /DEFAULTLIB:msvcrtd.lib
etc..

如果您在 MFC 源代码中搜索“mfcs”,您将找到如何注入此 /defaultlib 选项。来自 afx.h:

            #ifdef _DEBUG
                    #pragma comment(lib, "mfc" _MFC_FILENAME_VER "ud.lib")
                    #pragma comment(lib, "mfcs" _MFC_FILENAME_VER "ud.lib")
            #else
                    #pragma comment(lib, "mfc" _MFC_FILENAME_VER "u.lib")
                    #pragma comment(lib, "mfcs" _MFC_FILENAME_VER "u.lib")
            #endif

长话短说,MFC 应用程序链接两个库。 Mfc90u.lib是MFC DLL版本的导入库。 Mfcs90u.lib 是一个静态链接库,其中包含链接到可执行文件中的位。包括WinMain()。

Right-click your project in the Solution Explorer window, Properties, Linker, Command Line. Type /verbose in the "Additional Options" box. Rebuild your project. The Output window now shows a trace of where the linker found a symbol. Search it for "winmain" to find this:

1>    Searching c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\lib\mfcs90ud.lib:
1>      Found _wWinMain@16
1>        Referenced in msvcrtd.lib(wcrtexew.obj)
1>        Loaded mfcs90ud.lib(appmodul.obj)

Note the library name, mfcs90ud.lib is a static link library. If you search for "mfcs90ud.lib" then you can also see how that library got referenced:

1>Starting pass 1
1>Processed /DEFAULTLIB:mfc90ud.lib
1>Processed /DEFAULTLIB:mfcs90ud.lib
1>Processed /DEFAULTLIB:msvcrtd.lib
etc..

If you search the MFC source code for "mfcs", you'll find how this /defaultlib option got injected. From afx.h:

            #ifdef _DEBUG
                    #pragma comment(lib, "mfc" _MFC_FILENAME_VER "ud.lib")
                    #pragma comment(lib, "mfcs" _MFC_FILENAME_VER "ud.lib")
            #else
                    #pragma comment(lib, "mfc" _MFC_FILENAME_VER "u.lib")
                    #pragma comment(lib, "mfcs" _MFC_FILENAME_VER "u.lib")
            #endif

Long story short, an MFC app links two libraries. Mfc90u.lib is the import library for the DLL version of MFC. Mfcs90u.lib is a static link library that contains the bits that get linked into your executable. Including WinMain().

只为一人 2024-12-03 20:28:56

魔法是由 CWinApp 的构造函数完成的:

  1. 您声明此类型的全局变量(大部分是派生类型)。
  2. CWinApp::CWinApp 被调用(任何主例程之前)。
  3. 它设置了一些数据结构,稍后由 AfxGetApp() 返回 - 这是相当复杂的东西!
  4. wWinMain 被调用,它利用 CWinApp 构造函数设置的数据结构。

尝试从 wWinMain 或自定义 CWinApp 派生的构造函数进行调试。

The magic is done by CWinApp's constructor:

  1. You declare a global variable of this type (mostly of derived type).
  2. CWinApp::CWinApp gets called (before any main-routine).
  3. It sets up some data-structure, which is the later returned by AfxGetApp() - it's quite complicated stuff!
  4. wWinMain gets called, which utilizes the data-structure setup by the CWinApp constructor.

Try debugging from wWinMain, or from your custom CWinApp-derived constructor.

一个人的旅程 2024-12-03 20:28:56

链接器完成了这一切。它获取所有目标文件、库文件并匹配它需要的内容。除了装饰之外,它还可以将正确的声明与定义等相匹配。

The linker does all that. It takes all the object files, library files and matches up what it needs. Along with the decorations it can match the right declarations to definitions etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文