2017년 2월 21일 화요일

[C#] Calling a function in Native-DLL from C#(.NET)

It's a simple that to call a function in Native-DLL from C#.

First, import some namespace.

using System;
using System.Runtime.InteropService;

Next, define same function of Native-DLL with extern, static, DllImport attribute.

[DllImport("user32.dll")]
static extern int MessageBox(IntPtr hWnd, string text, string caption, int type);

Last, call the function.

MessageBox(IntPtr.Zero, "Test message", "Test captin", 0);


Following is example that to call MessageBox function of Windows DLL user32.dll.


using System;
using System.Runtime.InteropServices;

public class MsgBoxTest
{
    [DllImport("user32.dll")]
    static extern public int MessageBox(IntPtr hWnd, string text, string caption, int type);
}


public class Program
{
    static void Main(string[] args)
    {
        MsgBoxTest.MessageBox(IntPtr.Zero, "Please do not press this againg", "Attention", 0);
    }
}

댓글 없음:

댓글 쓰기