CPlusPlus - #027 跨语言桥接技术:在 C# 中调用 C++ 函数

跨语言桥接技术:在 C# 中调用 C++ 函数

1 前言

在现代软件开发中,C# 和 C++ 是两种广泛使用的编程语言。C# 提供了强大的开发工具和简洁的语法,而 C++ 则以其高效的性能和系统级编程能力著称。将这两种语言结合使用,可以充分利用它们各自的优势,为开发者提供更多的灵活性和功能扩展的可能性。

在这篇文章中,我们将探讨如何在 C# 中调用 C++ 编写的函数。具体来说,我们将介绍如何通过动态链接库(DLL)将 C++ 代码导入到 C# 中,并在 C# 程序中调用这些 C++ 函数。我们将重点介绍以下几个方面:

  • DLL 的创建:我们将展示如何用 C++ 编写 DLL 并导出函数,使其可以被 C# 代码调用。
  • 数据类型和结构体的传递:在 C++ 和 C# 之间传递基本数据类型和结构体时的注意事项,以及如何保证数据一致性。
  • 类对象的管理:如何在 C# 中创建和销毁 C++ 类对象,以及如何调用类成员函数。
  • 字符串和内存管理:处理 C++ 中的字符串和动态分配内存的方式,以及如何在 C# 中正确处理这些情况。

2 示例代码

2.1 myapi.h

#pragma once

#ifdef MYAPI_EXPORTS
#define MYAPI __declspec(dllexport) // 导出符号
#else
#define MYAPI __declspec(dllimport) // 导入符号
#endif

#ifdef __cplusplus
extern "C" {
#endif

    // 加法函数,返回两个整数的和
    MYAPI int AddNumbers(int a, int b);

    // 加法函数,返回两个浮点数的和
    MYAPI double AddDoubles(double a, double b);

    // 加法函数,通过引用参数返回两个浮点数的和
    MYAPI void AddDoublesByRef(double* a, double* b, double* result);

    // 获取字符值的函数
    MYAPI char GetCharValue(char c);

    // 打印字符串到控制台的函数
    MYAPI void PrintString(const char* str);

    // 获取中文字符串的函数,返回一个指向 UTF-8 编码字符串的指针
    MYAPI const char* GetChineseString();

    // 释放由 GetChineseString 函数分配的内存
    MYAPI void FreeString(const char* str);

    // 结构体类型定义,表示一个包含 x 和 y 两个整数的结构体
    typedef struct {
        int x;
        int y;
    } MyStruct;

    // 结构体加法函数,返回两个 MyStruct 结构体的和
    MYAPI MyStruct AddStructs(MyStruct a, MyStruct b);

    // 类定义,表示一个包含整数值的类
    class MyClass {
    public:
        int value; // 类的成员变量,表示一个整数值

        // 构造函数,初始化 value
        MyClass(int v) : value(v) {}

        // 成员函数,返回 value 的值
        int GetValue() const { return value; }
    };

    // 创建 MyClass 对象的函数,返回指向 MyClass 对象的指针
    MYAPI MyClass* CreateMyClass(int value);

    // 销毁 MyClass 对象的函数,释放对象内存
    MYAPI void DestroyMyClass(MyClass* obj);

    // 获取 MyClass 对象值的函数,返回 MyClass 对象的 value 值
    MYAPI int GetMyClassValue(MyClass* obj);

#ifdef __cplusplus
}
#endif

2.2 myapi.cpp

#include "myapi.h"
#include <iostream>
#include <cstring>

// 实现 AddNumbers 函数,返回两个整数的和
MYAPI int AddNumbers(int a, int b) {
    return a + b;
}

// 实现 AddDoubles 函数,返回两个浮点数的和
MYAPI double AddDoubles(double a, double b) {
    return a + b;
}

// 实现 AddDoublesByRef 函数,通过引用参数返回两个浮点数的和
MYAPI void AddDoublesByRef(double* a, double* b, double* result) {
    *result = *a + *b;
}

// 实现 GetCharValue 函数,返回传入的字符值
MYAPI char GetCharValue(char c) {
    return c;
}

// 实现 PrintString 函数,将字符串打印到控制台
MYAPI void PrintString(const char* str) {
    std::cout << str << std::endl;
}

// 实现 GetChineseString 函数,返回一个包含中文的 UTF-8 编码字符串
MYAPI const char* GetChineseString() {
    const char* str = u8"你好,世界!"; // 中文字符串的 UTF-8 编码
    size_t len = strlen(str) + 1; // 字符串长度加上一个结尾的 null 字符
    char* result = new char[len]; // 动态分配内存
    strcpy_s(result, len, str); // 复制字符串到新分配的内存中
    return result; // 返回新分配的字符串
}

// 实现 FreeString 函数,释放由 GetChineseString 函数分配的内存
MYAPI void FreeString(const char* str) {
    delete[] str; // 释放内存
}

