How to return a rowset from C # to C ++ through COM interaction

I created a com component for some mapping method in C # that returns a list of strings

as shown below. In v ++, I used std :: lst to get the return value from Disp (), but it

gives a compiler error that Disp is not a member of the class. I do return type as void, then

It works great. what can I change so that Disp returns a List and basically (C ++) I have to use

This is the return value.

Public interface ITest
{
    List<string> Disp();
}

class TestLib:ITest
{
    List<string> Disp()
    {
        List<string> li=new List<string>();
        li.Add("stack");
        li.Add("over");
        li.Add("Flow");

        return li;
    }
}

Test.dll was compiled and created successfully, as well as test.tlb. Now in the main function written in C ++

#include<list>
#import "..\test.tlb"
using namespace Test;
void main()
{
    HRESULT hr=CoInitialize(null);

    ITestPtr Ip(__uuidof(TestLib));

    std::list<string> li=new std::list<string>();

    li=Ip->Disp();
}

What is wrong in my code, when I try to compile this, it shows

'Disp': not a member of TestLib: ITest

, .... , . ????

+3
3

, . COM- List<T> - COM, , , std::list. COM .

UPDATE

ArrayList , - , , tlb . , IList. ( #import .tlh, IList, .)

, . :

[Guid("7366fe1c-d84f-4241-b27d-8b1b6072af92")]
public interface IStringCollection
{
    int Count { get; }
    string Get(int index);
}

[Guid("8e8df55f-a90c-4a07-bee5-575104105e1d")]
public interface IMyThing
{
    IStringCollection GetListOfStrings();
}

public class StringCollection : List<string>, IStringCollection
{
    public string Get(int index)
    {
        return this[index];
    }
}

public class Class1 : IMyThing
{
    public IStringCollection GetListOfStrings()
    {
        return new StringCollection { "Hello", "World" };
    }
}

, ( ) . , StringCollection Count, List<string>.

++:

#include "stdafx.h"
#import "..\ClassLibrary5.tlb"

#include <vector>
#include <string>

using namespace ClassLibrary5;

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(0);

    IMyThingPtr thing(__uuidof(Class1));

    std::vector<std::string> vectorOfStrings;

    IStringCollectionPtr strings(thing->GetListOfStrings());
    for (int n = 0; n < strings->GetCount(); n++)
    {
        const char *pStr = strings->Get(n);
        vectorOfStrings.push_back(pStr);
    }

    return 0;
}

++, .

, , , .

, ++/CLI? , CLR std-.

+7

, . # TestLib, TestCls. , , ( , , Disp, ).

+1

Guess: Disp () has not been declared public

-2
source

Source: https://habr.com/ru/post/1711070/


All Articles