Visual C++ .NET compatability with Visual C++

T

Tristan Sehgal

I am completley new to the .NET framework but have
a good understanding of Visual C++.

I am starting to develop a new class libary which other
programmers can use in their applications. To do this in
Visual C++, I'll be creating a DLL with exported classes.

I would like to develop this class libary in Visual
C++ .NET but am concerned it won't be able to be used by
standard Visual C++ developers.

If I created a DLL in .NET which makes use of some of the
..NET framework classes, could it be used in Visual C++
(assuming the header file presented hides away the .NET
class stuff)?

Any help and advice much appreciated.

Tristan.
 
T

Thijs

Hi,

As far as I know, you can mix unmanaged and managed C++ code. Just make sure
you only export unmanaged data structures and class interfaces, and although
I'm not absolutely sure, I think you'll be fine.

Things that might come in handy: the __pin keyword, see
http://www.codeproject.com/managedcpp/managed_types.asp?target=__pin for
more information

The gcroot<> template, see
http://www.codeproject.com/managedcpp/gcstl.asp?target=managed|stl for
more information on using STL in managed classes

I hope this helps.

Regards,

Thijs
 
C

Carl Daniel [VC++ MVP]

Tristan said:
I am completley new to the .NET framework but have
a good understanding of Visual C++.

I am starting to develop a new class libary which other
programmers can use in their applications. To do this in
Visual C++, I'll be creating a DLL with exported classes.

I would like to develop this class libary in Visual
C++ .NET but am concerned it won't be able to be used by
standard Visual C++ developers.

If I created a DLL in .NET which makes use of some of the
.NET framework classes, could it be used in Visual C++
(assuming the header file presented hides away the .NET
class stuff)?

I'm going to take a leap and assume that by "Visual C++" you mean VC6. If
by "standard Visual C++" you mean Visual C++ .NET, but generating unmanaged
(native) code only, then you can ignore the rest of this post, and take the
short answer: yes.

Short answer: yes: You can build a DLL with Visual C++ .NET 2002 or 2003
that can be used by "standard Visual C++ developers".

Long answer: It sounds like what you're wanting to do is produce a DLL
that's usable by programmers using a different version of Visual C++. This
can be done, but you have to be careful about the interface you expose.
Mixing compiler versions exposes you to all the same sorts of problems as
mixing compiler brands (e.g. Borland compiled DLL used from a VC compiled
EXE), with the added twist that mixing versions usually makes the problems
more subtle and easier to overlook.

To produce a DLL that can be used from any C/C++ compiler that can target
Windows, you need to restrict your exposed interface to a pure 'C'
interface, with all resource management handled explicitly through your
exposed APIs.

You have to assume that:
- Object layout between the two compilers might be incompatible.
- C Runtime library resources, such as <stdio.h> file handles are not
sharable across the interface.
- C++ runtime library resources, such as <iostream> streams are not sharable
across the interface.
- Heap allocations have permanent affinity with their original allocator
(e.g. if it's allocated by the DLL, it must be freed by the DLL).
- C++ name mangling is incompatible between the two systems, so only 'extern
"C"' functions can be exposed.
- Library designs in general will be incompatible: you can't pass
std::string, or CString, or ATL::CStringT<> across the interface.

If you adhere to these guidelines, you can produce a DLL that can be used by
anyone.

-cd
 
C

Carl Daniel [VC++ MVP]

Carl said:
To produce a DLL that can be used from any C/C++ compiler that can
target Windows, you need to restrict your exposed interface to a pure
'C' interface, with all resource management handled explicitly
through your exposed APIs.

One other option I forgot to mention: You could expose your objects through
COM interfaces. If you expose only COM interface, and live by the COM rules
correctly, you can make a DLL that can be used from another version of VC++
or even another language entirely.

-cd
 
T

Tristan Sehgal

Thanks very much for your help.

Along the same lines, I was wondering if a .NET component
can be created from unmanaged C++ code so that it could
be used by other .NET developers? Would there be any point
in doing this?

Many thanks

Tristan.
 
C

Carl Daniel [VC++ MVP]

Tristan said:
Thanks very much for your help.

Along the same lines, I was wondering if a .NET component
can be created from unmanaged C++ code so that it could
be used by other .NET developers? Would there be any point
in doing this?

A managed component (.NET class) can be created from Managed C++, but not
from unmanaged C++. It's possible to create an unmanaged class that wraps a
managed class, providing an unmanaged interface to that .NET class. This
can be useful, for example, to use .NET classes to build a plug-in for an
existing, native application.

-cd
 

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