Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1997-06-09
3
Medium Priority
?
568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

722 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