Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1997-06-09
3
Medium Priority
?
583 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Screencast - Getting to Know the Pipeline
Suggested Courses

971 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