calling managed c# functions from unmanaged c++

前端 未结 4 1844
灰色年华
灰色年华 2020-12-08 11:23

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

4条回答
  •  醉话见心
    2020-12-08 12:08

    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

提交回复
热议问题