开发者

passing a complex object in c++ to c# through COM

开发者 https://www.devze.com 2023-03-14 12:25 出处:网络
This question expands on the existing question here: Passing an object from C++ to C# though COM The previous question deals with a simple object, but I would like to do the same for a complex object

This question expands on the existing question here:

Passing an object from C++ to C# though COM

The previous question deals with a simple object, but I would like to do the same for a complex object.

So instead of TestEntity1 having a single property, if it has another property of type TestEntity2, how can I assign the property of type TestEntity2 of the TestEntity1 object in c++ consumer?

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开发者_C百科; 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);

Thank you.


I figured this out myself. :)

I had to use the interface to define the property like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号