.NET calling VC6 dll that uses MFC

G

geekworx

I have a .NET managed C++ application that needs to call a 3rd party
dll that was built with VC6 and returns MFC types. e.g.

int GetSomeStrings(CStringArray *pStringArray);

The problem I'm having is the destructor of the CStringArray is
crashing. Is it possible to call VC6 dlls that use MFC from .NET?

Thanks
 
G

Guest

I have a .NET managed C++ application that needs to call a 3rd party
dll that was built with VC6 and returns MFC types. e.g.

int GetSomeStrings(CStringArray *pStringArray);

The problem I'm having is the destructor of the CStringArray is
crashing. Is it possible to call VC6 dlls that use MFC from .NET?

Seeing as no answered yet I'll take a shot.

your dll is built against MFC42.
VS2005 ships with MFC80.

The first problem is that the types don't match, which can be a problem
especially in constructors and destructors since that is where most
allocation/deallocation is done.

Maybe you could try (don't know if it will work or not) to point your
include and lib directories for that project to the VC6 folders. However that
would still mean they use a different CRT runtime version.

And that is the second problem.
assuming you would get it to work, the limitation is that none of the 2
components should allocate or deallocate memory of an object that was created
in the other component.

If your dll adds elements to an array, which is then deleted by your VC2005
program, your program crashes because it will try to delete memory that it
does not own. The same goes for modifying string data etc. anything in your
dll that allocates or deallocates memory in an object that was created by
your app.

The best option would be to recompile the dll in VS2005, if possible.

This is the general problem with dlls. in order to keep them portable across
multiple compiler versions you have to restrict it to only use C style
functions, and always keep memory ownership inside the component where it was
allocated.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
C

Carl Daniel [VC++ MVP]

I have a .NET managed C++ application that needs to call a 3rd party
dll that was built with VC6 and returns MFC types. e.g.

int GetSomeStrings(CStringArray *pStringArray);

The problem I'm having is the destructor of the CStringArray is
crashing. Is it possible to call VC6 dlls that use MFC from .NET?

To reuse such a DLL without rebuilding it with VC8, I'd recommend something
like the following:

Design a wrapper DLL with a pure C interface that never passes CRT, MFC, ATL
or STL objects across the DLL interface. You'll need to come up with
suitable wrappers for all of the public functions of the DLL you're trying
to re-use.

Build that wrapper DLL using VC6 (so it's built w/MFC42 and the VC6 CRT),
then use that wrapper (and, indirectly, your VC6 MFC DLL) from your VC8
program.

If there's a very large interface to the DLL, this could get to be a lot of
work - but in general, you can't mix MFC, ATL or STL from different compiler
versions, so if you can't rebuild the DLL with VC8, this might be your best
shot.

-cd
 
A

Asfar

Hi,

Do you have a small example for building a wrapper dll. This would be of a
great help for reference.

Thanks,
-Asfar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top