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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
using System;
|
| 2 |
+
using System.Runtime.InteropServices;
|
| 3 |
+
using System.Text;
|
| 4 |
+
|
| 5 |
+
namespace FastSeekWpf.NativeInterop;
|
| 6 |
+
|
| 7 |
+
internal static class Win32Api
|
| 8 |
+
{
|
| 9 |
+
// Messages
|
| 10 |
+
public const uint WM_USER = 0x0400;
|
| 11 |
+
public const uint WM_HOTKEY = 0x0312;
|
| 12 |
+
public const uint WM_ACTIVATE = 0x0006;
|
| 13 |
+
public const uint WA_INACTIVE = 0;
|
| 14 |
+
|
| 15 |
+
// Window styles
|
| 16 |
+
public const uint WS_POPUP = 0x80000000;
|
| 17 |
+
public const uint WS_VISIBLE = 0x10000000;
|
| 18 |
+
public const uint WS_EX_TOOLWINDOW = 0x00000080;
|
| 19 |
+
public const uint WS_EX_NOACTIVATE = 0x08000000;
|
| 20 |
+
public const uint WS_EX_TOPMOST = 0x00000008;
|
| 21 |
+
|
| 22 |
+
// PeekMessage
|
| 23 |
+
public const uint PM_REMOVE = 0x0001;
|
| 24 |
+
|
| 25 |
+
// File access
|
| 26 |
+
public const uint GENERIC_READ = 0x80000000;
|
| 27 |
+
public const uint FILE_SHARE_READ = 0x00000001;
|
| 28 |
+
public const uint FILE_SHARE_WRITE = 0x00000002;
|
| 29 |
+
public const uint FILE_SHARE_DELETE = 0x00000004;
|
| 30 |
+
public const uint OPEN_EXISTING = 3;
|
| 31 |
+
public const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
|
| 32 |
+
public const uint FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000;
|
| 33 |
+
public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
|
| 34 |
+
public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
|
| 35 |
+
|
| 36 |
+
// IOCTL codes
|
| 37 |
+
public const uint FSCTL_ENUM_USN_DATA = 0x900B03;
|
| 38 |
+
public const uint FSCTL_QUERY_USN_JOURNAL = 0x900F44;
|
| 39 |
+
public const uint FSCTL_READ_USN_JOURNAL = 0x900BB;
|
| 40 |
+
public const uint FSCTL_READ_FILE_USN_DATA = 0x900EB;
|
| 41 |
+
|
| 42 |
+
// USN reasons
|
| 43 |
+
public const uint USN_REASON_FILE_CREATE = 0x00000100;
|
| 44 |
+
public const uint USN_REASON_FILE_DELETE = 0x00000200;
|
| 45 |
+
public const uint USN_REASON_RENAME_OLD_NAME = 0x00001000;
|
| 46 |
+
public const uint USN_REASON_RENAME_NEW_NAME = 0x00002000;
|
| 47 |
+
|
| 48 |
+
// Error codes
|
| 49 |
+
public const uint ERROR_HANDLE_EOF = 0x80070026;
|
| 50 |
+
public const uint ERROR_JOURNAL_NOT_ACTIVE = 1179;
|
| 51 |
+
|
| 52 |
+
// Kernel32
|
| 53 |
+
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
| 54 |
+
public static extern IntPtr CreateFileW(
|
| 55 |
+
string lpFileName,
|
| 56 |
+
uint dwDesiredAccess,
|
| 57 |
+
uint dwShareMode,
|
| 58 |
+
IntPtr lpSecurityAttributes,
|
| 59 |
+
uint dwCreationDisposition,
|
| 60 |
+
uint dwFlagsAndAttributes,
|
| 61 |
+
IntPtr hTemplateFile);
|
| 62 |
+
|
| 63 |
+
[DllImport("kernel32.dll", SetLastError = true)]
|
| 64 |
+
public static extern bool ReadFile(
|
| 65 |
+
IntPtr hFile,
|
| 66 |
+
byte[] lpBuffer,
|
| 67 |
+
uint nNumberOfBytesToRead,
|
| 68 |
+
out uint lpNumberOfBytesRead,
|
| 69 |
+
IntPtr lpOverlapped);
|
| 70 |
+
|
| 71 |
+
[DllImport("kernel32.dll", SetLastError = true)]
|
| 72 |
+
public static extern bool SetFilePointerEx(
|
| 73 |
+
IntPtr hFile,
|
| 74 |
+
long liDistanceToMove,
|
| 75 |
+
out long lpNewFilePointer,
|
| 76 |
+
uint dwMoveMethod);
|
| 77 |
+
|
| 78 |
+
[DllImport("kernel32.dll", SetLastError = true)]
|
| 79 |
+
public static extern bool DeviceIoControl(
|
| 80 |
+
IntPtr hDevice,
|
| 81 |
+
uint dwIoControlCode,
|
| 82 |
+
IntPtr lpInBuffer,
|
| 83 |
+
uint nInBufferSize,
|
| 84 |
+
IntPtr lpOutBuffer,
|
| 85 |
+
uint nOutBufferSize,
|
| 86 |
+
out uint lpBytesReturned,
|
| 87 |
+
IntPtr lpOverlapped);
|
| 88 |
+
|
| 89 |
+
[DllImport("kernel32.dll", SetLastError = true)]
|
| 90 |
+
public static extern bool CloseHandle(IntPtr hObject);
|
| 91 |
+
|
| 92 |
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
| 93 |
+
public static extern uint GetLogicalDriveStringsW(uint nBufferLength, [Out] char[] lpBuffer);
|
| 94 |
+
|
| 95 |
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
| 96 |
+
public static extern bool GetVolumeInformationW(
|
| 97 |
+
string lpRootPathName,
|
| 98 |
+
StringBuilder? lpVolumeNameBuffer,
|
| 99 |
+
uint nVolumeNameSize,
|
| 100 |
+
out uint lpVolumeSerialNumber,
|
| 101 |
+
out uint lpMaximumComponentLength,
|
| 102 |
+
out uint lpFileSystemFlags,
|
| 103 |
+
StringBuilder lpFileSystemNameBuffer,
|
| 104 |
+
uint nFileSystemNameSize);
|
| 105 |
+
|
| 106 |
+
[DllImport("kernel32.dll")]
|
| 107 |
+
public static extern IntPtr GetCurrentProcess();
|
| 108 |
+
|
| 109 |
+
[DllImport("kernel32.dll")]
|
| 110 |
+
public static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
|
| 111 |
+
|
| 112 |
+
// User32
|
| 113 |
+
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
| 114 |
+
public static extern IntPtr FindWindowW(string? lpClassName, string? lpWindowName);
|
| 115 |
+
|
| 116 |
+
[DllImport("user32.dll")]
|
| 117 |
+
public static extern bool PostMessageW(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
| 118 |
+
|
| 119 |
+
[DllImport("user32.dll", SetLastError = true)]
|
| 120 |
+
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
|
| 121 |
+
|
| 122 |
+
[DllImport("user32.dll", SetLastError = true)]
|
| 123 |
+
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
|
| 124 |
+
|
| 125 |
+
[DllImport("user32.dll")]
|
| 126 |
+
public static extern bool PeekMessageW(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg);
|
| 127 |
+
|
| 128 |
+
[DllImport("user32.dll")]
|
| 129 |
+
public static extern bool TranslateMessage(ref MSG lpMsg);
|
| 130 |
+
|
| 131 |
+
[DllImport("user32.dll")]
|
| 132 |
+
public static extern IntPtr DispatchMessageW(ref MSG lpMsg);
|
| 133 |
+
|
| 134 |
+
[DllImport("user32.dll")]
|
| 135 |
+
public static extern int GetSystemMetrics(int nIndex);
|
| 136 |
+
public const int SM_CXSCREEN = 0;
|
| 137 |
+
public const int SM_CYSCREEN = 1;
|
| 138 |
+
|
| 139 |
+
[DllImport("user32.dll")]
|
| 140 |
+
public static extern bool SetForegroundWindow(IntPtr hWnd);
|
| 141 |
+
|
| 142 |
+
[DllImport("user32.dll")]
|
| 143 |
+
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
| 144 |
+
public const int SW_HIDE = 0;
|
| 145 |
+
public const int SW_SHOW = 5;
|
| 146 |
+
|
| 147 |
+
// Shell32
|
| 148 |
+
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
|
| 149 |
+
public static extern IntPtr ShellExecuteW(
|
| 150 |
+
IntPtr hwnd,
|
| 151 |
+
string lpOperation,
|
| 152 |
+
string lpFile,
|
| 153 |
+
string? lpParameters,
|
| 154 |
+
string? lpDirectory,
|
| 155 |
+
int nShowCmd);
|
| 156 |
+
|
| 157 |
+
// Structs
|
| 158 |
+
[StructLayout(LayoutKind.Sequential)]
|
| 159 |
+
public struct MSG
|
| 160 |
+
{
|
| 161 |
+
public IntPtr hwnd;
|
| 162 |
+
public uint message;
|
| 163 |
+
public IntPtr wParam;
|
| 164 |
+
public IntPtr lParam;
|
| 165 |
+
public uint time;
|
| 166 |
+
public int pt_x;
|
| 167 |
+
public int pt_y;
|
| 168 |
+
}
|
| 169 |
+
}
|