C++基础开发之DLL开发的导出函数与C#导入函数参数类型对齐

前言

在C++中,通过DLL导出函数,通常使用__declspec(dllexport)修饰符。在C#中,通过DllImport来导入C++ DLL中的函数。确保参数类型在C++和C#之间正确对齐非常重要,在 C++ 开发 DLL 提供给 C# 使用的过程中,导出函数的参数类型可以包括多种。以下是一些常见的参数类型,以及每种类型的示例,同时提供了相应的 C# DllImport 函数的编写方式。

基础类型对照

C++ 数据类型 C# 数据类型
int int
unsigned int uint
short short
unsigned short ushort
long long
unsigned long ulong
float float
double double
char char
unsigned char byte
wchar_t char (UTF-16)
bool bool
void void
int* IntPtr
unsigned int* IntPtr
double* IntPtr
char* string (or IntPtr for raw memory)
const char* string (or IntPtr for raw memory)
struct struct (with [StructLayout] if needed)
HANDLE IntPtr

处理案例

1. 处理基础类型:

C++ 导出函数:

// YourLibrary.h

#ifndef YOURLIBRARY_H
#define YOURLIBRARY_H

#ifdef __cplusplus
extern "C" {
   
   
#endif

__declspec(dllexport) int AddTwoNumbers(int a, int b);

#ifdef __cplusplus
}
#endif

#endif // YOURLIBRARY_H

C# 导入函数:

// YourCSharpCode.cs

class Program {
   
   
    [DllImport("YourLibrary.dll")]
    public static extern int AddTwoNumbers(int a, int b);

    static void Main() {
   
   
        int result = AddTwoNumbers(3, 4);
        Console.WriteLine(result); // 输出 7
    }
}

2. 处理指针类型:

C++ 导出函数:

// YourLibrary.h

#ifndef YOURLIBRARY_H
#define YOURLIBRARY_H

#ifdef __cplusplus
extern "C" {
   
   
#endif

__declspec(dllexport) void MultiplyByTwo(int* value);

#ifdef __cplusplus
}
#endif

#endif // YOURLIBRARY_H

C# 导入函数:

// YourCSharpCode.cs

class Program {
   
   
    [DllImport("YourLibrary.dll")]
    public static extern void MultiplyByTwo(ref int value);

    static void Main() {
   
   
        int number = 5;
        MultiplyByTwo(ref number);
        Console.WriteLine(number); // 输出 10
    }
}

3. 处理指针数组:

C++ 导出函数:

// YourLibrary.h

#ifndef YOURLIBRARY_H
#define YOURLIBRARY_H

#ifdef __cplusplus
extern "C" {
   
   
#endif

__declspec(dllexport) void IncrementArray(int* array, int size);

#ifdef __cplusplus
}
#endif

#endif // YOURLIBRARY_H

C# 导入函数:

// YourCSharpCode.cs

class Program {
   
   
    [DllImport("YourLibrary.dll")]
    public static extern void IncrementArray(int[] array, int size);

    static void Main() {
   
   
        int[] numbers = {
   
    1, 2, 3, 4, 5 };
        IncrementArray(numbers, numbers.Length);
        Console.WriteLine(string.Join(", ", numbers)); // 输出 2, 3, 4, 5, 6
    }
}

4. 处理处理结构体:

C++ 导出函数:

// YourLibrary.h

#ifndef YOURLIBRARY_H
#define YOURLIBRARY_H

#ifdef __cplusplus
extern "C" {
   
   
#endif

struct Point {
   
   
    int x;
    int y;
};

__declspec(dllexport) int CalculateDistance(const Point* p1, const Point* p2);

#ifdef __cplusplus
}
#endif

#endif // YOURLIBRARY_H

C# 导入函数:

// YourCSharpCode.cs

[StructLayout(LayoutKind.Sequential)]
public struct Point {
   
   
    public int x;
    public int y;
}

class Program {
   
   
    [DllImport("YourLibrary.dll")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dotnet研习社

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

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

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

打赏作者

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

抵扣说明:

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

余额充值