0

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

I can't use ATL.

I embed the. Net core to my executable program.

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 ITest
{
    int func();
}
main(){
    var v = GetTestObject();
    obj = (ITest)Marshal.GetObjectForIUnknown(v);
    obj.func();// exception
    dynamic dobj = Marshal.GetObjectForIUnknown(v);
    dobj.func();// OK, IDispatch::Invoke be called
}

I hope to implement interface ITest of C#.

1
  • add [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] to ITest, is ok, IDispatch::Invoke be called. Commented Dec 13, 2016 at 4:45

2 Answers 2

2

What you need is called a Runtime callable wrapper.

Here is a How to article on how to create one.

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

Comments

0

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]

interface Test

{

}

Comments

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.