﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

public class NativeMethods {

    [StructLayout(LayoutKind.Sequential)]
    internal struct WNDCLASSEX {
        public uint cbSize;
        public uint style;
        public Delegate lpfnWndProc;
        public int cbClsExtra;
        public int cbWndExtra;
        public IntPtr hInstance;
        public IntPtr hIcon;
        public IntPtr hCursor;
        public IntPtr hbrBackground;
        public string lpszMenuName;
        public string lpszClassName;
        public IntPtr hIconSm;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct COPYDATASTRUCT {
        public UIntPtr dwData;
        public uint cbData;
        public IntPtr lpData;
    }

    [DllImport("shell32.dll")]
    internal static extern IntPtr ExtractAssociatedIcon(
        IntPtr hInst,
        [In, Out] string lpIconPath,
        [In, Out] ref UIntPtr lpiIcon
    );

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern ushort RegisterClassEx(
        [In] WNDCLASSEX lpwcx
    );

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern IntPtr CreateWindowEx(
        uint dwExStyle,
        [In, Optional] string lpClassName,
        [In, Optional] string lpWindowName,
        [In] uint dwStyle,
        [In] int x,
        [In] int y,
        [In] int nWidth,
        [In] int nHeight,
        [In, Optional] IntPtr hWndParent,
        [In, Optional] IntPtr hMenu,
        [In, Optional] IntPtr hInstance,
        [In, Optional] IntPtr lpParam
    );

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool GetMessage(
        [Out] out Message lpMsg,
        [In, Optional] IntPtr hWnd,
        [In] uint wMsgFilterMin,
        [In] uint wMsgFilterMax
    );

    [DllImport("user32.dll")]
    internal static extern bool TranslateMessage(
        [In] Message m
    );

    [DllImport("user32.dll")]
    internal static extern IntPtr DispatchMessage(
        [In] Message m
    );

    [DllImport("user32.dll")]
    internal static extern IntPtr DefWindowProc(
        [In] IntPtr hWnd,
        [In] uint Msg,
        [In] UIntPtr wParam,
        [In] IntPtr lParam
    );

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool DestroyWindow(
        [In] IntPtr hWnd
    );

    [DllImport("user32.dll")]
    internal static extern IntPtr SetWinEventHook(
        [In] uint eventMin,
        [In] uint eventMax,
        [In] IntPtr hmodWinEventProc,
        [In] Delegate lpfnWinEventProc,
        [In] uint idProcess,
        [In] uint idThread,
        [In] uint dwflags
    );

    [DllImport("user32.dll")]
    internal static extern bool UnhookWinEvent(
        [In] IntPtr hWinEventHook
    );

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern IntPtr FindWindow(
        [In, Optional] string lpClassName,
        [In, Optional] string lpWindowName
    );

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern IntPtr SendMessage(
        [In] IntPtr hWnd,
        [In] uint Msg,
        [In] UIntPtr wParam,
        [In] IntPtr lParam
    );

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern IntPtr SendMessage(
        [In] IntPtr hWnd,
        [In] uint Msg,
        [In] UIntPtr wParam,
        [Out] StringBuilder lParam
    );
}
