Solved

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

Posted on 1997-06-09
3
522 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 117
maxBlock challenge 30 129
building a service to accessed via telnet client, want colors ? 4 88
Path of Workbook 3 77
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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