0

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?

2
  • Can you remove the ClassInterface(ClassInterfaceType.None) attribute from the class? Not sure it's causing problem, but its at least useless. also, can you remove the raw_interfaces_only attribute? Commented Dec 19, 2016 at 10:53
  • As I've been following a tutorial on how to do COM interop, could you explain why the ClassInterface(ClassInterfaceType.None) is useless? Commented Dec 19, 2016 at 12:33

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

I changed my interface type to InterfaceIsIUnknown. This solved my problem. Thank you.
Glad to hear. Note that if you intend to keep raw_interfaces_only, you should always check the HRESULT return value. It will tell you whether the method succeeded or not.

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.