温馨提示×

extern c#如何调用外部函数

c#
小樊
100
2024-08-21 11:23:28
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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相关技术

0