C#调用C++ dll 返回数组

先看一下C语言函数返回数组的问题;

先看一个错误的示范;

    因为 a 是局部变量,只存在函数 function() 中,返回给main中的b是错误的;

函数返回数组的一种写法如下;

#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]);
} 

 

然后看一下C#调用C++ dll返回数组的问题;

先参阅此文,

在一个解决方案中用C#测试调用C++ DLL_vs如何调用同一个解决方案的c#和c++库_bcbobo21cn的博客-CSDN博客 

全部代码如下;这是返回数组的一种情形;

dll的cpp代码,

#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;
}

dll的h文件,

#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# 测试程序;

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)
        {

        }
    }
}

 

 这是返回数组的一种情形,还有其他情况,有时间再继续;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在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 ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值