This article is a solution of using VS2010(2013) to build Apps which based on MFC4.2 . It is more like a geek’s trick sharing than a tech solution . It is also a little gift to those who are still “in love” with VC6 and MFC4.2 , just like me.
MFC version 4.2 is the develop kit which shipped with Microsoft Visual Studio C++ 6.0 ,aka VC6 .
MFC version 10.0 is the one which shipped with Microsoft Visual Studio 2010, aka VS2010.
Compare to VS2010, VC6 has a lot of flaws which make developers unhappy.
But, VC6 has a big advantage , which is : MFC42(u).dll MSVCRT.dll MSVCP60.dll are shipped with Windows OS since Windows XP. This means you don’t have to distribute of these common run-time DLLs by your self.
On the other hand,if you build an app with VS2010, you have to pack/deliver MFC100(u).dll MSVCR100.dll MSVCP100.dll together with your app. It is very inconvenient , especially when we just write and share a little tool which size is only 50k to our friends.
So , it is a good idea to use VS2010 to build a app which based on MFC4.2 .
How To Build
Before we start , we need to download and install the latest Windows Server 2003 SP1 Platform SDK(http://www.microsoft.com/en-us/download/details.aspx?id=15656).
We install it at D:\PlatformSDK .
Then we can create a new MFC App with VC6 , and open it with VS2010. Click Yes to convert VC6 project to VS2010 VC++ project.
After that, by configuring the “VC++ Directories” of the VS2010 VC++ project, we can compile your project with MFC4.2 header files.
a) Set Executable Path to $(ExecutablePath).
b) Set Include Path to d:\PlatformSDK\Include;d:\PlatformSDK\Include\mfc;d:\PlatformSDK\Include\atl;d:\PlatformSDK\Include\crt
c) Set Reference Path to EMPTY.
d) Set Library Path to d:\PlatformSDK\Lib;d:\PlatformSDK\Lib\MfcLib_x86
e) Set Exclude Path to EMPTY.
The directory d:\PlatformSDK\Lib\MfcLib_x86 have to be make by yourself. And then, copy all files from D:\Program Files\Microsoft Visual Studio\VC98\MFC\Lib into it. Then copy msvcrt.lib msvcrtd.lib msvcprt.lib msvcprtd.lib from D:\Program Files\Microsoft Visual Studio\VC98\Lib into MfcLib_x86 just been made. (Assume that VC6 is installed at D:\Program Files\Microsoft Visual Studio)
Now we can start build our MFC app with MFC4.2 class library.
Project should be compiled successfully. But the Link.exe (aka Linker) will report many errors just like this:
error LNK2001: unresolved external symbol ___security_cookie
error LNK2001: unresolved external symbol ___report_gsfailure
error LNK2001: unresolved external symbol __except_handler4
error LNK2001: unresolved external symbol __NLG_Notify
error LNK2001: unresolved external symbol __NLG_Destination
error LNK2019: unresolved external symbol @__security_check_cookie@4
This symbols are automatically used by the compiler of VS2010. And apparently they are not supplied by MFC4.2. To solve these problems, I extract all the obj files in msvcrt.lib of VS2010 (which is in $VS2010_Install_Dir\VC\lib), then search those unresolved symbols in these obj files.
After a long time of analyzing and searching, I’ve successfully solved all linker errors with these obj files which I’ve extracted. Trust me, it really a hard work. Anyone could try it if not believe in me.
Now the project could be compiled and linked successfully.
How to verify the result?
By using the Dependency Walker, Depends.exe(a tool which shipped in VC6, could also be downloaded from internet), we could find out which DLLs are statically linked in our app.
Just like this:
The screenshot clearly show us that MFCAPP.exe only use the MFC4.2 DLLs, instead of MFC10.0 DLLs.
Using the code
I’ve packed all objs files which required by VS2010 into a Static LIB file. Everyone could download it from this article. So you have no need to do those hard and boring works again as I did. And I would be glad to see any discussion and feedback.
What you only need to do is to add two lines in your code:
If your App was build in a Release and Non-Unicode(MBCS) mode, then you might meet a linker error LNK2019: unresolved external symbol
__CrtDbgReport. In this case , use the macro “
CRTNEW_RESOLVE_CRTDBGREPORT_LINK_ERROR();” at any place and this error will be solved.
Now , enjoy your MFC4.2 developing with VS2010 or VS2013
By the way, you could use Lambda expressions and other C++ 11 features in your MFC4.2 project now. Is it cool enought to you ?
Points of Interest
The compiler and linker are both working under strict rules. I believed it much more after I searched 1000+ obj files.
2014/4/24 Post the first version