2017년 2월 21일 화요일

[C#] A example that to call any Native-DLL functions from C#.

using System;
using System.Runtime.InteropServices;
using System.Text;

// define class for struct of native-dll
[StructLayout(LayoutKind.Sequential)]
public class SystemTIme
{
    public ushort Year;
    public ushort Month;
    public ushort DayOfWeek;
    public ushort Day;
    public ushort Hour;
    public ushort Minute;
    public ushort Second;
    public ushort Milliseconds;
}

// define some functions for Native-DLL
public class NativeDLL
{
    [DllImport("user32.dll")]
    static extern public int MessageBox(IntPtr hWnd, string text, string caption, int type);

    [DllImport("kernel32.dll")]
    static extern public int GetWindowsDirectory(StringBuilder sb, int maxChars);

    [DllImport("kernel32.dll")]
    static extern public void GetSystemTime(SystemTIme t);
 

}

// Call the functions of Native-DLL
public class Program
{
    static void Main(string[] args)
    {
        // message box test
        NativeDLL.MessageBox(IntPtr.Zero, "Please do not press this againg", "Attention", 0);

        // get windows directory
        StringBuilder sb = new StringBuilder();
        NativeDLL.GetWindowsDirectory(sb, 256);
        NativeDLL.MessageBox(IntPtr.Zero, sb.ToString(), "Directory", 0);

        // get system time
        SystemTIme t = new SystemTIme();
        NativeDLL.GetSystemTime(t);
        NativeDLL.MessageBox(IntPtr.Zero, string.Format("{0}년 {1}월 {2}일 {3}시 {4}분 {5}초", t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second), "System Time", 0);

     
    }
}

댓글 없음:

댓글 쓰기