20

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

1

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, This looks very interesting. Do you have a version for VS 2010, or can you point me to what changes to do to make it available for VS 2010 / Net 4? Thanks
Should work just fine (I use it myyself in 2010), you may want to change the target framework of the newly created project to 4.0, though. VS 2010 defaults to .Net 2.0 when no target framework setting is present.
Thanks, great. I saw that (I created a sample test empty project, but not tested if it works yet)
Hallo Robert. I'm using your project and everything works fine, except when C# function returns string[]. I get an error: Unhandled Exception: System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'return value': Invalid managed/unmanaged type combination. On your project page, there's no example when dealing with arrays as return type. Is there any example where I can see how to fetch array of strings in C++? Thanks in advance.
Your link is dead.
6

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

4 Comments

You do not really have to go down that route manually. Check out the link in my reply. It is entirely possible to have the counterpart of DllImport in c#, including all the marshaling goodness that comes with .Net. My MSBuild task adds the appropriate vt-fixups after the build and you do not need to deploy another assembly...
I tried it: The DLL has the exports. However, how am I supposed to use it? I did not find a .lib to allow me to link to the DLL.
I don't use C++, but I guess it isn't any different than like you would for any library that doesn't come with C/C++ headers: Write the header for the functions you want to import yourself. As i said, I don#t use C++. It is pretty straightforward from C or Delphi.
The headers isn't the problem. To link to the library you need a .lib, otherwise you'd have to use LoadLibrary() and GetProcAddress() for every call. I "solved" it by manually generating a .def and generating a .lib from that, as in support.microsoft.com/kb/131313 Also, having a method call itself failed (standard use case when making the call UI-thread-safe). Could I perhaps contact you via email? I didn't find an address anywhere.
1

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

1 Comment

Please post here a solution or at least a summary of it. The link can become broken in the future. And also it's easier to read all solutions here. If they are more than one you can post them as multiple answers. Thanks! ;)
0

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

2 Comments

I followed second link which you have provided and successfully compile the cpp file but failed to run it . It is failing at CreateInstance() using smartpointer..... so iam unable to call functions of c# dll
Please post here a solution or at least a summary of it. The link can become broken in the future. And also it's easier to read all solutions here. If they are more than one you can post them as multiple answers. Thanks! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.