File size: 7,102 Bytes
fbf0258 776e190 c878efa 776e190 fbf0258 c878efa fbf0258 8c7599c fbf0258 c878efa b5a56e9 fbf0258 8c7599c fbf0258 8c7599c fbf0258 8c7599c fbf0258 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | using System;
using System.Runtime.InteropServices;
using System.Text;
namespace FastSeekWpf.NativeInterop;
internal static class Win32Api
{
// Messages
public const uint WM_USER = 0x0400;
public const uint WM_HOTKEY = 0x0312;
public const uint WM_ACTIVATE = 0x0006;
public const uint WA_INACTIVE = 0;
// Window styles
public const uint WS_POPUP = 0x80000000;
public const uint WS_VISIBLE = 0x10000000;
public const uint WS_EX_TOOLWINDOW = 0x00000080;
public const uint WS_EX_NOACTIVATE = 0x08000000;
public const uint WS_EX_TOPMOST = 0x00000008;
// PeekMessage
public const uint PM_REMOVE = 0x0001;
// File access
public const uint GENERIC_READ = 0x80000000;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const uint FILE_SHARE_DELETE = 0x00000004;
public const uint OPEN_EXISTING = 3;
public const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
public const uint FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000;
// ── IOCTL codes ──
// From Windows SDK winioctl.h. Verified against windows-rs crate.
public const uint FSCTL_ENUM_USN_DATA = 0x000900B3;
public const uint FSCTL_QUERY_USN_JOURNAL = 0x000900F4;
public const uint FSCTL_READ_USN_JOURNAL = 0x000900BB;
// USN reasons
public const uint USN_REASON_FILE_CREATE = 0x00000100;
public const uint USN_REASON_FILE_DELETE = 0x00000200;
public const uint USN_REASON_RENAME_OLD_NAME = 0x00001000;
public const uint USN_REASON_RENAME_NEW_NAME = 0x00002000;
// Win32 error codes (raw, NOT HRESULT)
// Marshal.GetLastWin32Error() returns these raw codes.
// Rust windows-rs wraps them as HRESULT (0x8007XXXX).
public const int ERROR_HANDLE_EOF = 38; // Raw: 0x26
public const int ERROR_INSUFFICIENT_BUFFER = 122; // Raw: 0x7A
public const int ERROR_JOURNAL_NOT_ACTIVE = 1179; // Raw: 0x49B
// DWM
public const int DWMWA_WINDOW_CORNER_PREFERENCE = 33;
public const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
public const int DWMWCP_ROUND = 2;
// Kernel32
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr CreateFileW(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToRead,
out uint lpNumberOfBytesRead,
IntPtr lpOverlapped);
// Unsafe overload: read into a byte[] at a given offset — matches Rust ReadFile into &mut buffer[leftover..]
public static unsafe bool ReadFile(
IntPtr hFile,
byte[] lpBuffer,
int offset,
uint nNumberOfBytesToRead,
out uint lpNumberOfBytesRead,
IntPtr lpOverlapped)
{
fixed (byte* p = &lpBuffer[offset])
{
return ReadFilePointer(hFile, p, nNumberOfBytesToRead, out lpNumberOfBytesRead, lpOverlapped);
}
}
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "ReadFile")]
private static extern unsafe bool ReadFilePointer(
IntPtr hFile,
byte* lpBuffer,
uint nNumberOfBytesToRead,
out uint lpNumberOfBytesRead,
IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetFilePointerEx(
IntPtr hFile,
long liDistanceToMove,
out long lpNewFilePointer,
uint dwMoveMethod);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
IntPtr lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern uint GetLogicalDriveStringsW(uint nBufferLength, [Out] char[] lpBuffer);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetVolumeInformationW(
string lpRootPathName,
StringBuilder? lpVolumeNameBuffer,
uint nVolumeNameSize,
out uint lpVolumeSerialNumber,
out uint lpMaximumComponentLength,
out uint lpFileSystemFlags,
StringBuilder lpFileSystemNameBuffer,
uint nFileSystemNameSize);
// User32
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowW(string? lpClassName, string? lpWindowName);
[DllImport("user32.dll")]
public static extern bool PostMessageW(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
public static extern int GetMessageW(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
[DllImport("user32.dll")]
public static extern bool PeekMessageW(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg);
[DllImport("user32.dll")]
public static extern bool TranslateMessage(ref MSG lpMsg);
[DllImport("user32.dll")]
public static extern IntPtr DispatchMessageW(ref MSG lpMsg);
[DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);
public const int SM_CXSCREEN = 0;
public const int SM_CYSCREEN = 1;
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
// Shell32
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ShellExecuteW(
IntPtr hwnd,
string lpOperation,
string lpFile,
string? lpParameters,
string? lpDirectory,
int nShowCmd);
// Dwmapi
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
// Structs
[StructLayout(LayoutKind.Sequential)]
public struct MSG
{
public IntPtr hwnd;
public uint message;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public POINT pt;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
}
|