Skip to content

Commit d963427

Browse files
authored
Code Quality: Removed Vanara from some classes (#16235)
1 parent d6fd89c commit d963427

File tree

9 files changed

+96
-102
lines changed

9 files changed

+96
-102
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ PSGetPropertyKeyFromName
135135
ShellExecuteEx
136136
CoTaskMemFree
137137
QueryDosDevice
138+
FindFirstFileEx
139+
FindNextFile
140+
CreateFile
141+
GetFileSizeEx
142+
WIN32_FIND_DATAW
143+
FILE_ACCESS_RIGHTS
138144
SHAddToRecentDocs
139145
SHARD
140146
BHID_EnumItems

src/Files.App/Data/Items/ShellLibraryItem.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.IO;
5-
64
namespace Files.App.Data.Items
75
{
86
public sealed class ShellLibraryItem
97
{
108
public const string EXTENSION = ".library-ms";
119

12-
public static readonly string LibrariesPath = Win32PInvoke.GetFolderFromKnownFolderGUID(new Guid("1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE"));
10+
public static readonly string LibrariesPath = Win32Helper.GetFolderFromKnownFolderGUID(new Guid("1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE"));
1311

1412
/// <summary>
1513
/// Full path of library file.<br/>

src/Files.App/Helpers/Win32/Win32Helper.Shell.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,15 @@ public static partial class Win32Helper
7979
return (folder, flc);
8080
});
8181
}
82+
83+
public static string GetFolderFromKnownFolderGUID(Guid guid)
84+
{
85+
nint pszPath;
86+
Win32PInvoke.SHGetKnownFolderPath(guid, 0, nint.Zero, out pszPath);
87+
string path = Marshal.PtrToStringUni(pszPath);
88+
Marshal.FreeCoTaskMem(pszPath);
89+
90+
return path;
91+
}
8292
}
8393
}

src/Files.App/Helpers/Win32/Win32Helper.WindowManagement.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
using Microsoft.UI.Input;
55
using Microsoft.UI.Xaml;
66
using System.Reflection;
7-
using Vanara.PInvoke;
87
using Windows.Win32;
98
using Windows.Win32.UI.WindowsAndMessaging;
10-
using static Vanara.PInvoke.User32;
119

1210
namespace Files.App.Helpers
1311
{
@@ -60,20 +58,5 @@ public static void ChangeCursor(this UIElement uiElement, InputCursor cursor)
6058
[cursor]
6159
);
6260
}
63-
64-
/// <summary>
65-
/// Changes an attribute of the specified window.
66-
/// </summary>
67-
/// <param name="hWnd">A handle to the window and, indirectly, the class to which the window belongs.</param>
68-
/// <param name="nIndex">The zero-based offset to the value to be set.</param>
69-
/// <param name="dwNewLong">The replacement value.</param>
70-
/// <returns>If the function succeeds, the return value is the previous value of the specified offset.</returns>
71-
public static IntPtr SetWindowLong(HWND hWnd, WindowLongFlags nIndex, IntPtr dwNewLong)
72-
{
73-
return
74-
IntPtr.Size == 4
75-
? Win32PInvoke.SetWindowLongPtr32(hWnd, nIndex, dwNewLong)
76-
: Win32PInvoke.SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
77-
}
7861
}
7962
}

src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System.Runtime.InteropServices;
66
using System.Runtime.InteropServices.ComTypes;
77
using System.Text;
8-
using Vanara.PInvoke;
9-
using static Vanara.PInvoke.User32;
8+
using Windows.Win32.Foundation;
9+
using Windows.Win32.System.Com;
1010

