C# calls C++ dll to return array

Let’s first look at the problem of C language functions returning arrays;

Let’s look at a wrong demonstration first;

    Because a is a local variable and only exists in the function function(), it is wrong to return it to b in main;

One way to write the function return array is as follows;

#include<stdio.h>
int function(int * p){
	int a[5];
	a[0] = 1;
	a[1] = 2;
	a[2] = 3;
	a[3] = 4;
	a[4] = 5;

	p[0]=a[0];
	p[1]=a[1];
	p[2]=a[2];
	p[3]=a[3];
	p[4]=a[4];

	return 0;
}
int main(){
	int* b;
	function(b);
//	printf("123\n");
	printf("第一次%d%d%d%d%d\n",b[0],b[1],b[2],b[3],b[4]);
	printf("第二次%d%d%d%d%d\n",b[0],b[1],b[2],b[3],b[4]);
} 

 

Then take a look at the problem of C# calling C++ dll to return an array;

Please refer to this article first,

Test calling C++ DLL with C# in a solution_vs. How to call c# and c++ libraries of the same solution_bcbobo21cn's blog-CSDN blog 

The entire code is as follows; this is a case of returning an array;

cpp code of dll,

#include "pch.h"
#include "dlltest.h"



DllTest_API int __stdcall Add(int a, int b)
{
	return a + b;
}

DllTest_API int __stdcall getages(int *pAge, int ng)
{
	int n[10];
	n[0]=23;n[1]=21;n[2]=21;n[3]=21;n[4]=21;
	n[5]=21;n[6]=37;n[7]=21;n[8]=21;n[9]=25;

	pAge[0] = n[0];
	pAge[1] = n[1];
	pAge[2] = n[2];
	pAge[3] = n[3];
	pAge[4] = n[4];
	pAge[5] = n[5];
	pAge[6] = n[6];
	pAge[7] = n[7];
	pAge[8] = n[8];
	pAge[9] = n[9];

	ng = 10;

	return ng;
}

h file of dll,

#pragma once

#define DllTest_API __declspec(dllexport)
EXTERN_C DllTest_API int __stdcall Add(int a, int b);
EXTERN_C DllTest_API int __stdcall getages(int *pAge, int ng);

C# test program;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace mytest
{
    public partial class Form1 : Form
    {
        [DllImport(@"D:\vcprj\dlltest\Debug\dlltest\testdll.dll")]
        extern static int Add(int a, int b);

        [DllImport(@"D:\vcprj\dlltest\Debug\dlltest\testdll.dll")]
        extern static int getages(IntPtr pAge, int ng);

        int[] ages = new int[10];

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int c = Add(200, 67);
            textBox1.Text = textBox1.Text + c.ToString() + Environment.NewLine;
            textBox1.Text += Environment.NewLine;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int pn=0;
            IntPtr p1 = Marshal.AllocHGlobal(10 * sizeof(int));
            getages(p1, pn);
            Marshal.Copy(p1, ages, 0, 10);

            for (int i = 0; i < 10; i++)
            {
                textBox1.Text = textBox1.Text + ages[i].ToString() + Environment.NewLine;
            }
            textBox1.Text += Environment.NewLine;
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
    }
}

 

 This is one case of returning an array, there are other cases, please continue when you have time;

Guess you like

Origin blog.csdn.net/bcbobo21cn/article/details/132938755