How to Call .NET C# DLL methods from unmanaged C++?

G

Guest

Well - from my initial research, this doesn't seem to be an easy solution if
it is doable at all.

I have .NET 2.0 C# Classes compiled into DLLs.

I also have older unmanaged (non-.NET) C++ code which I would like to have
use the code in those C# DLLs.

Anyone have a step-by-step simple example for doing such?

Thanks,
Jeff
 
B

Ben Voigt [C++ MVP]

Jeff said:
Well - from my initial research, this doesn't seem to be an easy solution
if
it is doable at all.

I have .NET 2.0 C# Classes compiled into DLLs.

I also have older unmanaged (non-.NET) C++ code which I would like to have
use the code in those C# DLLs.

Anyone have a step-by-step simple example for doing such?

Use the C++/CLI compiler, which turns any sort of C++ code into a .NET
application, and lets C++ make calls into other .NET languages.

For an example, choose New Project > Visual C++ > CLR > CLR Console
Application (or Windows Forms Application).
 
G

Guest

Thanks Ben. However, compiling the existing C++ unmanaged code using /CLI is
not an option.

We need to have a native C++ program using methods from a C# .NET DLL. Is
that possible?

Thanks,
Jeff
 
G

Guest

Jeff said:
Thanks Ben. However, compiling the existing C++ unmanaged code using /CLI is
not an option.

We need to have a native C++ program using methods from a C# .NET DLL. Is
that possible?

Make the .NET DLL a COM DLL and call that from your native C++ code.

Not easy. But possible.

Arne
 
B

Ben Voigt [C++ MVP]

Jeff said:
Thanks Ben. However, compiling the existing C++ unmanaged code using /CLI
is
not an option.

Why? What is your requirement that is not met by linking with /clr?
We need to have a native C++ program using methods from a C# .NET DLL. Is
that possible?

Knowing why you are required to have a "native" program will answer whether
C# can be used in that scenario.

Loading the .NET runtime into a native application to call C# is not (in any
meaningful way) so different from linking with /clr and having some native
and some managed parts.

In either case you have a dependency on having .NET installed, extra memory
used by .NET, and the possibility of the GC suspending any thread that does
not run purely native code (and the possibility to have purely native
threads the GC doesn't care about).
 

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