【C#与C++结构体数组互相传递】

在这里插入图片描述

应该是目前网上能看到的思路最清晰的版本了!

首选是CPP端,把以下编译为dll

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>

//extern "C" __declspec(dllexport)  void __stdcall init(int &num)
//{
//	num = 233;
//} //OK

typedef struct Student
{
	char name[20];
	int age;
	double scores[32];
}Student;

typedef struct Class
{
	int number;
	Student students[126];
}Class;

extern "C" __declspec(dllexport) int ExportClass(Class** pClass, int& num)
{
	num = 50;
	*pClass = new Class[num];
	for (int i = 0; i < num; i++)
	{
		(*pClass)[i].number = i;
		for (int j = 0; j < 126; j++)
		{
			memset((*pClass)[i].students[j].name, 0, 20);
			sprintf((*pClass)[i].students[j].name, "name_%d_%d", i, j);
			(*pClass)[i].students[j].age = j % 2 == 0 ? 15 : 20;
		}
	}
	return 0;
}

extern "C" __declspec(dllexport) int ImportClass(Class* pClass, int num)
{
	for (int i = 0; i < num; i++)
	{
		printf(" c number:%d ", pClass[i].number);
		for (int j = 0; j < 126; j++)
		{
			printf(" name%s ",pClass[i].students[j].name);
			printf(" age%d ", pClass[i].students[j].age);
		}
		printf("\n");
	}
	return 0;
}

第一个函数ExportClass:用于传入一个空指针的地址,在函数内部重新对它实例化并赋值,num为最终获取的对象个数,因为c#端是不知道的!
第二个函数ImportClass:用于传入一个C#端过来的指针,长度(num)由C#端决定,同理,这个时候CPP端也不知道指针指向数值长度

C#端:

using System.Runtime.InteropServices;


[StructLayout(LayoutKind.Sequential)]
public struct Student
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
    public string name;
    public int age;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    public double[] scores;
}
[StructLayout(LayoutKind.Sequential)]
public struct Class
{
    public int number;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 126)]
    public Student[] students;

}
public class ddd
{
    [DllImport("DllFoobar.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void init(ref int num);

    [DllImport("DllFoobar.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int ExportClass(ref IntPtr pclasses ,ref int num);


    [DllImport("DllFoobar.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int ImportClass([In, Out] Class[] classes, int num);

    unsafe static  void Main()
    {
        int num = 0;
        int size = Marshal.SizeOf(typeof(Class));
        IntPtr infosIntptr = IntPtr.Zero;// Marshal.AllocHGlobal(size * 50);
        //Class[] infos = new Class[50];

        ExportClass(ref infosIntptr,ref num);
        
        Console.WriteLine("infosIntptr:"+ infosIntptr);
        //ExportClass(classes,ref num);
        for (int inkIndex = 0; inkIndex < num; inkIndex++)
        {
            IntPtr ptr = (IntPtr)(infosIntptr.ToInt64() + inkIndex * size);
            var dd = (Class)Marshal.PtrToStructure(ptr, typeof(Class));

            Console.WriteLine(dd.number+" "+dd.students[0].name);
            //break;
        }
        Console.WriteLine("num:"+ num+ " infosIntptr:" +(infosIntptr!=IntPtr.Zero));
        Marshal.FreeHGlobal(infosIntptr);

        /
        int c = 6;
        Class[] classes=new Class[c];
        for (int i = 0; i < classes.Length; i++)
        {
            classes[i].number = i;
            classes[i].students = new Student[126];
            for (int s = 0; s < 126; s++)
            {
                classes[i].students[s].name = "aa_dd_"+s;
                classes[i].students[s].age = i + 10;
            }
        }
        ImportClass(classes,c);



        Console.ReadKey();
    }
}

PS:
这里ExportClass内由于第一个参数CPP端是一个二级指针pclasses,所以对这个指针提取地址,也就是 ref IntPtr pclasses,即指向指针的指针,由于这个指针是在CPP里面实例化的,所以整活完后需要手动在C#端释放内存;

第二个函数就比较直观了,通过特殊的定义[In, Out] Class[] classes传入数值到CPP里

运行上面两个工程,最后如果是下面的界面,那么恭喜你成功了:
在这里插入图片描述

Ref
https://blog.csdn.net/fenggewan/article/details/88409551
https://www.jb51.net/article/103825.htm
https://blog.csdn.net/bruce135lee/article/details/80952032

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在C#中调用C++传递数组,可以使用平台调用(Platform Invocation Services,P/Invoke)来实现。在C#中,可以使用DllImport特性来声明C++函数,并使用IntPtr来表示指针类型。以下是一个示例代码: ```csharp // 声明C++函数 \[DllImport("YourCppLibrary.dll", CallingConvention = CallingConvention.Cdecl)\] private static extern void YourCppFunction(IntPtr array, int length); // 定义结构体 \[StructLayout(LayoutKind.Sequential)\] public struct YourStruct { // 定义结构体成员 public int intValue; public float floatValue; // ... } // 在C#中调用C++函数 YourStruct\[\] array = new YourStruct\[10\]; IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(YourStruct)) * array.Length); for (int i = 0; i < array.Length; i++) { Marshal.StructureToPtr(array\[i\], (IntPtr)((long)ptr + i * Marshal.SizeOf(typeof(YourStruct))), false); } YourCppFunction(ptr, array.Length); Marshal.FreeHGlobal(ptr); ``` 在这个示例中,我们首先使用DllImport特性声明了一个C++函数,然后定义了一个结构体来表示数组的元素类型。在调用C++函数之前,我们使用Marshal.AllocHGlobal方法为数组分配内存,并使用Marshal.StructureToPtr方法将数组中的元素复制到内存中。最后,我们调用C++函数,并在使用完毕后使用Marshal.FreeHGlobal方法释放内存。 请注意,示例中的函数和结构体名称是示意性的,你需要根据你的实际情况进行相应的修改。另外,还需要确保C++函数的参数类型和顺序与C#中的声明一致。 #### 引用[.reference_title] - *1* [C#调用C++DLL数组,结构体传递](https://blog.csdn.net/zsqysq/article/details/119646597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C# 调用 C++ dll 指针数组](https://blog.csdn.net/hondef/article/details/129027991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值