Calling C# from C++, Reverse P/Invoke, Mixed Mode DLLs and C++/CLI

前端 未结 3 450
故里飘歌
故里飘歌 2020-12-08 08:22

As I understand it I can use reverse P/Invoke to call C# from C++. Reverse P/Invoke is simply a case of:

  1. Create you managed (c#) class.
  2. Create a c++/
3条回答
  •  甜味超标
    2020-12-08 08:51

    Note, you can also do a IL roundtrip of the C# dll and export static methods, which work basically the same as the exports in C++/CLI. However, this is always a post-compile step, and it does have some caveats (which your C++/CLI export have too, btw.).

    You can ILDASM both the C# and the C++/CLI DLLs to see how exports are don; it is something like this (from a sample on the net):

    // unmexports.il
    // Compile with : ilasm unmexports.il /dll
    assembly extern mscorlib {}
    ..assembly UnmExports {}
    ..module UnmExports.dll
    // This flag is important
    ..corflags 0x00000002
    // This instructs the CLR to create a marshaling thunk for the unmanaged caller
    ..vtfixup [1] int32 fromunmanaged at VT_01
    ..data VT_01 = int32(0)
    ..method public static void foo()
    {
    ..vtentry 1:1
    ..export [1] as foo
    ldstr "Hello from managed world"
    call void [mscorlib]System.Console::WriteLine(string)
    ret
    }
    

提交回复
热议问题