如何调用c++函数从c#中获取Vector调用
How to call C++ function takes Vector call from C#
我正在用c++工作,并创建分析数据的库。我创建了几个类,这些类的函数采用c++向量。现在我想在c#中创建UI并调用这些类。我正在考虑创建API从c#调用。
既然数据是数组/向量,那么我怎么能从c#调用它?
我本想发表评论,但我的知名度不够高。在c++类库中使用STL类(如vector或string)会有一些复杂性。您可以在这里查看更多信息和可能的解决方案:我可以为Dll传递std::string,我可以用Dll ´s做些什么?
您需要创建自己的c++/CLI互操作来实现这一点。
强烈推荐一个不错的书,"专家c++/CLI"马库斯Heege,非常值得一读。
下面是我简短的例子:
// Program.cs
static void Main(string[] args)
{
List<string> someStringList = new List<string>();
someStringList.Add("Betty");
someStringList.Add("Davis");
someStringList.Add("Eyes");
NativeClassInterop nativeClass = new NativeClassInterop();
string testString = nativeClass.StringCat(someStringList);
}
// NativeClass.h, skipping this, it's obvious anyways
// NativeClass.cpp, normal C++ class, this was in some DLL project, don't need exports
#include "stdafx.h"
#include "NativeClass.h"
std::string NativeClass::StringCat(std::vector<std::string> stringList)
{
std::string result = "";
for(unsigned int i = 0; i < stringList.size(); i++)
{
if(i != 0)
{
result += " ";
}
result += stringList[i];
}
return result;
}
// NativeClassInterop.cpp, in same DLL project, but compile this file with /clr switch
#include <gcroot.h>
#using <System.dll>
#include <vector>
#include <string>
#include "NativeClass.h"
// Helper method
static std::string nativeStringFromManaged(System::String^ str)
{
System::IntPtr hGlobal =
System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
std::string nativeString((hGlobal.ToPointer() == 0)
? "" : (char*)hGlobal.ToPointer());
System::Runtime::InteropServices::Marshal::FreeHGlobal(hGlobal);
return nativeString;
}
// C++/CLI wrapper class
public ref class NativeClassInterop
{
public:
System::String^ StringCat(System::Collections::Generic::List<System::String^>^ someStringList)
{
// You get to do the marshalling for the inputs
std::vector<std::string> stringList;
for(int i = 0; i < someStringList->Count; i++)
{
stringList.push_back(nativeStringFromManaged(someStringList[i]));
}
NativeClass nativeClass;
std::string nativeString = nativeClass.StringCat(stringList);
// And for the outputs ;-)
System::String^ managedString = gcnew System::String(nativeString.c_str());
return managedString;
}
};
相关文章:
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 如果 std::vector::clear() 不是静态的,如何在没有实例的情况下调用它?
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 在 C++20 之前,在带有常量或引用字段的"A"上调用 'std::vector<A>
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 错误:调用"es_queue::set_rpc_vector(std::vector >&, std::__cxx11::string)"没有匹配函数
- 当我从 std::vector 中的新放置调用析构函数时会发生什么?
- 对于优化级别为 0 的 std::vector,析构函数被调用两次
- C++如果调用 vector,矢量分割错误会导致崩溃
- 调用 vector.erase() 函数时指针操作无效错误
- 我是否应始终在功能末尾调用vector clear()
- 在调用vector::assign()之前调用vector::reserve()更好吗?
- 在向量引用上调用 vector::reserve()
- 对存储在boost::any中的向量调用vector::size()
- 从Cocos2d::Layer派生的类有一个指针数据成员.这个元素也有一个向量容器.调用vector::push_bac
- 错误C2280:试图引用已删除的函数(试图调用vector.erase)
- 在C++中,为什么我可以在<double>类中调用 vector::begin() 来获取私有内容?
- 调用vector.erase()时出现分段错误
- 调用vector中对象的成员函数