当前位置: 首页 > news >正文

C#与C++交互开发系列(十四):C++中STL容器与C#集合传递的形式

在这里插入图片描述

前言

在跨语言开发中,C++ 的 STL 容器(如 std::vector, std::map)和 C# 的集合类(如 List<T>, Dictionary<TKey, TValue>)之间的数据传递是一个常见需求。由于两者的内存布局和实现机制不同,直接传递并不现实,需要对容器进行转换、序列化或采用适当的桥接方式。本文将详细介绍如何在 C++ 和 C# 之间进行集合数据的传递,涵盖几种常见容器的处理方法,并提供完整的代码示例。

1. 使用 std::vectorList<T> 传递数组数据

std::vectorList<T> 都是动态数组,在内存上通常是连续存储的,这让我们可以更方便地在 C++ 与 C# 之间传递这些数据。

C++ 代码示例

我们可以通过 std::vector 存储整数数组,然后通过指针传递给 C#:

// CppLibrary.cpp
#include <vector>extern "C" __declspec(dllexport)
int* GetIntArray(int* size) {static std::vector<int> data = {1, 2, 3, 4, 5}; // 静态变量保证数据不会在函数返回后释放*size = data.size();return data.data();  // 返回指向数据起始位置的指针
}

C# 代码示例

在 C# 中,可以通过 DllImport 来调用此 C++ 函数,并将返回的指针数据复制到 List<int> 中:

using System;
using System.Runtime.InteropServices;class Program
{[DllImport("MyNativeLib.dll", CallingConvention = CallingConvention.Cdecl)]private static extern IntPtr GetIntArray(out int size);static void Main(){int size;IntPtr ptr = GetIntArray(out size);List<int> result = new List<int>();for (int i = 0; i < size; i++){int value = Marshal.ReadInt32(ptr, i * sizeof(int));result.Add(value);}Console.WriteLine("Received data:");result.ForEach(Console.WriteLine);}
}

执行结果

Received data:
1
2
3
4
5

2. 使用 std::mapDictionary<TKey, TValue> 传递键值对数据

std::map 是一种键值对容器,与 C# 的 Dictionary<TKey, TValue> 类似。在将数据传递给 C# 时,可以使用中间数组格式或序列化的字符串传递方式。

C++ 代码示例

假设我们有一个 C++ std::map,需要将其内容传递给 C#,可以将键值对放入平铺的数组中(键和值依次排列):

// CppLibrary.cpp
#include <map>
#include <vector>extern "C" __declspec(dllexport)
int* GetMapData(int* size) {static std::map<int, int> data = {{1, 100}, {2, 200}, {3, 300}};static std::vector<int> flatData;flatData.clear();for (const auto& kv : data) {flatData.push_back(kv.first);flatData.push_back(kv.second);}*size = flatData.size();return flatData.data();
}

C# 代码示例

在 C# 中,可以使用数组接收数据,并将其组织为 Dictionary<int, int>

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;class Program
{[DllImport("MyNativeLib.dll", CallingConvention = CallingConvention.Cdecl)]private static extern IntPtr GetMapData(out int size);static void Main(){int size;IntPtr ptr = GetMapData(out size);Dictionary<int, int> result = new Dictionary<int, int>();for (int i = 0; i < size; i += 2){int key = Marshal.ReadInt32(ptr, i * sizeof(int));int value = Marshal.ReadInt32(ptr, (i + 1) * sizeof(int));result[key] = value;}Console.WriteLine("Received dictionary:");foreach (var kv in result){Console.WriteLine($"Key: {kv.Key}, Value: {kv.Value}");}}
}

执行结果

Received dictionary:
Key: 1, Value: 100
Key: 2, Value: 200
Key: 3, Value: 300

3. 复杂容器与序列化传递

对于更复杂的 STL 容器(如嵌套的 std::vector<std::vector<int>>)或大规模的 std::unordered_map,可以考虑使用序列化来传递数据。常见方法包括 JSON 序列化或 Protobuf 序列化:

