﻿// Decompiled with JetBrains decompiler
// Type: Plugin.Helper.NativeMethods
// Assembly: PluginRemoteDesktop, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 51140FE5-D599-450C-ACC7-933BD6C18D0C
// Assembly location: C:\Users\ian\Downloads\Telegram Desktop\fein\Plugins\PluginRemoteDesktop.dll

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


namespace Plugin.Helper
{
	public static class NativeMethods
	{
		[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		internal static extern IntPtr LoadLibrary(string lpFileName);

		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool FreeLibrary(IntPtr hModule);

		[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
		internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

		[DllImport("kernel32.dll", SetLastError = true)]
		internal static extern bool QueryFullProcessImageName(
			[In] IntPtr hProcess,
			[In] uint dwFlags,
			[Out] StringBuilder lpExeName,
			[In, Out] ref uint lpdwSize);

		[DllImport("gdi32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		internal static extern bool BitBlt(
			[In] IntPtr hdc,
			int nXDest,
			int nYDest,
			int nWidth,
			int nHeight,
			[In] IntPtr hdcSrc,
			int nXSrc,
			int nYSrc,
			int dwRop);

		[DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
		internal static extern IntPtr CreateDC(
			string lpszDriver,
			string lpszDevice,
			string lpszOutput,
			IntPtr lpInitData);

		[DllImport("gdi32.dll")]
		internal static extern bool DeleteDC([In] IntPtr hdc);

		[DllImport("user32.dll")]
		internal static extern bool GetLastInputInfo(ref NativeMethods.LASTINPUTINFO plii);

		[DllImport("user32.dll")]
		internal static extern bool SetCursorPos(int x, int y);

		[DllImport("user32.dll")]
		internal static extern IntPtr GetMessageExtraInfo();

		[DllImport("user32.dll")]
		internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] NativeMethods.INPUT[] pInputs, int cbSize);

		[DllImport("user32.dll")]
		internal static extern bool SystemParametersInfo(
			uint uAction,
			uint uParam,
			ref IntPtr lpvParam,
			uint flags);

		[DllImport("user32.dll")]
		internal static extern int PostMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll", CharSet = CharSet.Unicode)]
		internal static extern IntPtr OpenDesktop(
			string hDesktop,
			int flags,
			bool inherit,
			uint desiredAccess);

		[DllImport("user32.dll")]
		internal static extern bool CloseDesktop(IntPtr hDesktop);

		[DllImport("user32.dll")]
		internal static extern bool EnumDesktopWindows(
			IntPtr hDesktop,
			NativeMethods.EnumDesktopWindowsProc callback,
			IntPtr lParam);

		[DllImport("user32.dll")]
		internal static extern bool IsWindowVisible(IntPtr hWnd);

		[DllImport("user32.dll")]
		internal static extern IntPtr GetForegroundWindow();

		[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

		[DllImport("iphlpapi.dll", SetLastError = true)]
		internal static extern uint GetExtendedTcpTable(
			IntPtr pTcpTable,
			ref int dwOutBufLen,
			bool sort,
			int ipVersion,
			NativeMethods.TcpTableClass tblClass,
			uint reserved = 0);

		[DllImport("iphlpapi.dll")]
		internal static extern int SetTcpEntry(IntPtr pTcprow);

		internal struct LASTINPUTINFO
		{
			public static readonly int SizeOf = Marshal.SizeOf(typeof (NativeMethods.LASTINPUTINFO));
			[MarshalAs(UnmanagedType.U4)]
			public uint cbSize;
			[MarshalAs(UnmanagedType.U4)]
			public uint dwTime;
		}

		internal struct INPUT
		{
			internal uint type;
			internal NativeMethods.InputUnion u;

			internal static int Size => Marshal.SizeOf(typeof (NativeMethods.INPUT));
		}

		[StructLayout(LayoutKind.Explicit)]
		internal struct InputUnion
		{
			[FieldOffset(0)]
			internal NativeMethods.MOUSEINPUT mi;
			[FieldOffset(0)]
			internal NativeMethods.KEYBDINPUT ki;
			[FieldOffset(0)]
			internal NativeMethods.HARDWAREINPUT hi;
		}

		internal struct MOUSEINPUT
		{
			internal int dx;
			internal int dy;
			internal int mouseData;
			internal uint dwFlags;
			internal uint time;
			internal IntPtr dwExtraInfo;
		}

		internal struct KEYBDINPUT
		{
			internal ushort wVk;
			internal ushort wScan;
			internal uint dwFlags;
			internal uint time;
			internal IntPtr dwExtraInfo;
		}

		internal struct HARDWAREINPUT
		{
			public uint uMsg;
			public ushort wParamL;
			public ushort wParamH;
		}

		internal delegate bool EnumDesktopWindowsProc(IntPtr hDesktop, IntPtr lParam);

		internal struct MibTcprowOwnerPid
		{
			public uint state;
			public uint localAddr;
			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
			public byte[] localPort;
			public uint remoteAddr;
			[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
			public byte[] remotePort;
			public uint owningPid;

			public IPAddress LocalAddress => new IPAddress((long) this.localAddr);

			public ushort LocalPort
			{
				get
				{
					return BitConverter.ToUInt16(new byte[2]
					{
						this.localPort[1],
						this.localPort[0]
					}, 0);
				}
			}

			public IPAddress RemoteAddress => new IPAddress((long) this.remoteAddr);

			public ushort RemotePort
			{
				get
				{
					return BitConverter.ToUInt16(new byte[2]
					{
						this.remotePort[1],
						this.remotePort[0]
					}, 0);
				}
			}
		}

		internal struct MibTcptableOwnerPid
		{
			public uint dwNumEntries;
			private readonly NativeMethods.MibTcprowOwnerPid table;
		}

		internal enum TcpTableClass
		{
			TcpTableBasicListener,
			TcpTableBasicConnections,
			TcpTableBasicAll,
			TcpTableOwnerPidListener,
			TcpTableOwnerPidConnections,
			TcpTableOwnerPidAll,
			TcpTableOwnerModuleListener,
			TcpTableOwnerModuleConnections,
			TcpTableOwnerModuleAll,
		}
	}
}
