|
| 1 | +using Microsoft.Win32.SafeHandles; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Files.Helpers |
| 11 | +{ |
| 12 | + public class NativeDirectoryChangesHelper |
| 13 | + { |
| 14 | + [DllImport("api-ms-win-core-handle-l1-1-0.dll")] |
| 15 | + public static extern bool CloseHandle(IntPtr hObject); |
| 16 | + |
| 17 | + [DllImport("api-ms-win-core-io-l1-1-1.dll")] |
| 18 | + public static extern bool GetOverlappedResult(IntPtr hFile, OVERLAPPED lpOverlapped, out int lpNumberOfBytesTransferred, bool bWait); |
| 19 | + |
| 20 | + [DllImport("api-ms-win-core-io-l1-1-1.dll")] |
| 21 | + public static extern bool CancelIo(IntPtr hFile); |
| 22 | + |
| 23 | + [DllImport("api-ms-win-core-synch-l1-2-0.dll")] |
| 24 | + public static extern uint WaitForMultipleObjectsEx(uint nCount, IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds, bool bAlertable); |
| 25 | + |
| 26 | + [DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)] |
| 27 | + public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName); |
| 28 | + |
| 29 | + [DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)] |
| 30 | + public static extern bool ResetEvent(IntPtr hEvent); |
| 31 | + |
| 32 | + [DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)] |
| 33 | + public static extern UInt32 WaitForSingleObjectEx(IntPtr hHandle, UInt32 dwMilliseconds, bool bAlertable); |
| 34 | + |
| 35 | + public enum File_Attributes : uint |
| 36 | + { |
| 37 | + Readonly = 0x00000001, |
| 38 | + Hidden = 0x00000002, |
| 39 | + System = 0x00000004, |
| 40 | + Directory = 0x00000010, |
| 41 | + Archive = 0x00000020, |
| 42 | + Device = 0x00000040, |
| 43 | + Normal = 0x00000080, |
| 44 | + Temporary = 0x00000100, |
| 45 | + SparseFile = 0x00000200, |
| 46 | + ReparsePoint = 0x00000400, |
| 47 | + Compressed = 0x00000800, |
| 48 | + Offline = 0x00001000, |
| 49 | + NotContentIndexed = 0x00002000, |
| 50 | + Encrypted = 0x00004000, |
| 51 | + Write_Through = 0x80000000, |
| 52 | + Overlapped = 0x40000000, |
| 53 | + NoBuffering = 0x20000000, |
| 54 | + RandomAccess = 0x10000000, |
| 55 | + SequentialScan = 0x08000000, |
| 56 | + DeleteOnClose = 0x04000000, |
| 57 | + BackupSemantics = 0x02000000, |
| 58 | + PosixSemantics = 0x01000000, |
| 59 | + OpenReparsePoint = 0x00200000, |
| 60 | + OpenNoRecall = 0x00100000, |
| 61 | + FirstPipeInstance = 0x00080000 |
| 62 | + } |
| 63 | + |
| 64 | + public const uint GENERIC_READ = 0x80000000; |
| 65 | + |
| 66 | + [DllImport("api-ms-win-core-file-fromapp-l1-1-0.dll", CharSet = CharSet.Auto, |
| 67 | + CallingConvention = CallingConvention.StdCall, |
| 68 | + SetLastError = true)] |
| 69 | + public static extern IntPtr CreateFileFromApp( |
| 70 | + string lpFileName, |
| 71 | + uint dwDesiredAccess, |
| 72 | + uint dwShareMode, |
| 73 | + IntPtr SecurityAttributes, |
| 74 | + uint dwCreationDisposition, |
| 75 | + uint dwFlagsAndAttributes, |
| 76 | + IntPtr hTemplateFile |
| 77 | + ); |
| 78 | + |
| 79 | + [DllImport("api-ms-win-core-file-fromapp-l1-1-0.dll", CharSet = CharSet.Auto, |
| 80 | + CallingConvention = CallingConvention.StdCall, |
| 81 | + SetLastError = true)] |
| 82 | + public static extern IntPtr CreateFile2FromApp( |
| 83 | + string lpFileName, |
| 84 | + uint dwDesiredAccess, |
| 85 | + uint dwShareMode, |
| 86 | + uint dwCreationDisposition, |
| 87 | + IntPtr pCreateExParams |
| 88 | + ); |
| 89 | + |
| 90 | + public delegate void LpoverlappedCompletionRoutine(uint dwErrorCode, |
| 91 | + uint dwNumberOfBytesTransfered, |
| 92 | + OVERLAPPED lpOverlapped |
| 93 | + ); |
| 94 | + |
| 95 | + public unsafe struct OVERLAPPED |
| 96 | + { |
| 97 | + public IntPtr Internal; |
| 98 | + public IntPtr InternalHigh; |
| 99 | + public Union PointerAndOffset; |
| 100 | + public IntPtr hEvent; |
| 101 | + |
| 102 | + [StructLayout(LayoutKind.Explicit)] |
| 103 | + public struct Union |
| 104 | + { |
| 105 | + [FieldOffset(0)] public void* IntPtr; |
| 106 | + [FieldOffset(0)] public OffsetPair Offset; |
| 107 | + |
| 108 | + public struct OffsetPair { public uint Offset; public uint OffsetHigh; } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public const int FILE_NOTIFY_CHANGE_FILE_NAME = 1; |
| 113 | + public const int FILE_NOTIFY_CHANGE_DIR_NAME = 2; |
| 114 | + |
| 115 | + public unsafe struct FILE_NOTIFY_INFORMATION |
| 116 | + { |
| 117 | + public uint NextEntryOffset; |
| 118 | + public uint Action; |
| 119 | + public uint FileNameLength; |
| 120 | + public fixed char FileName[1]; |
| 121 | + } |
| 122 | + |
| 123 | + [DllImport("api-ms-win-core-file-l2-1-0.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 124 | + public unsafe static extern bool ReadDirectoryChangesW(IntPtr hDirectory, byte* lpBuffer, |
| 125 | + int nBufferLength, bool bWatchSubtree, int dwNotifyFilter, int* |
| 126 | + lpBytesReturned, ref OVERLAPPED lpOverlapped, |
| 127 | + LpoverlappedCompletionRoutine lpCompletionRoutine); |
| 128 | + } |
| 129 | +} |
0 commit comments