Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

How to implement interface of C# use COM of native C++?

$
0
0

I'm using .Net Core in cross platform, include windows and linux.

I can't use ATL.

Iembed the.Net core to myexecutableprogram.

I hope expose c++ class instance pointer to .Net Core(C#), and convert to interface of C#.

C++:

class foo : public IDispatch, public IManagedObject, public IInspectable...{} fobj;

extern "C" __declspec(dllexport) foo* WINAPI GetTestObject(){return new foo;}

C#:

[DllImport("foo.dll")]

static extern IntPtr GetTestObject();

[Guid("48DF8E89-57DE-3599-AD7C-B49500EF01C0")]
interface Test
{
   int func();
}

main(){

    var v = GetTestObject();
    obj = (Test)Marshal.GetObjectForIUnknown(v);
    obj.func();// exception

    dynamic dobj = Marshal.GetObjectForIUnknown(v);

    dobj.func();// OK, IDispatch::Invoke be called

}

I hope to implement interface Test of C#.


Viewing all articles
Browse latest Browse all 1710


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>