简体   繁体   中英

C# COM INTEROP in c++

I'm trying to call my C# dll from a C++ client, so far I have the dll all setup and in my registry (I can create and call it from the power shell for example).

The problem I'm having is that I can't call it from my C++ code.

My C# interface:

namespace MyInterop
{
    [Guid("BE507380-1997-4BC0-AF01-EE5D3D537E6B"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IMyDotNetInterface
    {
        void ShowCOMDialog();
    }
}

My C# class that implements the interface:

namespace MyInterop
{

    [Guid("38939B1E-461C-4825-80BB-725DC7A88836"), ClassInterface(ClassInterfaceType.None)]
    public class MyDotNetClass : IMyDotNetInterface
    {
        public MyDotNetClass()
        { }

        public void ShowCOMDialog()
        {
            MessageBox.Show("I am a" + 
                  "  Managed DotNET C# COM Object Dialog");
        }
    }
}

Very simple as I'm just testing at the moment. I now import the tlb into my C++ file

#import "<Path to file>\MyInterop.tlb" raw_interfaces_only

Finally I try to call it:

HRESULT hr = CoInitialize(NULL);

MyInterop::IMyDotNetInterfacePtr MyDotNetClass(__uuidof(MyInterop::MyDotNetClass));

MyDotNetClass->ShowCOMDialog();

CoUninitialize();

But, VS is telling me that ShowCOMDialog is not a member of my interface. Have I missed something?

By declaring raw_interfaces_only , you have surpressed the generation of wrapper functions, as indicated in this link. And since your interface is based on IDispatch , you are forced to call your interface methods indirectly via IDispatch's Invoke .

Suggestions:

  1. Change your interface type to ComInterfaceType.InterfaceIsDual or ComInterfaceType.InterfaceIsIUnknown
  2. Remove raw_interfaces_only in order to work with the generated wrapper functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM