Re: Switching to MFC in shared DLL brings runtime error
On Fri, 4 Jul 2008 01:39:02 -0700 (PDT), wba <we.blaze.away@gmail.com>
wrote:
Hello,
My Visual Studio 2005 solution is composed by many projects: an exe,
some dynamic dlls and some static libs.
All of them use MFC in a static library and I need to switch to MFC in
a shared DLL.
For the EXE and DLLs, this is not a small change. It will cause all of them
to use the same MFC and CRT code and data. I've only ever used MFC
"extension DLLs", which require dynamic linking to MFC (and thus the CRT as
well); if you are using "regular DLLs", you may have other changes to make
WRT compiler settings, assuming it'll work at all, which I don't know
offhand. You might want to create some dummy projects of the desired types
and compare settings.
The compilation is fine but when I debug the application I got the
error:
"Debugger: An unhandled non-continuable exception was thrown during
process load"
No more hints, and it is not possible to execute any piece of code.
Not familiar with that error message. Plug it into the google, and you
might get some hints.
I then analyzed my exe files with Dependency Walker to see if there
are errors while loading dlls.
I find out that in two of my dynamic dlls some function (but not all)
are not exported at all (and they were while compiling using MFC in a
static library) and I guess that this is the cause of the runtime
error.
I've no idea why some functions are correctly exported and others are
not. I can only add that it seems that only the functions that deal
with CString are not exported.
How do you export the functions? For each DLL, you should do something
like:
#ifdef COMPILING_X_DLL
#define X_EXPORT __declspec(dllexport)
#else
#define X_EXPORT __declspec(dllimport)
#endif
Then in each DLL, you replace "X" with a suitably unique identifier; often
the name of the DLL will do. You export functions as:
X_EXPORT void f();
and whole classes with:
class X_EXPORT X
{
};
Note that you must *not* use AFX_EXT_CLASS et.al., because they have no
chance of working correctly when you have multiple DLLs all trying to use
them.
--
Doug Harrison
Visual C++ MVP