226 lines
7.1 KiB
C#
226 lines
7.1 KiB
C#
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace AutoAgent.Domain
|
|
{
|
|
internal static class User32Dll
|
|
{
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct MOUSEINPUT
|
|
{
|
|
public int dx;
|
|
public int dy;
|
|
public int mouseData;
|
|
public uint dwFlags;
|
|
public uint time;
|
|
public IntPtr dwExtraInfo;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct KEYBDINPUT
|
|
{
|
|
public ushort wVk;
|
|
public ushort wScan;
|
|
public uint dwFlags;
|
|
public uint time;
|
|
public IntPtr dwExtraInfo;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public struct INPUT
|
|
{
|
|
[FieldOffset(0)]
|
|
public int type;
|
|
[FieldOffset(8)]
|
|
public MOUSEINPUT mi;
|
|
[FieldOffset(8)]
|
|
public KEYBDINPUT ki;
|
|
}
|
|
|
|
public static readonly int INPUT_SIZE = Marshal.SizeOf(typeof(INPUT));
|
|
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public struct RECT
|
|
{
|
|
[FieldOffset(0)] public int Left;
|
|
[FieldOffset(4)] public int Top;
|
|
[FieldOffset(8)] public int Right;
|
|
[FieldOffset(12)] public int Bottom;
|
|
[FieldOffset(0)] public int X;
|
|
[FieldOffset(4)] public int Y;
|
|
}
|
|
|
|
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern uint SendInput(uint nInputs, IntPtr pInputs, int cbSize);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern short GetKeyState(int nVirtKey);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern int MapVirtualKey(int uCode, uint uMapType);
|
|
|
|
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool ScreenToClient(IntPtr hWnd, ref RECT lpPoint);
|
|
|
|
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
public static extern int GetWindowTextLength(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
|
|
|
|
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool GetCursorPos(out RECT lpPoint);
|
|
|
|
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool IsWindowVisible(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern IntPtr GetForegroundWindow();
|
|
|
|
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
|
|
|
|
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool GetClientRect(IntPtr hwnd, out RECT lpRect);
|
|
|
|
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool ClientToScreen(IntPtr hWnd, ref RECT lpPoint);
|
|
|
|
[DllImport("user32.dll")]
|
|
public static extern int GetSystemMetrics(int nIndex);
|
|
}
|
|
|
|
internal static class Kernel32Dll
|
|
{
|
|
[Flags]
|
|
public enum MemoryState : uint
|
|
{
|
|
Commit = 0x00001000,
|
|
Reserve = 0x00002000,
|
|
Free = 0x00010000,
|
|
}
|
|
|
|
[Flags]
|
|
public enum MemoryType : uint
|
|
{
|
|
Image = 0x01000000,
|
|
Mapped = 0x00040000,
|
|
Private = 0x00020000,
|
|
}
|
|
|
|
[Flags]
|
|
public enum MemoryPageProtectionState : uint
|
|
{
|
|
NoAccess = 0x0001,
|
|
ReadOnly = 0x0002,
|
|
ReadWrite = 0x0004,
|
|
WriteCopy = 0x0008,
|
|
Execute = 0x0010,
|
|
ExecuteRead = 0x0020,
|
|
ExecuteReadWrite = 0x0040,
|
|
ExecuteWriteCopy = 0x0080,
|
|
Guard = 0x0100,
|
|
NoCache = 0x0200,
|
|
WriteCombine = 0x0400,
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct MemoryBasicInformation
|
|
{
|
|
public IntPtr BaseAddress;
|
|
public IntPtr AllocationBase;
|
|
public uint AllocationProtect;
|
|
public ushort PartitionId;
|
|
public UIntPtr RegionSize;
|
|
public MemoryState State;
|
|
public MemoryPageProtectionState PageProtection;
|
|
public MemoryType Type;
|
|
}
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
public static extern IntPtr OpenProcess(
|
|
uint dwDesiredAccess,
|
|
[MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
|
|
int dwProcessId
|
|
);
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool CloseHandle(IntPtr hObject);
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
public static extern int VirtualQueryEx(
|
|
IntPtr hProcess,
|
|
IntPtr lpAddress,
|
|
out MemoryBasicInformation lpBuffer,
|
|
uint dwLength
|
|
);
|
|
}
|
|
|
|
internal static class PsApiDll
|
|
{
|
|
[Flags]
|
|
public enum ModuleFilterFlags : uint
|
|
{
|
|
Default = 0x00,
|
|
Modules32Bit = 0x01,
|
|
Modules64Bit = 0x02,
|
|
ModulesAll = Modules32Bit | Modules64Bit,
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct ModuleInformation
|
|
{
|
|
public IntPtr BaseAddress;
|
|
public uint MemorySize;
|
|
public IntPtr EntryPointAddress;
|
|
public StringBuilder Name;
|
|
public StringBuilder FileName;
|
|
}
|
|
|
|
[DllImport("psapi.dll", SetLastError = true)]
|
|
public static extern bool EnumProcessModulesEx(
|
|
IntPtr hProcess,
|
|
out IntPtr[] lphModule,
|
|
uint cb,
|
|
out uint lpcbNeeded,
|
|
ModuleFilterFlags dwFilterFlag
|
|
);
|
|
|
|
[DllImport("psapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
|
public static extern uint GetModuleFileNameEx(
|
|
IntPtr hProcess,
|
|
IntPtr hModule,
|
|
[Out] StringBuilder lpFilename,
|
|
uint nSize
|
|
);
|
|
|
|
[DllImport("psapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
|
public static extern uint GetModuleBaseName(
|
|
IntPtr hProcess,
|
|
IntPtr hModule,
|
|
out StringBuilder lpBaseName,
|
|
uint nSize
|
|
);
|
|
|
|
[DllImport("psapi.dll", SetLastError = true)]
|
|
public static extern bool GetModuleInformation(
|
|
IntPtr hProcess,
|
|
IntPtr hModule,
|
|
out ModuleInformation lpmodinfo,
|
|
uint cb
|
|
);
|
|
}
|
|
}
|