using System;
using System.Runtime.InteropServices;
class Program
{
// 导入C++ DLL中的函数
[DllImport("MyCppDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int Add(int a, int b);
static void Main(string[] args)
{
// 调用C++ DLL中的Add函数
int result = Add(5, 7);
Console.WriteLine("Result: " + result);
}
}
[DllImport("MyCppDll.dll", CallingConvention = CallingConvention.Cdecl)]
用于导入外部DLL中的函数。MyCppDll.dll
是你编译好的C++动态链接库的名称,CallingConvention.Cdecl
指定了调用约定(C++通常使用 cdecl
)。public static extern int Add(int a, int b);
声明了一个外部方法,该方法在C++ DLL中实现。Main
方法中调用了 Add
函数,并将结果打印到控制台。请确保你的C++ DLL已经正确编译并且位于程序可以找到的位置(例如与可执行文件在同一目录下)。
上一篇:c,c++,c#有何区别
下一篇:c# ??
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站