我注意到有人问过similar question 如何在C ++中使用C#COM组件
但该线程被标记为重复,因为问题的标题过于通用,并且未指定C#。
由于我无法回答这个问题,我正在创建这个我将回答的新问题,其他人也可以提出建议
答案 0 :(得分:-1)
以下是我这方面的答案:
在.NET中引用C ++ COM组件很容易;我们所要做的就是
但反过来并不像上面那么简单。
所以这一次我提出了我们需要遵循的步骤,以便在C ++代码中引用COM可见的.NET dll。
使用如下的regasm注册COM Visible .NET assam:
regasm< dll_name_with_full_path> / TLB
例如
regasm "C:\SampleProject\bin\Release\Atreya.Anugrah.ComVisibleComponent.dll" /tlb
请参阅.h文件中的.NET组件,如下所示:
#import“< generated_tlb_name_with_full_path>” named_guids raw_interfaces_only
例如
#import "C:\SampleProject\bin\Release\Atreya.Anugrah.ComVisibleComponent.tlb" named_guids raw_interfaces_only
在.h文件中创建一个类,并像这样声明成员变量:
ComVisibleComponent :: * memberVariablePointer;例如
Atreya_Anugrah_ComVisibleComponent::IMyInterface* m_pIMyInterface;
现在,在.cpp文件中初始化声明的成员变量指针,如下所示:
hr = CoCreateInstance(
__uuidof(Atreya_Anugrah_ComVisibleComponent::<Name_of_CoClass_Implementing_Interface>), // COM class id
NULL, // Outer unknown
CLSCTX_ALL, // Server INFO
Atreya_Anugrah_ComVisibleComponent::IID_<Name_of_Interface>, // interface id
(void**) &m_pIMyInterface); // Returned Interface
if (FAILED(hr))
{
_com_error e(hr);
strErrorMsg.Format("Error: Unable to create COM Visible .NET component. hr = %d. Message = %s",hr, e.ErrorMessage());
return hr;
}
//you are good to use your initialized pointer here :)
m_pIMyInterface->foo()
P.S。这篇文章没有解释我们如何使我们的.NET组件成为COM可见。请参考其他帖子/文章,因为这是非常简单的活动。
合十! (问候)
Anugrah Atreya