// 实现 AddStructs 函数,返回两个 MyStruct 结构体的和
MYAPI MyStruct AddStructs(MyStruct a, MyStruct b) {
    MyStruct result;
    result.x = a.x + b.x; // 计算 x 的和
    result.y = a.y + b.y; // 计算 y 的和
    return result;
}

// 实现 CreateMyClass 函数,创建 MyClass 对象并返回指向对象的指针
MYAPI MyClass* CreateMyClass(int value) {
    return new MyClass(value); // 使用构造函数创建对象
}

// 实现 DestroyMyClass 函数,销毁 MyClass 对象并释放内存
MYAPI void DestroyMyClass(MyClass* obj) {
    delete obj; // 释放对象内存
}

// 实现 GetMyClassValue 函数,返回 MyClass 对象的 value 值
MYAPI int GetMyClassValue(MyClass* obj) {
    return obj->GetValue(); // 调用成员函数获取 value 值
}

2.3 main.cs

using System;
using System.Runtime.InteropServices;
using System.Text;

class Program
{
    // 导入 AddNumbers 函数,调用约定为 Cdecl
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int AddNumbers(int a, int b);

    // 导入 AddDoubles 函数,调用约定为 Cdecl
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern double AddDoubles(double a, double b);

    // 导入 AddDoublesByRef 函数,调用约定为 Cdecl,通过引用参数返回两个浮点数的和
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void AddDoublesByRef(ref double a, ref double b, out double result);

    // 导入 GetCharValue 函数,调用约定为 Cdecl,返回传入的字符值
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern char GetCharValue(char c);

    // 导入 PrintString 函数,调用约定为 Cdecl,将字符串打印到控制台
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void PrintString([MarshalAs(UnmanagedType.LPStr)] string str);

    // 导入 GetChineseString 函数,调用约定为 Cdecl,返回指向 UTF-8 编码中文字符串的指针
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern IntPtr GetChineseString();

    // 导入 FreeString 函数,调用约定为 Cdecl,释放由 GetChineseString 分配的内存
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern void FreeString(IntPtr str);

    // 定义 MyStruct 结构体,使用 LayoutKind.Sequential 以确保内存布局顺序
    [StructLayout(LayoutKind.Sequential)]
    public struct MyStruct
    {
        public int x; // 结构体的 x 成员
        public int y; // 结构体的 y 成员
    }

    // 导入 AddStructs 函数,调用约定为 Cdecl,返回两个 MyStruct 结构体的和
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern MyStruct AddStructs(MyStruct a, MyStruct b);

    // 导入 CreateMyClass 函数,调用约定为 Cdecl,创建 MyClass 对象并返回对象指针
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr CreateMyClass(int value);

    // 导入 DestroyMyClass 函数,调用约定为 Cdecl,销毁 MyClass 对象并释放内存
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void DestroyMyClass(IntPtr obj);

    // 导入 GetMyClassValue 函数,调用约定为 Cdecl,获取 MyClass 对象的 value 值
    [DllImport("C:\\Users\\hp\\source\\repos\\ConsoleApplication3\\x64\\Debug\\Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int GetMyClassValue(IntPtr obj);

    static void Main(string[] args)
    {
        // 测试 AddNumbers 函数
        int sum = AddNumbers(5, 3);
        Console.WriteLine("Result of adding numbers: " + sum);

        // 测试 AddDoubles 函数
        double doubleSum = AddDoubles(5.5, 3.3);
        Console.WriteLine("Result of adding doubles: " + doubleSum);

        // 测试 AddDoublesByRef 函数
        double a = 5.5;
        double b = 3.3;
        double result;
        AddDoublesByRef(ref a, ref b, out result);
        Console.WriteLine("Result of adding doubles by ref: " + result);

        // 测试 GetCharValue 函数
        char charResult = GetCharValue('A');
        Console.WriteLine("Result of GetCharValue: " + charResult);

        // 测试 PrintString 函数
        PrintString("Hello from C++");

        // 测试 GetChineseString 函数
        IntPtr ptrChineseString = GetChineseString();
        string chineseString = Marshal.PtrToStringUTF8(ptrChineseString);
        Console.WriteLine("Chinese string from C++: " + chineseString);
        FreeString(ptrChineseString); // 释放中文字符串内存

        // 测试 AddStructs 函数
        MyStruct structA = new MyStruct { x = 1, y = 2 };
        MyStruct structB = new MyStruct { x = 3, y = 4 };
        MyStruct structResult = AddStructs(structA, structB);
        Console.WriteLine("Result of adding structs: (" + structResult.x + ", " + structResult.y + ")");

        // 测试 MyClass 相关函数
        IntPtr myClassInstance = CreateMyClass(42);
        int classValue = GetMyClassValue(myClassInstance);
        Console.WriteLine("Value from MyClass: " + classValue);
        DestroyMyClass(myClassInstance); // 销毁 MyClass 对象
    }
}
  • 13
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满天飞飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值