How does MFC's wWinMain end up in the executable?
Asked Answered
D

3

7

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?

Dorise answered 29/7, 2011 at 10:34 Comment(0)
F
10

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().

Fledgy answered 29/7, 2011 at 11:30 Comment(1)
The reason for the question is I wanted to override some of the initial startup in MFC. My initial thought was to go for WinMain, but now I know I can use AfxWinMain instead.Dorise
A
2

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.

Adhesion answered 30/7, 2011 at 13:24 Comment(3)
Hi, thanks for your answer, but that didn't actually answer my question.Dorise
You mean the inclusion of appmodul.obj ?Adhesion
Yes pretty much, I didn't realise MFC consisted of a static library and dynamic library.Dorise
S
1

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.

Superfecundation answered 29/7, 2011 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.