C#与C++联合开发(C#调用C++)

6 篇文章 0 订阅
3 篇文章 0 订阅

1.入门实例

(1)C++部分

AddOperate.h

extern "C" _declspec(dllexport) int Sum(int a, int b);

AddOperate.cpp

#include "AddOperate.h"
#include "iostream"
using namespace std;

int Sum(int a, int b)
{
	if (a - (int)a != 0 || b - (int)b != 0) {
		cout << "请输入整数" << endl;
		return -1;
	}
	return a + b;
}

(2)Csharp部分

Algrithem.cs

using System.Runtime.InteropServices;

public class Algrithem
{
    [DllImport("Calculate.dll", CallingConvention = CallingConvention.Cdecl)]
    public extern static int Sum(int a, int b);

    [DllImport("Calculate.dll",EntryPoint ="Sum", CallingConvention = CallingConvention.Cdecl)]
    public extern static int Sum2(int a, int b);
}

然后直接使用Algrithem.sum(1,20)这样就可以了

说明:

  • Sum函数未指明EntryPoint,就会以函数名去dll中找同名函数
  • Sum2函数指明了入点EntryPoint,函数名就可以随便写了

2.进阶

参考:C#调用C++数组,结构体DLL

微软官方文档:Native and .NET Interoperability,整个子目录都是讲这个的

进阶完整例子

(1)C++部分

AddOperate.h

#pragma once

// 两个求和(传入普通数据)
extern "C" _declspec(dllexport) int Sum(int a, int b);

// 累加(传入int数组)
extern  "C"  __declspec(dllexport) void Add2Source(const int N, const int n[], int& Source);

// 设置用户名(传出char数组)(这种把指针传进来的,相当于C#中参数char[] data 又加上了 ref,但是也可以当out)
extern "C" __declspec(dllexport) void SetName(char* userName, int length);

// 设置用户名(传出char数组)(这种把指针传进来的,相当于C#中参数int[] data 又加上了 ref,但是也可以当out)
extern "C" __declspec(dllexport) void SetData(int* data, int length);

// 设置数据(传出int数组)
extern "C" __declspec(dllexport) void GetData(int* data, int length);

struct cmppe_submit
{
	char user[10][200];
};

// 得到用户信息(传出结构体,数组指针字符串)
extern "C" __declspec(dllexport) void GetUser(cmppe_submit * lpSubit);

struct Vector3
{
	float X, Y, Z;
};

// 传入、传出带参数结构体
extern "C" __declspec(dllexport)  void SendStructFromCSToCPP(Vector3 vector);

AddOperate.cpp

#include "AddOperate.h"
#include "iostream"
using namespace std;

int Sum(int a, int b)
{
	if (a - (int)a != 0 || b - (int)b != 0) {
		cout << "请输入整数" << endl;
		return -1;
	}
	return a + b;
}

 void Add2Source(const int N, const int n[], int& Source)
{
	for (int i = 0; i < N; i++)
	{
		Source += n[i];
	}
}

void SetName(char * userName,int length)
{
	cout << userName << endl;
}

void SetData(int* data, int length)
{
	for (int n=0;n<length;++n)
	{
		cout << *(data+n) << endl;
	}
}

void GetData(int* data, int length)
{
	for (int n = 0; n < length; ++n)
	{
		*(data + n) = n;
	}
}

void GetUser(cmppe_submit* lpSubit)
{
	strcpy_s(lpSubit->user[0], "John Smith");
}

void SendStructFromCSToCPP(Vector3 vector)
{
	vector.X = vector.X + 10;
	cout << "got vector3 in cpp,x:";
	cout << vector.X + 10;
	cout << ",Y:";
	cout << vector.Y;
	cout << ",Z:";
	cout << vector.Z<<endl;
}

(2)C#部分

导入dll的代码,文件名Algrithem.cs

using System.Runtime.InteropServices;

public class Algrithem
{
    const string dllName = "Calculate.dll";

    [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
    public extern static int Sum(int a, int b);

    [DllImport(dllName, EntryPoint ="Sum", CallingConvention = CallingConvention.Cdecl)]
    public extern static int Sum2(int a, int b);

    [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
    public extern static void Add2Source(int N, int[] n, ref int Source);

    public struct cmppe_submit
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2000)]
        public byte[] dst_addr;
    }

    [DllImport(dllName, EntryPoint = "GetUser", CallingConvention = CallingConvention.Cdecl)]
    public extern static void GetUser(ref cmppe_submit lpSubmit);//用ref声明结构

    [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
    public extern static void SetName(char[] userName);

    [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
    public extern static void SetData(int[] userName, int length);

    [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
    public extern static void GetData(int[] data, int length);

    [DllImport(dllName, EntryPoint = "GetData", CallingConvention = CallingConvention.Cdecl)]
    public extern static void GetData2(System.IntPtr pData, int length);


    [StructLayout(LayoutKind.Sequential)]
    public struct Vector3
    {
        public float X, Y, Z;
    }

    [DllImport(dllName, EntryPoint = "SendStructFromCSToCPP",CallingConvention = CallingConvention.Cdecl)]
    public extern static void SendStructFromCSToCPP(Vector3 vector);

}

放到调用处

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int a = Algrithem.Sum(1, 2);
            int b = Algrithem.Sum2(1, 2);
            int Z = 0;
            Algrithem.Add2Source(3,new int[]{ 1,2,3,4},ref Z);

            char[] name = "Bill Gates".ToArray();
            Algrithem.SetName(name);

            Algrithem.SetData(new int[] { 4, 2, 3 },3);

            int[] Data = new int[3];
            Algrithem.GetData(Data, 3);

            // 得到用户名
            Algrithem.cmppe_submit submit;
            submit.dst_addr = new byte[2000];
            Algrithem.GetUser(ref submit);
            string str = System.Text.Encoding.Default.GetString(submit.dst_addr, 0, 25);
            Console.WriteLine(str);


            Algrithem.Vector3 vector = new Algrithem.Vector3() { X = 10, Y = 20, Z = 30 };
            Algrithem.SendStructFromCSToCPP(vector); //将vector传递给C++并在C++中输出

        }
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值