DutyContent/ThirdParty/NativeMethods.cs

127 lines
3.7 KiB
C#
Raw Permalink Normal View History

2021-03-17 18:14:48 +01:00
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DutyContent.ThirdParty
{
class NativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
public static void ScrollToBottom(RichTextBox richTextBox)
{
SendMessage(richTextBox.Handle, 277, (IntPtr)7, IntPtr.Zero);
richTextBox.SelectionStart = richTextBox.Text.Length;
}
[DllImport("user32.dll", SetLastError = true)]
2021-04-28 06:46:03 +02:00
public static extern IntPtr GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLong(IntPtr hwnd, int index, IntPtr dwNewLong);
2021-03-17 18:14:48 +01:00
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint access_flag, bool inherit_handle, int process_id);
[DllImport("kernel32.dll")]
public static extern int CloseHandle(IntPtr handle);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int process_id);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
public class ProcessHandle
{
public Process Process { get; private set; }
public IntPtr Handle { get; private set; }
public System.Net.IPEndPoint EndPoint { get; set; }
public ProcessHandle(Process p)
{
Process = p;
if (p != null && !p.HasExited)
Handle = OpenProcess(16, false, p.Id);
}
~ProcessHandle()
{
if (Handle != IntPtr.Zero)
CloseHandle(Handle);
}
}
[DllImport("Iphlpapi.dll", SetLastError = true)]
public static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, AddressFamily ipVersion, int tcpTableType, int reserved = 0);
[StructLayout(LayoutKind.Sequential)]
public struct TcpTable
{
public uint entries;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 1)]
public TcpRow[] row;
}
[StructLayout(LayoutKind.Sequential)]
2021-08-16 19:04:55 +02:00
public struct TcpRow : IEquatable<TcpRow>, IComparable<TcpRow>
2021-03-17 18:14:48 +01:00
{
public TcpState 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(__localAddr);
public ushort LocalPort => BitConverter.ToUInt16(new byte[2] { __localPort[1], __localPort[0] }, 0);
public IPAddress RemoteAddress => new IPAddress(__remoteAddr);
public ushort RemotePort => BitConverter.ToUInt16(new byte[2] { __remotePort[1], __remotePort[0] }, 0);
2021-08-16 19:04:55 +02:00
public int CompareTo(TcpRow other)
{
// just sorting by remote address for this project
return __remoteAddr.CompareTo(other.__remoteAddr);
}
2021-03-17 18:14:48 +01:00
public bool Equals(TcpRow right)
{
return
__localAddr == right.__localAddr && LocalPort == right.LocalPort &&
__remoteAddr == right.__remoteAddr && RemotePort == right.RemotePort &&
owningPid == right.owningPid;
}
public override bool Equals(object obj)
{
return !(obj is null) && obj is TcpRow && Equals((TcpRow)obj);
}
public override int GetHashCode()
{
unchecked
{
var h = (int)__localAddr;
h = (h * 397) ^ LocalPort;
h = (h * 397) ^ (int)__remoteAddr;
h = (h * 397) ^ RemotePort;
h = (h * 397) ^ (int)owningPid;
return h;
}
}
}
}
}