Solved

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

Posted on 1997-06-09
3
512 Views
Last Modified: 2013-11-20
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?

 Thanx!

 Krishna
0
Comment
Question by:krishnam
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
mbhakta earned 50 total points
ID: 1302047
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.

0
 

Author Comment

by:krishnam
ID: 1302048
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
0
 
LVL 4

Expert Comment

by:mbhakta
ID: 1302049
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.

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now