using System;
using System.Runtime.InteropServices;
namespace CSharpDll
{
public class MathOperations
{
// 使用 DllExport 特性导出函数,采用 Cdecl 调用约定
[DllExport("Add", CallingConvention = CallingConvention.Cdecl)]
public static int Add(int a, int b)
{
return a + b;
}
}
}
#include <stdio.h>
#include <windows.h>
// 定义函数指针类型,与 C# 中导出的函数签名一致
typedef int (*AddFunction)(int, int);
int main()
{
// 加载动态链接库
HINSTANCE hDll = LoadLibrary("CSharpDll.dll");
if (hDll == NULL)
{
printf("无法加载动态链接库!\n");
return 1;
}
// 获取函数地址
AddFunction add = (A