在c+中传递复杂对象+;通过COM发送到c#
此问题扩展了此处的现有问题:在c+中传递复杂对象+;通过COM发送到c#,c#,c++,interop,com-interop,C#,C++,Interop,Com Interop,此问题扩展了此处的现有问题: 上一个问题涉及一个简单的对象,但我想对一个复杂的对象也这样做 P>而不是具有一个属性的TestNeTi1,如果它具有TestNeTy2的另一个属性,那么如何分配C++用户Eng/P>TestNeTi1对象的TestEntIy2类型的属性? C#: C++: 谢谢。这是我自己想出来的。:) 我必须使用接口定义属性,如下所示: [ComVisible(true)] public interface ITestEntity1 { string Name { g
上一个问题涉及一个简单的对象,但我想对一个复杂的对象也这样做 <> P>而不是具有一个属性的TestNeTi1,如果它具有TestNeTy2的另一个属性,那么如何分配C++用户Eng/P>TestNeTi1对象的TestEntIy2类型的属性? C#: C++:
谢谢。这是我自己想出来的。:) 我必须使用接口定义属性,如下所示:
[ComVisible(true)]
public interface ITestEntity1
{
string Name { get; set; }
ITestEntity2 Entity2 { get; set; }
}
[ComVisible(true)]
public class TestEntity1 : ITestEntity1
{
public string Name { get; set; }
public ITestEntity2 Entity2 { get; set; }
}
是否有理由使用COM互操作,而不是直接使用C++/CLI创建.Net包装器?我发现这种方法简单得多,但您可能有不同的需要,需要COM。@jwismar,不幸的是,我将使用遗留代码来调用这个新的COM对象,而且我目前没有使用托管c++/cli重新编译\port它的选项。如果在com服务器中包含typelib,则可以自动导入接口
// ComClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#import "..\Debug\ClassLibrary1.tlb" raw_interfaces_only
int _tmain(int argc, _TCHAR* argv[])
{
ITestGatewayPtr spTestGateway;
spTestGateway.CreateInstance(__uuidof(TestGateway));
ITestEntity1Ptr spTestEntity1;
spTestEntity1.CreateInstance(__uuidof(TestEntity1));
_bstr_t name(L"name");
spTestEntity1->put_Name(name);
ITestEntity2Ptr spTestEntity2;
spTestEntity2.CreateInstance(__uuidof(TestEntity2));
//spTestEntity1->putref_Entity2(spTestEntity2); //error C2664: 'ClassLibrary::ITestEntity1::putref_Entity2' : cannot convert parameter 1 from 'ClassLibrary::ITestEntity2Ptr' to 'ClassLibrary::_TestEntity2 *'
spTestGateway->DoSomething(spTestEntity1);
[ComVisible(true)]
public interface ITestEntity1
{
string Name { get; set; }
ITestEntity2 Entity2 { get; set; }
}
[ComVisible(true)]
public class TestEntity1 : ITestEntity1
{
public string Name { get; set; }
public ITestEntity2 Entity2 { get; set; }
}