Qt Application, VS2008 and “requires MFC shared dll version”


I recently ran into a problem where I needed to build a Qt application that depended on a library that made use of MFC.  I started by building the Qt side (GUI, etc) with QtCreator and then proceeded to import the .pro file into VS2008 using the standard VS Qt plugin before adding the mentioned native Visual C++ library to the Visual Studio solution.

The first issue I ran into was a compilation error:

Error 1 fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\include\afx.h 24

This was an easy one to fix, it simply required a change in the main project’s properties to “Use MFC in a Shared DLL”:

usemfc

Unfortunately, the next problem was significantly harder to solve.  My solution now built fine, but as soon as I tried to run it, it crashed with an “Unhandled exception at 0x78a5b48a (mfc90d.dll) in myApplication.exe: 0xC0000005: Access violation reading location 0x00000000.” error which turned out to be at line 37 in “winmain.cpp”:

if (!pThread->InitInstance())
{
   ...
}

Obviously pThread was null, but why and what to do?

After a bit of searching, I found that a number of people had had similar issues, but unfortunately none of them were using Qt and most of the solutions seemed overly complex or not relevant to my particular scenario. I eventually found an article that pointed out that the reason for the null pThread object was that the CWinApp constructor was never called (for those interested, this is that article).

After some more thinking and struggling, I figured that, if the CWinApp constructor isn’t called, it might very well be due to the the application entry point. Now I was onto something good. The solution was to change the entry point to from “int main” to “int APIENTRY WinMain”, i.e. from

int main(int argc, char *argv[])
{
   QGuiApplication app(argc, argv);
   ...
   return app.exec();
}

to

int APIENTRY WinMain( HINSTANCE instance, HINSTANCE, LPSTR, int )
{
   QGuiApplication app( __argc, __argv );
   ...
   return app.exec();
}

And that was it, everything is running as it should (for now at least).

Advertisements

2 Replies to “Qt Application, VS2008 and “requires MFC shared dll version””

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s