diff --git a/PInvoke/Kernel32/FileApi.cs b/PInvoke/Kernel32/FileApi.cs index d3ddd0fe..1b7897ba 100644 --- a/PInvoke/Kernel32/FileApi.cs +++ b/PInvoke/Kernel32/FileApi.cs @@ -338,7 +338,7 @@ namespace Vanara.PInvoke /// An IAsyncResult instance that references the asynchronous request. public static unsafe IAsyncResult BeginReadFile(HFILE hFile, byte[] buffer, uint numberOfBytesToRead, AsyncCallback requestCallback, object stateObject) { - var ar = OverlappedAsync.SetupOverlappedFunction(hFile, requestCallback, stateObject); + OverlappedAsync.OverlappedAsyncResult ar = OverlappedAsync.SetupOverlappedFunction(hFile, requestCallback, stateObject); fixed (byte* pIn = buffer) { var ret = ReadFile(hFile, pIn, numberOfBytesToRead, null, ar.Overlapped); @@ -392,7 +392,7 @@ namespace Vanara.PInvoke /// An IAsyncResult instance that references the asynchronous request. public static unsafe IAsyncResult BeginWriteFile(HFILE hFile, byte[] buffer, uint numberOfBytesToWrite, AsyncCallback requestCallback, object stateObject) { - var ar = OverlappedAsync.SetupOverlappedFunction(hFile, requestCallback, stateObject); + OverlappedAsync.OverlappedAsyncResult ar = OverlappedAsync.SetupOverlappedFunction(hFile, requestCallback, stateObject); fixed (byte* pIn = buffer) { var ret = WriteFile(hFile, pIn, numberOfBytesToWrite, null, ar.Overlapped); @@ -1817,8 +1817,8 @@ namespace Vanara.PInvoke public static IEnumerable EnumFilesEx(string lpFileName, FINDEX_SEARCH_OPS fSearchOp = FINDEX_SEARCH_OPS.FindExSearchNameMatch, FIND_FIRST dwAdditionalFlags = 0, bool excludeShortName = false) { var minWin7 = Environment.OSVersion.Version >= new Version(6, 1); - var lvl = excludeShortName && minWin7 ? FINDEX_INFO_LEVELS.FindExInfoBasic : FINDEX_INFO_LEVELS.FindExInfoStandard; - using (var h = FindFirstFileEx(lpFileName, lvl, out var data, fSearchOp, default, dwAdditionalFlags)) + FINDEX_INFO_LEVELS lvl = excludeShortName && minWin7 ? FINDEX_INFO_LEVELS.FindExInfoBasic : FINDEX_INFO_LEVELS.FindExInfoStandard; + using (SafeSearchHandle h = FindFirstFileEx(lpFileName, lvl, out WIN32_FIND_DATA data, fSearchOp, default, dwAdditionalFlags)) { if (h.IsInvalid) ThrowIfNotNoMore(); yield return data; @@ -1827,7 +1827,7 @@ namespace Vanara.PInvoke ThrowIfNotNoMore(); } - void ThrowIfNotNoMore() { var e = Win32Error.GetLastError(); if (e.Failed && e != Win32Error.ERROR_NO_MORE_FILES) throw e.GetException(); } + static void ThrowIfNotNoMore() { var e = Win32Error.GetLastError(); if (e.Failed && e != Win32Error.ERROR_NO_MORE_FILES) throw e.GetException(); } } /// Retrieves the names of the volumes on a computer using and . @@ -1835,7 +1835,7 @@ namespace Vanara.PInvoke public static IEnumerable EnumVolumes() { var sb = new StringBuilder(MAX_PATH, MAX_PATH); - using (var h = FindFirstVolume(sb, (uint)sb.Capacity)) + using (SafeVolumeSearchHandle h = FindFirstVolume(sb, (uint)sb.Capacity)) { if (h.IsInvalid) ThrowIfNotNoMore(); yield return sb.ToString(); @@ -1844,7 +1844,7 @@ namespace Vanara.PInvoke ThrowIfNotNoMore(); } - void ThrowIfNotNoMore() { var e = Win32Error.GetLastError(); if (e.Failed && e != Win32Error.ERROR_NO_MORE_FILES) throw e.GetException(); } + static void ThrowIfNotNoMore() { var e = Win32Error.GetLastError(); if (e.Failed && e != Win32Error.ERROR_NO_MORE_FILES) throw e.GetException(); } } /// Converts a file time to a local file time. @@ -3167,7 +3167,7 @@ namespace Vanara.PInvoke { var sb1 = new StringBuilder(MAX_PATH + 1); var sb2 = new StringBuilder(MAX_PATH + 1); - var ret = GetVolumeInformation(rootPathName, sb1, sb1.Capacity, out var sn, out var cl, out var flags, sb2, sb2.Capacity); + var ret = GetVolumeInformation(rootPathName, sb1, sb1.Capacity, out var sn, out var cl, out FileSystemFlags flags, sb2, sb2.Capacity); volumeName = sb1.ToString(); volumeSerialNumber = sn; maximumComponentLength = cl; @@ -3721,17 +3721,15 @@ namespace Vanara.PInvoke { deviceName = deviceName?.TrimEnd('\\'); var bytes = 16; - var retLen = 0U; - using (var mem = new SafeHGlobalHandle(0)) + uint retLen; + using var mem = new SafeHGlobalHandle(0); + do { - do - { - mem.Size = (bytes *= 4); - retLen = QueryDosDevice(deviceName, (IntPtr)mem, mem.Size / Marshal.SystemDefaultCharSize); - } while (retLen == 0 && Win32Error.GetLastError() == Win32Error.ERROR_INSUFFICIENT_BUFFER); - if (retLen == 0) throw new Win32Exception(); - return mem.ToStringEnum().ToArray(); - } + mem.Size = bytes *= 4; + retLen = QueryDosDevice(deviceName, mem, mem.Size / Marshal.SystemDefaultCharSize); + } while (retLen == 0 && Win32Error.GetLastError() == Win32Error.ERROR_INSUFFICIENT_BUFFER); + if (retLen == 0) throw new Win32Exception(); + return mem.ToStringEnum().ToArray(); } /// @@ -4218,8 +4216,8 @@ namespace Vanara.PInvoke public static bool SetFileInformationByHandle([In] HFILE hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, T lpFileInformation) where T : struct { if (!CorrespondingTypeAttribute.CanSet(FileInformationClass, typeof(T))) throw new InvalidOperationException("Type mismatch."); - using (var mem = SafeHGlobalHandle.CreateFromStructure(lpFileInformation)) - return SetFileInformationByHandle(hFile, FileInformationClass, mem, (uint)mem.Size); + using var mem = SafeHGlobalHandle.CreateFromStructure(lpFileInformation); + return SetFileInformationByHandle(hFile, FileInformationClass, mem, mem.Size); } /// @@ -5776,7 +5774,7 @@ namespace Vanara.PInvoke } /// Provides a that releases a created HFILE instance at disposal using CloseHandle. - public class SafeHFILE : SafeKernelHandle + public class SafeHFILE : SafeSyncHandle { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. @@ -5793,7 +5791,7 @@ namespace Vanara.PInvoke /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. - public static explicit operator SafeHFILE(Microsoft.Win32.SafeHandles.SafeFileHandle h) => new SafeHFILE(h.DangerousGetHandle(), false); + public static explicit operator SafeHFILE(Microsoft.Win32.SafeHandles.SafeFileHandle h) => new(h.DangerousGetHandle(), false); /// Performs an implicit conversion from to . /// The safe handle instance. diff --git a/PInvoke/Shared/Handles.cs b/PInvoke/Shared/Handles.cs index 91be2306..1454f7fd 100644 --- a/PInvoke/Shared/Handles.cs +++ b/PInvoke/Shared/Handles.cs @@ -724,7 +724,7 @@ namespace Vanara.PInvoke /// Provides a handle to a file. [StructLayout(LayoutKind.Sequential), DebuggerDisplay("{handle}")] - public struct HFILE : IKernelHandle + public struct HFILE : ISyncHandle { /// Represents an invalid handle. public static readonly HFILE INVALID_HANDLE_VALUE = new IntPtr(-1);