By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

.NET calling VC6 dll that uses MFC

P: n/a
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

Mar 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 21 '06 #2

P: n/a
ge******@gmail.com wrote:
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
Mar 21 '06 #3

P: n/a
Hi,

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

Thanks,
-Asfar

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uc**************@TK2MSFTNGP12.phx.gbl...
ge******@gmail.com wrote:
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

Mar 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.