1111
namespace Files.App.Helpers
1212
{
@@ -90,20 +90,6 @@ public static extern uint CoWaitForMultipleObjects(
9090
out uint dwIndex
9191
);
9292

93-
[DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLong")]
94-
public static extern int SetWindowLongPtr32(
95-
HWND hWnd,
96-
WindowLongFlags nIndex,
97-
IntPtr dwNewLong
98-
);
99-
100-
[DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLongPtr")]
101-
public static extern IntPtr SetWindowLongPtr64(
102-
HWND hWnd,
103-
WindowLongFlags nIndex,
104-
IntPtr dwNewLong
105-
);
106-
10793
[DllImport("shell32.dll")]
10894
public static extern IntPtr SHBrowseForFolder(
10995
ref BROWSEINFO lpbi
@@ -498,7 +484,7 @@ int dwFlags
498484
);
499485

500486
[DllImport("shell32.dll")]
501-
static extern int SHGetKnownFolderPath(
487+
public static extern int SHGetKnownFolderPath(
502488
[MarshalAs(UnmanagedType.LPStruct)] Guid rfid,
503489
uint dwFlags,
504490
IntPtr hToken,
@@ -507,14 +493,5 @@ out IntPtr pszPath
507493

508494
[DllImport("shell32.dll", EntryPoint = "SHUpdateRecycleBinIcon", CharSet = CharSet.Unicode, SetLastError = true)]
509495
public static extern void SHUpdateRecycleBinIcon();
510-
511-
public static string GetFolderFromKnownFolderGUID(Guid guid)
512-
{
513-
IntPtr pPath;
514-
SHGetKnownFolderPath(guid, 0, IntPtr.Zero, out pPath);
515-
string path = Marshal.PtrToStringUni(pPath);
516-
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
517-
return path;
518-
}
519496
}
520497
}

src/Files.App/Utils/Shell/ItemStreamHelper.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System;
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
24
using System.Runtime.InteropServices;
3-
using Vanara.PInvoke;
5+
using Windows.Win32.System.Com;
46

57
namespace Files.App.Utils.Shell
68
{
@@ -20,12 +22,15 @@ public static IntPtr IShellItemFromPath(string path)
2022

2123
public static IntPtr IStreamFromPath(string path)
2224
{
23-
IntPtr pstm;
24-
var hr = Win32PInvoke.SHCreateStreamOnFileEx(path,
25+
var hr = Win32PInvoke.SHCreateStreamOnFileEx(
26+
path,
2527
STGM.STGM_READ | STGM.STGM_FAILIFTHERE | STGM.STGM_SHARE_DENY_NONE,
26-
0, 0, IntPtr.Zero, out pstm);
28+
0, 0,
29+
IntPtr.Zero, out var pstm);
30+
2731
if ((int)hr < 0)
2832
return IntPtr.Zero;
33+
2934
return pstm;
3035
}
3136

src/Files.App/Utils/Shell/PreviewHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ void SetupHandler(Guid clsid)
160160
// If we use Activator.CreateInstance(Type.GetTypeFromCLSID(...)),
161161
// CLR will allow in-process server, which defeats isolation and
162162
// creates strange bugs.
163-
HRESULT hr = Win32PInvoke.CoCreateInstance(ref clsid, IntPtr.Zero, Win32PInvoke.ClassContext.LocalServer, ref iid, out pph);
163+
Windows.Win32.Foundation.HRESULT hr2 = Win32PInvoke.CoCreateInstance(ref clsid, IntPtr.Zero, Win32PInvoke.ClassContext.LocalServer, ref iid, out pph);
164+
HRESULT hr = new(hr2.Value);
164165
// See https://blogs.msdn.microsoft.com/adioltean/2005/06/24/when-cocreateinstance-returns-0x80080005-co_e_server_exec_failure/
165166
// CO_E_SERVER_EXEC_FAILURE also tends to happen when debugging in Visual Studio.
166167
// Moreover, to create the instance in a server at low integrity level, we need

src/Files.App/Utils/Storage/Operations/FileSizeCalculator.cs

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Collections.Concurrent;
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using System.Collections.Concurrent;
25
using System.IO;
3-
using Vanara.PInvoke;
4-
using static Vanara.PInvoke.Kernel32;
6+
using Windows.Win32;
7+
using Windows.Win32.Storage.FileSystem;
58

69
namespace Files.App.Utils.Storage.Operations
710
{
@@ -22,7 +25,18 @@ public FileSizeCalculator(params string[] paths)
2225

2326
public async Task ComputeSizeAsync(CancellationToken cancellationToken = default)
2427
{
25-
await Parallel.ForEachAsync(_paths, cancellationToken, async (path, token) => await Task.Factory.StartNew(() =>
28+
await Parallel.ForEachAsync(
29+
_paths,
30+
cancellationToken,
31+
async (path, token) => await Task.Factory.StartNew(() =>
32+
{
33+
ComputeSizeRecursively(path, token);
34+
},
35+
token,
36+
TaskCreationOptions.LongRunning,
37+
TaskScheduler.Default));
38+
39+
unsafe void ComputeSizeRecursively(string path, CancellationToken token)
2640
{
2741
var queue = new Queue<string>();
2842
if (!Win32Helper.HasFileAttribute(path, FileAttributes.Directory))
@@ -35,76 +49,78 @@ await Parallel.ForEachAsync(_paths, cancellationToken, async (path, token) => aw
3549

3650
while (queue.TryDequeue(out var directory))
3751
{
38-
using var hFile = FindFirstFileEx(
39-
directory + "\\*.*",
40-
FINDEX_INFO_LEVELS.FindExInfoBasic,
41-
out WIN32_FIND_DATA findData,
42-
FINDEX_SEARCH_OPS.FindExSearchNameMatch,
43-
IntPtr.Zero,
44-
FIND_FIRST.FIND_FIRST_EX_LARGE_FETCH);
45-
46-
if (!hFile.IsInvalid)
52+
WIN32_FIND_DATAW findData = default;
53+
54+
fixed (char* pszFilePath = directory + "\\*.*")
4755
{
48-
do
56+
var hFile = PInvoke.FindFirstFileEx(
57+
pszFilePath,
58+
FINDEX_INFO_LEVELS.FindExInfoBasic,
59+
&findData,
60+
FINDEX_SEARCH_OPS.FindExSearchNameMatch,
61+
null,
62+
FIND_FIRST_EX_FLAGS.FIND_FIRST_EX_LARGE_FETCH);
63+
64+
if (!hFile.IsNull)
4965
{
50-
if ((findData.dwFileAttributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
51-
// Skip symbolic links and junctions
52-
continue;
66+
do
67+
{
68+
FILE_FLAGS_AND_ATTRIBUTES attributes = (FILE_FLAGS_AND_ATTRIBUTES)findData.dwFileAttributes;
5369

54-
var itemPath = Path.Combine(directory, findData.cFileName);
70+
if (attributes.HasFlag(FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_REPARSE_POINT))
71+
// Skip symbolic links and junctions
72+
continue;
5573

56-
if ((findData.dwFileAttributes & FileAttributes.Directory) != FileAttributes.Directory)
57-
{
58-
ComputeFileSize(itemPath);
59-
}
60-
else if (findData.cFileName != "." && findData.cFileName != "..")
61-
{
62-
queue.Enqueue(itemPath);
63-
}
74+
var itemPath = Path.Combine(directory, findData.cFileName.ToString());
75+
76+
if (attributes.HasFlag(FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_DIRECTORY))
77+
{
78+
ComputeFileSize(itemPath);
79+
}
80+
else if (findData.cFileName.ToString() is string fileName &&
81+
fileName.Equals(".", StringComparison.OrdinalIgnoreCase) &&
82+
fileName.Equals("..", StringComparison.OrdinalIgnoreCase))
83+
{
84+
queue.Enqueue(itemPath);
85+
}
6486

65-
if (token.IsCancellationRequested)
66-
break;
87+
if (token.IsCancellationRequested)
88+
break;
89+
}
90+
while (PInvoke.FindNextFile(hFile, &findData));
6791
}
68-
while (FindNextFile(hFile, out findData));
92+
93+
PInvoke.CloseHandle(hFile);
6994
}
7095
}
7196
}
72-
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default));
97+
}
7398
}
7499

75100
private long ComputeFileSize(string path)
76101
{
77102
if (_computedFiles.TryGetValue(path, out var size))
78-
{
79103
return size;
80-
}
81104

82-
using var hFile = CreateFile(
105+
using var hFile = PInvoke.CreateFile(
83106
path,
84-
Kernel32.FileAccess.FILE_READ_ATTRIBUTES,
85-
FileShare.Read,
107+
(uint)FILE_ACCESS_RIGHTS.FILE_READ_ATTRIBUTES,
108+
FILE_SHARE_MODE.FILE_SHARE_READ,
86109
null,
87-
FileMode.Open,
110+
FILE_CREATION_DISPOSITION.OPEN_EXISTING,
88111
0,
89112
null);
90113

91-
if (!hFile.IsInvalid)
92-
{
93-
if (GetFileSizeEx(hFile, out size) && _computedFiles.TryAdd(path, size))
94-
{
95-
Interlocked.Add(ref _size, size);
96-
}
97-
}
114+
if (!hFile.IsInvalid && PInvoke.GetFileSizeEx(hFile, out size) && _computedFiles.TryAdd(path, size))
115+
Interlocked.Add(ref _size, size);
98116

99117
return size;
100118
}
101119

102120
public void ForceComputeFileSize(string path)
103121
{
104122
if (!Win32Helper.HasFileAttribute(path, FileAttributes.Directory))
105-
{
106123
ComputeFileSize(path);
107-
}
108124
}
109125

110126
public bool TryGetComputedFileSize(string path, out long size)

src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Windows.Win32.Graphics.DirectComposition;
1616
using Windows.Win32.Graphics.Dwm;
1717
using Windows.Win32.Graphics.Dxgi;
18+
using Windows.Win32.UI.WindowsAndMessaging;
1819
using WinRT;
1920
using static Vanara.PInvoke.ShlwApi;
2021
using static Vanara.PInvoke.User32;
@@ -245,14 +246,11 @@ public unsafe void PointerEntered(bool onPreview)
245246
(uint)Marshal.SizeOf(dwAttrib));
246247

247248
if (isOfficePreview)
248-
Win32Helper.SetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE, 0);
249+
PInvoke.SetWindowLongPtr(new((nint)hwnd), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, 0);
249250
}
250251
else
251252
{
252-
Win32Helper.SetWindowLong(
253-
hwnd,
254-
WindowLongFlags.GWL_EXSTYLE,
255-
(nint)(WindowStylesEx.WS_EX_LAYERED | WindowStylesEx.WS_EX_COMPOSITED));
253+
PInvoke.SetWindowLongPtr(new((nint)hwnd), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (nint)(WINDOW_EX_STYLE.WS_EX_LAYERED | WINDOW_EX_STYLE.WS_EX_COMPOSITED));
256254

257255
var dwAttrib = Convert.ToUInt32(true);
258256

0 commit comments

Comments
 (0)