user186246
user186246

Reputation: 1877

calling managed c# functions from unmanaged c++

How to call managed c# functions from unmanaged c++

Upvotes: 20

Views: 27633

Answers (4)

Simon
Simon

Reputation: 1684

I used C++/CLI wrapper classes described here and it was relatively easy to implement.

Upvotes: 1

Daniel Rose
Daniel Rose

Reputation: 17638

I used COM interop first, but by now I switched to IJW (it just works), as it is a lot simpler. I have a wrapper C++/CLR DLL (compile with /clr).

A simple example (using statics to make the calls easier):

namespace MyClasses       
{
    public class MyClass
    {
        public static void DoSomething()
        {
            MessageBox.Show("Hello World");
        }
    }
}

In the DLL I can reference namespaces as follows:

using namespace MyClasses;

And call it:

__declspec(dllexport) void CallManagedCode()
{
    MyClass::DoSomething();
}

Now you have an unmanaged DLL export "CallManagedCode" which calls into the managed code.

Of course, you also have to convert data between the managed/unmanaged boundary. Starting with VS2008, Microsoft includes a marshal-helper for converting between unmanaged and managed types. See http://msdn.microsoft.com/en-us/library/bb384865.aspx

Upvotes: 6

Robert Giesecke
Robert Giesecke

Reputation: 4314

Or use a project of mine that allows C# to create unmanaged exports. Those can be consumed as if they were written in a native language.

Upvotes: 11

ratty
ratty

Reputation: 13434

RE: How to call managed C# code from an unmanaged C++ application?

http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.interop/2005-05/msg00030.html

Calling Managed .NET C# COM Objects from Unmanaged C++ Code ...

http://www.codeproject.com/KB/cs/ManagedCOM.aspx

Wrapping a managed C# DLL in a unmanaged C++ project : dll .

http://www.experts-exchange.com/Programming/Languages/.NET/Q_22006727.html

Upvotes: 0

Related Questions