Hi, @Aspire
You could refer to example code link in the similar thread's question: How does implementing multiple COM interfaces work in C++?
Below is a quick test
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#import "......\bin\Debug\MyInterop.tlb" named_guids raw_interfaces_only
#include <iostream>
#include<combaseapi.h>
#include"./Debug/MyInterop.tlh"
struct MyClass:public MyInterop::IMyDotNetInterface
{
public:
virtual ULONG __stdcall AddRef(void) override { return S_OK; }
virtual ULONG __stdcall Release(void) override { return S_OK; }
MyClass()
{
}
virtual HRESULT __stdcall QueryInterface(REFIID riid, void** ppvObject) override
{
if (riid == __uuidof(IUnknown))
{
*ppvObject = (IUnknown*)this;
return S_OK;
}
IID uid;
IIDFromString(L"{C# interface GUID/IID}", &uid);
if (riid == uid)
{
*ppvObject = (IDispatch*)this;// (IUnknown*)this;
return S_OK;
}
if (riid == __uuidof(IDispatch))
{
*ppvObject = (IDispatch*)this;
return S_OK;
}
return E_NOTIMPL;
}
virtual HRESULT __stdcall GetTypeInfoCount(UINT* pctinfo) override { return S_OK; }
virtual HRESULT __stdcall GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo) override { return S_OK; }
virtual HRESULT __stdcall GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId) override
{
*rgDispId = 1;
return S_OK;
}
virtual HRESULT __stdcall Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr) override
{
return S_OK;
}
virtual HRESULT __stdcall ShowCOMDialog()
{
std::cout << "hello";
return S_OK;
}
};
void DotNetCom()
{
CoInitialize(NULL); //Initialize all COM Components
MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;
// CreateInstance parameters
// e.g. CreateInstance (<namespace::CLSID_<ClassName>)
HRESULT hRes =
pDotNetCOMPtr.CreateInstance(MyInterop::CLSID_MyDotNetClass);
if (hRes == S_OK)
{
BSTR str;
pDotNetCOMPtr->ShowCOMDialog();
//call .NET COM exported function ShowDialog ()
}
CoUninitialize(); //DeInitialize all COM Components
}
int main()
{
MyClass test;
test.ShowCOMDialog();
}
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.