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

【C#与C++结构体数组互相传递】_第1张图片

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

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

#pragma warning(disable:4996)
#include 
#include 

//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里

运行上面两个工程,最后如果是下面的界面,那么恭喜你成功了:
【C#与C++结构体数组互相传递】_第2张图片

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

你可能感兴趣的:(工具,c++,c#,visual,studio)