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

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

问题描述

我甲肝创建一个COM为及部件在C#中的一些显示方法返回一个字符串列表

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

如下所示。以V ++我甲肝使用的std :: LST可以捕捉到显⽰返回值(),但它

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

使编译器错误显⽰不是类的成员。我让我返回值类型A S无效然后

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

它工作正常。我可以修改,以便显⽰返回主(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的成功,也test.tlb。
 现在的主要功能是用C ++编写

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();
}

什么是错误的,我code当我尝试编译该它显示

What's wrong in my code when i try to compile this it shows

显⽰:是不是TESTLIB的成员:ITEST

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

如何解决这个plz帮助我....当我让它返回类型的类空它工作正常。什么错误,我没有????

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互操作并没有从列表℃的标准映射; T&GT; 在COM东西,它肯定不会将它映射到的std ::列表。泛型是不允许出现在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.

更新

我试着用的ArrayList 作为返回类型,因为这是不通用的我想也许在 TLB 将包括键入它的信息。没有工作,所以我试图的IList 。没有工作或者(在#进口语句产生的简称 .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.)

因此​​,作为一种变通方法我试过宣布一个简单的列表界面。在code结束这样的:

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 类没有定义计数属性,因为它继承了从<$完全好C $ C>列表与LT;字符串方式&gt;

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 ++端:

Then I have this on the C++ side:

#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?将工作pretty无缝连接,虽然它仍然不会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天全站免登陆