小编Cou*_*ken的帖子

如何将对象列表从C++传递给C#?

我的第一个问题:)

我正在使用C++(一个游戏的地图编辑器)编写的应用程序,它具有用C#编写的前端UI.因为我是C#的新手,所以我想尽可能地在C++方面做.

从C#开始,我想调用一个C++函数,该函数将返回一个带有简单变量类型(int和string)的结构列表,这样我就可以在UI中填充一个带有它们的listBox.这可能吗?我应该如何在C#中编写dll导入函数?

我试着在这里搜索答案,但我只找到了如何将列表从C#传递给C++的帖子.

C++代码:

struct PropData
{
PropData( const std::string aName, const int aId )
{
    myName = aName;
    myID = aId;
}

std::string myName;
int myID;
};

extern "C" _declspec(dllexport) std::vector<PropData> _stdcall GetPropData()
{
std::vector<PropData> myProps;

myProps.push_back( PropData("Bush", 0) );
myProps.push_back( PropData("Tree", 1) );
myProps.push_back( PropData("Rock", 2) );
myProps.push_back( PropData("Shroom", 3) );

return myProps;
}
Run Code Online (Sandbox Code Playgroud)

C#导入功能:

    [DllImport("MapEditor.dll")]
    static extern ??? GetPropData();
Run Code Online (Sandbox Code Playgroud)

编辑:

在Ed S.的帖子之后,我将c ++代码更改为struct PropData {PropData(const std :: string aName,const int aId){myName = aName; myID = aId; …

c# c++ dll cross-language dllimport

7
推荐指数
1
解决办法
3383
查看次数

标签 统计

c# ×1

c++ ×1

cross-language ×1

dll ×1

dllimport ×1