[英]How to pass List<int> from C# to C++ CLi?
我是C語言家族的初學者......
所以,問題是 - 在我的解決方案中,我有一個存儲List<int>
的repo(用C#編寫),我也有一個引擎(用C ++編寫)。 所以,我需要將C#實現中的List傳遞給C ++ CLI來執行...
據我所知,問題是C ++知道如何使用std::vector
和C#知道如何使用List
,我需要以某種方式將List轉換為vector ...
怎么做?
任何適當的假設。
編輯
很抱歉誤解,但我的CLI作為純C ++實現的映射器。 因此,據我所知,從C#我需要將List
傳遞給C ++ CLI,C ++ CLI將List
轉換為vector
並使用純C ++實現調用另一個C ++文件。
這是我的解決方案
h文件使用命名空間系統; using namespace System :: Collections :: Generic;
//forward declaration
class MathCore;
namespace MathCore_CLI_namespace
{
public ref class MathCore_CLI
{
public:
MathCore_CLI();
~MathCore_CLI();
int computeMulPlusVals(List<int>^ list_first, List<int>^ list_second);
//int computeMulPlusVals(std::vector<int> vect_first, std::vector<int> vect_second);
private:
MathCore * m_pMathCore;
};
}
cpp文件
#include "stdafx.h"
#include "MathCore_CLI.h"
#include "..\Engine\MathCore.h"
#include <iostream>
#include <array>
using namespace System;
using namespace System::Collections::Generic;
namespace MathCore_CLI_namespace
{
const int size = 5;
int count = 0;
int arrayVal[size];
MathCore_CLI::MathCore_CLI()
{
m_pMathCore = new MathCore();
}
MathCore_CLI::~MathCore_CLI()
{
delete m_pMathCore;
}
int computeMulPlusVals(List<int>^ list_first, List<int>^ list_second)
{
return 0;
}
}
錯誤
我做錯了什么?
C ++ CLI直接支持List,您不需要轉換。 這是典型的簽名。
private: System::Void FooMethod( System::Collections::Generic::List<Int32>^ list )
此外,在您更新的問題中,您有鏈接器錯誤。
打開C ++項目屬性,查找鏈接器,然后輸入。 將位置添加到(EngineLib_Cli.dll)庫中
我還找到了更優化的解決方案
int MathCore_CLI::computeMulPlusVals(array<int>^ arr_first, array<int>^ arr_second)
{
auto vec_first = std::vector<int>(arr_first->Length);
cli::pin_ptr<int> pPinnedFirst = &arr_first[0];
memcpy(vec_first.data(), pPinnedFirst, arr_first->Length * sizeof(int));
auto vec_second = std::vector<int>(arr_second->Length);
cli::pin_ptr<int> pPinnedSecond = &arr_second[0];
memcpy(vec_second.data(), pPinnedSecond, arr_second->Length * sizeof(int));
return m_pMathCore->computeMulPlusVals(vec_first, vec_second);
}
按步驟
1)你需要創建你的向量2)在堆中保存這個內存3)只需復制數據
它會更快
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.