在C#中调用外部函数可以使用DllImport属性来引入外部函数的定义。以下是一个简单的示例:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll")]
public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
static void Main()
{
MessageBox(IntPtr.Zero, "Hello World!", "MessageBox Example", 0);
}
}
在上面的示例中,我们通过DllImport属性引入了user32.dll中的MessageBox函数,并在Main方法中调用了该函数。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:C# Extern相关技术