简体   繁体   English

如何传递List <int> 从C#到C ++ CLi?

[英]How to pass List<int> from C# to C++ CLi?

I am beginner in C language family... 我是C语言家族的初学者......

So, problem is - in my solution I have a repo (wrote in C#) that store List<int> , also I have an engine that (wrote in C++). 所以,问题是 - 在我的解决方案中,我有一个存储List<int>的repo(用C#编写),我也有一个引擎(用C ++编写)。 So, I need pass List from C# implementation to C++ CLI for executing... 所以,我需要将C#实现中的List传递给C ++ CLI来执行...

As far as I understood problem is C++ know how to work with std::vector and C# know how to work with List and I need somehow convert List to vector... 据我所知,问题是C ++知道如何使用std::vector和C#知道如何使用List ,我需要以某种方式将List转换为vector ...

How to do it? 怎么做?

Any assumption appropriate. 任何适当的假设。

EDIT 编辑

Sorry for misunderstanding, but my CLI works as a mapper for pure C++ implementation. 很抱歉误解,但我的CLI作为纯C ++实现的映射器。 So, as far as I understood from C# I need to pass my List to C++ CLI , C++ CLI will convert a List to vector and invoke another C++ file with pure C++ implementation. 因此,据我所知,从C#我需要将List传递给C ++ CLI,C ++ CLI将List转换为vector并使用纯C ++实现调用另一个C ++文件。

This is my solution 这是我的解决方案

h file using namespace System; h文件使用命名空间系统; using namespace System::Collections::Generic; 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 file 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;
}
}

Error 错误

在此输入图像描述

What am I doind wrong? 我做错了什么?

C++ CLI directly supports List and you don't need a conversion. C ++ CLI直接支持List,您不需要转换。 Here is a typical signature. 这是典型的签名。

private: System::Void FooMethod( System::Collections::Generic::List<Int32>^ list )

Besides, In your updated question, you have a linker error. 此外,在您更新的问题中,您有链接器错误。

Open C++ project properties, Find Linker and then Input. 打开C ++项目属性,查找链接器,然后输入。 Add the location to your (EngineLib_Cli.dll) Library there 将位置添加到(EngineLib_Cli.dll)库中

Also I found more optimized solution 我还找到了更优化的解决方案

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

By steps 按步骤

1) you need to create your vector 2) hold this memory in heap 3) just copy the data 1)你需要创建你的向量2)在堆中保存这个内存3)只需复制数据

It is going to be much more faster 它会更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM