using MFC/calling an MFC dll- in a Win32 project

Can I use MFC without the Application framework.

 I have an MFC ODBC DLL that exports some C-Style APIs that expects MFC objects (specifically, CTypedPtrArray<CObArray, CMyNonODBCClass* and CString) as its parameters.

 But now the catch is that the application that will be using this DLL is a Win32 Non-MFC Application, built with the same version of Visual C++ (5.0).

 Three solutions come to my mind.

 1. Changing some settings in the Non-MFC Application to make it use MFC.
 2. Porting the Application to MFC. It would require significant effort.
 3. Changing my DLL to only export APIs that do not require any MFC stuff.

 Although this requires some effort, it would be much less than #2.

I found out that option 1 is in fact quite trivial.

In the non-MFC Win32 application, I included <afx.h and <afxtempl.h and
then linked to MFC42D.lib

However, I'm still stuck trying to get rid of these warnings.
blah.h(51) : warning C4275: non dll-interface class 'CObject' used as base for dll-interface class 'CBlah'
blah.h(61) : warning C4251: 'm_LastName' : class 'CString' needs to have dll-interface to be used by clients of class 'CBlah'

CBlah is my class derived from CObject and defined in the DLL. I built the DLL project as an MFC project, dynamically linked to MFC, withDatabase support.

 Can someone please comment on this, and suggest the best way to approach
 this problem?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is a common problem most MFC developers face , when some non-MFC application tries accessing their DLL. The answer is pretty simple. You have to follow the following steps :
- Write wrapper API's for every export call you make.
Like if you have a call say
void XYZ::abcd( CString &str)
also make a C based call like :
void abcd( LPCSTR lpszString);
Put this wrapper in a seperate header and this the header all your non - MFC applications will use.
- Use extern "C" wherever possible.

- Statically, link your dll with MFC libraries. This way MFC*.DLL's don't interfere with your activities.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krishnamAuthor Commented:
Thanx for answering my question. What you are suggesting I do is something I'm aware of (option 3) and  what I might do only as a last resort.

Like I said, I've managed to make option 1 work (the preferred solution).
Both the Win32 application and the DLL now link to MFC Dyamically.
The application runs beautifully and I can excercise all the APIs, passing both the CString and the CTypePtrArray objects back and forth, as I please. I don't get even a single warning at compile time (improvement since I last posted this question)!

There still exists a problem though: When I terminate my application, I get an unhandled exception error. Here's the message I get:

Unhandled exception in application.exe (ODBC32.DLL): 0xC0000005: Access Violation
Good to hear that. Now the exception you are getting could be because of several reasons.
Might be some other code in the application causing this access violation.
The database might be still open before the ExitInstance().
The ODBC drivers might be causing a mismatch.

It is really hard to diagnose siting here. I would suggest using a tool like boundschecker to get deeper.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.