如何通过COM互操作将一个字符串集合从C#返回到C ++ [英] How to return a collection of strings from C# to C++ via COM interop

查看:254
本文介绍了如何通过COM互操作将一个字符串集合从C#返回到C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中为某些Display方法创建了一个componet,它返回一个String List

I hav created a com componet for some Display method in C# it returns a String List

,如下所示。在v ++中我hav使用std :: lst从Disp()捕获返回值,但它

as shown below. In v++ i hav used std::lst to catch the return value from Disp() but it

给出编译器错误Disp不是类的成员。我使返回类型的无效然后

gives compiler error that Disp is not a member of class. I i make return type a s void then

它工作正常。我可以修改,以便Disp返回一个列表和在主(c + +)我必须使用

it works fine. what i can modify so that Disp return a List and in main(c++) i have to use

这个返回值。

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并进行测试。 tlb。
现在用c ++编写的main函数

compiled and created Test.dll successfully and also test.tlb. Now in main function that is 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's wrong in my code when i try to compile this it shows


'Disp':不是TestLib的成员:ITest

'Disp':is not a member of TestLib:ITest

如何解决这个plz帮助我....当我使它返回类型为void在类中它工作正常。WHAT错误我做了???

how to solve this plz help me ....when i make it return type as void in Class it works fine .what mistake i did????

推荐答案

这只是不工作,即使你修复错别字。 COM互操作没有从 List< T> 到COM中的某个标准映射,它肯定不会映射到 std :: list 。泛型不允许出现在COM界面中。

This just isn't going to work, even if you fix the typos. COM interop doesn't have a standard mapping from List<T> to something in COM, and it certainly won't map it to std::list. Generics aren't allowed to appear in COM interfaces.

UPDATE

尝试使用 ArrayList 作为返回类型,因为这是非通用的我认为也许 tlb 将包括类型信息。这没有工作,所以我尝试 IList 。这也没有工作( #import 语句产生一个 .tlh 文件引用 IList ,但没有定义它。)

I tried using ArrayList as the return type, as that's non-generic I thought maybe the tlb would include type information for it. That didn't work so I tried IList. That didn't work either (the #import statement produced a .tlh file that referred to IList but had not definition for it.)

因此,作为解决方法,我试图声明一个简单的列表接口。代码最终如下:

So as a workaround I tried declaring a simple list interface. The code ends up like this:

[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 属性,因为它继承了一个完美的从< c $ c> List< string> 。

So I have my own (very simplistic) string collection interface. Note that my StringCollection class doesn't have to define the Count property because it inherits a perfectly good on from List<string>.

然后我在C +端:

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



我必须手动复制字符串集合的内容一个合适的C ++标准容器,但它工作。

I have to manually copy the contents of the string collection a proper C++ standard container, but it works.

可能有一种方法从标准集合类中获取正确的类型信息,所以你不必制作自己的收集界面,但

There may be a way to get proper type info from the standard collection classes, so you don't have to make your own collection interfaces, but if not, this should serve okay.

或者,你看过C ++ / CLI吗?这将工作非常无缝,虽然它仍然不会将CLR集合转换为std容器自动。

Alternatively, have you looked at C++/CLI? That would work pretty seamlessly, although it still wouldn't convert CLR collections to std containers automatically.

这篇关于如何通过COM互操作将一个字符串集合从C#返回到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