C#生成DLL给C语言调用的例子

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 
#include 

// 定义函数指针类型,与 C# 中导出的函数签名一致
typedef int (*AddFunction)(int, int);

int main()
{
    // 加载动态链接库
    HINSTANCE hDll = LoadLibrary("CSharpDll.dll");
    if (hDll == NULL)
    {
        printf("无法加载动态链接库!\n");
        return 1;
    }

    // 获取函数地址
    AddFunction add = 

你可能感兴趣的:(C#入门到精通,c#,c语言)