温馨提示×

extern c#如何调用外部函数

c#
小樊
87
2024-08-21 11:23:28
栏目: 编程语言

在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方法中调用了该函数。

0