anshdadhich's picture
CRITICAL FIX: ERROR_HANDLE_EOF = 38 (raw Win32), not 0x80070026 (HRESULT). GetLastWin32Error returns raw codes. Rust windows-rs wraps as HRESULT which confused the original port.
c878efa verified
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;
}
}