C++ 使用 JSON 序列化传递数据

通过vcpkg 安装 nlohmann/json 库,
在这里插入图片描述
执行指令

vcpkg install nlohmann-json

在这里插入图片描述
MyNativeLib项目–>C/C+±->常规–>附加包含目录,添加vcpkage的include目录。
在这里插入图片描述

// 使用 nlohmann/json 库进行序列化
#include <nlohmann/json.hpp>
#include <string>
#include <map>extern "C" __declspec(dllexport)
const char* GetSerializedMap() {static std::map<int, std::string> data = {{1, "one"}, {2, "two"}};nlohmann::json j = data;static std::string serialized = j.dump();return serialized.c_str();
}

C# 使用 JSON 反序列化接收数据

项目引用Newtonsoft.Json组件

 <ItemGroup><PackageReference Include="Newtonsoft.Json" Version="13.0.3" /></ItemGroup>
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Newtonsoft.Json;class Program
{[DllImport("MyNativeLib.dll", CallingConvention = CallingConvention.Cdecl)]private static extern IntPtr GetSerializedMap();static void Main(){IntPtr ptr = GetSerializedMap();string jsonString = Marshal.PtrToStringAnsi(ptr);var dictionary = JsonConvert.DeserializeObject<List<List<object>>>(jsonString).ToDictionary(pair => Convert.ToInt32(pair[0]), pair => pair[1].ToString());Console.WriteLine("Deserialized dictionary:");foreach (var kv in dictionary){Console.WriteLine($"Key: {kv.Key}, Value: {kv.Value}");}}
}

执行结果

Deserialized dictionary:
Key: 1, Value: one
Key: 2, Value: two

结论

在 C++ 和 C# 之间传递 STL 容器和集合数据时,需要考虑到两者在内存管理和容器实现方面的差异。本文介绍了几种常用容器的传递方法:

  1. 使用指针传递简单数组,如 std::vectorList<T>
  2. 将键值对平铺为数组形式传递,实现 std::mapDictionary<TKey, TValue> 的转换。
  3. 对于复杂数据,使用 JSON 等序列化方式,可以方便地在 C++ 和 C# 之间传递复杂的数据结构。

在实际应用中,可以根据数据的复杂程度和性能要求选择合适的方法,以确保跨语言的高效数据传递。


http://www.mrgr.cn/news/60125.html

相关文章:

  • 如何在Node.js中执行解压缩文件操作
  • 【STM32+HAL】OV2640捕获图像显示
  • codimd更改登录超时时限
  • nuxt数据库之增删改查,父组件子组件传值
  • 浪潮云启操作系统(InLinux)bcache缓存实践:理解OpenStack环境下虚拟机卷、Ceph OSD、bcache设备之间的映射关系
  • pdf免费压缩软件 pdf文件压缩免费软件 软件工具方法
  • python函数-18
  • 在linux系统中使用zlib库 压缩解压 文件(C++)
  • redis缓存击穿如何解决和预防?
  • H3C Hybrid 实验
  • 深入浅出 C++ STL:解锁高效编程的秘密武器
  • C/C++小宇宙代码
  • 道路车辆功能安全 ISO 26262标准(9-4)—面向汽车安全完整性等级 (ASIL) 和安全的分析
  • 清华面试文稿
  • 平衡控制——直立环——速度环
  • 基于Datawhale开源量化投资学习指南(11):LightGBM在量化选股中的优化与实战
  • android studio编译错误提示无法下载仓库
  • stm32单片机基于rt-thread 的 littlefs 文件系统 的使用
  • 梦笔记20241028
  • AngularJS 指令
  • python处理文件和图片
  • Golang | Leetcode Golang题解之第516题最长回文子序列
  • Flux 开源替代,他来了——Liberflux
  • spring-第十二章 GoF代理模式
  • Bootstrap 5 弹出框
  • MSR寄存器独有的还是共享的