PMARSH PMARSH - 4 years ago 584
C# Question

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?

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download