I have a COM Dll written in unmanaged C++ that expose a com interface. The trivial sample of idl is shown below:
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(A806FAED-FCE2-4F1B-AE67-4B36D398508E),
dual,
nonextensible,
pointer_default(unique)
]
interface IObjectToPass : IDispatch{
[propget, id(1)] HRESULT Name([out, retval] BSTR* pVal);
[propput, id(1)] HRESULT Name([in] BSTR newVal);
};
[
uuid(B99537C0-BEBC-4670-A77E-06AB5350DC23),
version(1.0),
]
library {
importlib("stdole2.tlb");
[
uuid(FB7C7D08-0E38-40D2-A160-0FC8AE76C3CF)
]
coclass ObjectToPass
{
[default] interface IObjectToPass;
};
};
so IObjectToPass interface is the interface exposed. Now I've implemented a .NET library (ObjectConsumer) in C# that import via an Interop created by tlbimp this COM object so I can use the IObjectToPass interface. Here below the sample code of this library:
using Interop.ComTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;ComTestLib
using System.Threading.Tasks;
namespace ObjectConsumer
{
public class Consumer
{
public void PassObject(IObjectToPass passobj)
{
string str = passobj.Name;
}
}
}
both c++ and c# module compiles without problem
Now I've created a simple console application in c++ wich support CLR that import both ComTestLib via import and Consumer adding a reference to console application project. My goal is to get an instance of ObjectToPass inside my console application and pass to consumer calling the public method PassObject as show below
#include "stdafx.h"
#import "..\Debug\ComTest.tlb" no_namespace, raw_interfaces_only
int main()
{
CoInitialize(NULL);
IObjectToPassPtr obj;
HRESULT hr = obj.CreateInstance(__uuidof(IObjectToPass) );
ObjectConsumer::Consumer^ consumer = gcnew ObjectConsumer::Consumer();
obj->put_Name(_T("MyName"));
consumer->PassObject(obj);
CoUninitialize();
return 0;
}
This code won't compile. Compiler cannot convert argument 1 from 'IObjectToPassPtr' to 'Interop::ComTest::IObjectToPass ^'. I've made different attempts but it seems that IObjectToPass* cannot be casted to 'Interop::ComTest::IObjectToPass^. Is there a way to do it?