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

  • Question

  • 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();
        var 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 Test of C#.


    • Edited by FyterLuo Tuesday, December 13, 2016 3:17 AM
    Monday, December 12, 2016 6:29 AM

Answers

  • [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]

    interface ITest{...}

    is OK, c++::foo::Invoke(IDispatch) be called.

    • Marked as answer by FyterLuo Tuesday, December 13, 2016 7:06 AM
    Tuesday, December 13, 2016 4:27 AM

All replies

  • Hi FyterLuo,

    >>I hope to implement interface Test of C#.

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

    Based on your scenario, I want to know what your error message is. And how do yo define "obj" in first snipped code ? Could you also post here? It could be better to help us understand your issue.

    Best regards,

    Kristin

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 13, 2016 2:46 AM
  • Hi FyterLuo,

    >>I hope to implement interface Test of C#.

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

    Based on your scenario, I want to know what your error message is. And how do yo define "obj" in first snipped code ? Could you also post here? It could be better to help us understand your issue.

    Best regards,

    Kristin

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    obj is interface of ITest
    Tuesday, December 13, 2016 3:18 AM
  • Hi FyterLuo,

    >>I hope to implement interface Test of C#.

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

    Based on your scenario, I want to know what your error message is. And how do yo define "obj" in first snipped code ? Could you also post here? It could be better to help us understand your issue.

    Best regards,

    Kristin

     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    0xC0000005: Access violation
    Tuesday, December 13, 2016 4:21 AM
  • [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]

    interface ITest{...}

    is OK, c++::foo::Invoke(IDispatch) be called.

    • Marked as answer by FyterLuo Tuesday, December 13, 2016 7:06 AM
    Tuesday, December 13, 2016 4:27 AM
  • @FyterLuo

    Thanks for sharing your solution here, It could be better to help someone who has the same issue.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 14, 2016 1:35 AM