c# - 通过 COM 将 C++ 中的复杂对象传递给 C#

标签 c# c++ interop com-interop

此问题扩展了此处的现有问题:
Passing an object from C++ to C# though COM

上一个问题涉及一个简单的对象,但我想对一个复杂的对象做同样的事情。

因此,TestEntity1 不是具有单个属性,而是如果它具有 TestEntity2 类型的另一个属性,我如何在 C++ Consumer 中分配 TestEntity1 对象的 TestEntity2 类型的属性?

C#:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ClassLibrary1
{
    [ComVisible(true)]
    public interface ITestEntity1
    {
        string Name { get; set; }
        TestEntity2 Entity2 { get; set; }
    }

    [ComVisible(true)]
    public class TestEntity1 : ITestEntity1
    {
        public string Name { get; set; }
    }

    [ComVisible(true)]
    public interface ITestEntity2
    {
        string Description { get; set; }
    }

    [ComVisible(true)]
    public class TestEntity2 : ITestEntity2
    {
        public string Description { get; set; }
    }

    [ComVisible(true)]
    public interface ITestGateway
    {
        void DoSomething(
            [MarshalAs(UnmanagedType.Interface)]object comInputValue);
    }

    [ComVisible(true)]
    public class TestGateway : ITestGateway
    {
        public void DoSomething(object comInputValue)
        {
            if (!(comInputValue is TestEntity1))
            {
                throw new ArgumentException("com input value", "comInputValue");
            }

            TestEntity1 entity = comInputValue as TestEntity1;
            //entity.Name
            //entity.Entity2
        }
    }
}

C++:

// 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);

谢谢。

最佳答案

这是我自己想出来的。 :)

我必须使用接口(interface)来定义属性,如下所示:

[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; }
}

关于c# - 通过 COM 将 C++ 中的复杂对象传递给 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6406578/

相关文章:

c++ - 使用 cout << 运算符时如何用前导零填充 float

python - C++-Python 互操作 : Marshalling data faster

javascript - Dart SDK 0.8.10.3_r29803 dart :js callbacks

c# - 如何创建单个 Web 应用程序作为许多公司的子域

c# - 与 IndexOutOfRangeException 相关的问题

具有默认参数值的c#方法不会生成没有参数的重载?

c# - 使用互操作在 excel 中设置数字格式

C#。 NPOCO。错误 : "Incorrect syntax near ' &'. " when trying to join

c++ - 使用 MVC 模式避免多人游戏和重播中的同步问题的提示

c++ - 库机器类型 'x86' 与目标机器类型 'X64' 冲突