Skip to content

Commit bbbc381

Browse files
committed
Added Obsolete to some win32 helpers
1 parent acb2e07 commit bbbc381

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,6 @@ IContextMenu2
235235
GetSubMenu
236236
GetMenuItemCount
237237
GetMenuItemInfo
238+
LoadString
239+
LoadLibrary
240+
FreeLibrary

src/Files.App.Storage/Storables/WindowsStorage/WindowsStorageHelpers.Icon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public unsafe static HRESULT TryGetThumbnail(this IWindowsStorable storable, int
102102
return HRESULT.S_OK;
103103
}
104104

105-
public unsafe static HRESULT TryExtractImageFromDll(this IWindowsStorable storable, int size, int index, out byte[]? imageData)
105+
public unsafe static HRESULT TryExtractImageFromExecutable(this IWindowsStorable storable, int size, int index, out byte[]? imageData)
106106
{
107107
DllIconCache ??= [];
108108
imageData = null;

src/Files.App.Storage/Storables/WindowsStorage/WindowsStorageHelpers.Shell.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,6 @@ public static bool InvokeShellNewItem(this IWindowsStorable storable, ShellNewIt
230230

231231
HRESULT hr = default;
232232

233-
CMINVOKECOMMANDINFO cmici = default;
234-
cmici.cbSize = (uint)sizeof(CMINVOKECOMMANDINFO);
235-
cmici.lpVerb = (PCSTR)(byte*)item.Id;
236-
cmici.nShow = (int)SHOW_WINDOW_CMD.SW_SHOWNORMAL;
237-
238233
if (folder.NewMenuPtr is null)
239234
{
240235
IContextMenu* pNewMenu = default;
@@ -246,7 +241,14 @@ public static bool InvokeShellNewItem(this IWindowsStorable storable, ShellNewIt
246241
folder.NewMenuPtr = pNewMenu;
247242
}
248243

249-
folder.NewMenuPtr->InvokeCommand(&cmici);
244+
CMINVOKECOMMANDINFO cmici = default;
245+
cmici.cbSize = (uint)sizeof(CMINVOKECOMMANDINFO);
246+
cmici.lpVerb = (PCSTR)(byte*)item.Id;
247+
cmici.nShow = (int)SHOW_WINDOW_CMD.SW_SHOWNORMAL;
248+
249+
hr = folder.NewMenuPtr->InvokeCommand(&cmici);
250+
if (hr.ThrowIfFailedOnDebug().Failed)
251+
return false;
250252

251253
return false;
252254
}

src/Files.App.Storage/Storables/WindowsStorage/WindowsStorageHelpers.Storage.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using System.Runtime.InteropServices;
45
using Windows.Win32;
56
using Windows.Win32.Foundation;
67
using Windows.Win32.Storage.FileSystem;
@@ -84,5 +85,37 @@ public static bool TryShowFormatDriveDialog(HWND hWnd, uint driveLetterIndex, SH
8485
var result = PInvoke.SHFormatDrive(hWnd, driveLetterIndex, id, options);
8586
return result is 0xFFFF;
8687
}
88+
89+
public static bool TryExtractStringFromExecutable(string szExecutablePath, uint dwIndex, out string extractedString)
90+
{
91+
extractedString = string.Empty;
92+
93+
HINSTANCE hInst = default;
94+
95+
fixed (char* pszExecutablePath = szExecutablePath)
96+
{
97+
hInst = PInvoke.LoadLibrary(pszExecutablePath);
98+
if (hInst.IsNull)
99+
return false;
100+
}
101+
102+
int cchExtractedString = 1024;
103+
char* pszExtractedString = (char*)NativeMemory.Alloc((nuint)cchExtractedString);
104+
if (pszExtractedString is null)
105+
{
106+
if (!hInst.IsNull) PInvoke.FreeLibrary(hInst);
107+
return false;
108+
}
109+
110+
cchExtractedString = PInvoke.LoadString(hInst, dwIndex, pszExtractedString, cchExtractedString);
111+
if (cchExtractedString is 0)
112+
{
113+
if (!hInst.IsNull) PInvoke.FreeLibrary(hInst);
114+
if (pszExtractedString is not null) NativeMemory.Free(pszExtractedString);
115+
return false;
116+
}
117+
118+
return true;
119+
}
87120
}
88121
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
using Vanara.PInvoke;
1515
using Windows.System;
1616
using Windows.Win32;
17-
using Windows.Win32.Foundation;
1817
using Windows.Win32.Storage.FileSystem;
19-
using static Vanara.PInvoke.Kernel32;
2018
using COMPRESSION_FORMAT = Windows.Win32.Storage.FileSystem.COMPRESSION_FORMAT;
2119
using HRESULT = Vanara.PInvoke.HRESULT;
2220
using HWND = Vanara.PInvoke.HWND;
@@ -28,6 +26,7 @@ namespace Files.App.Helpers
2826
/// </summary>
2927
public static partial class Win32Helper
3028
{
29+
[Obsolete($"Use {nameof(STATask)} instead.")]
3130
public static Task StartSTATask(Func<Task> func)
3231
{
3332
var taskCompletionSource = new TaskCompletionSource();
@@ -62,6 +61,7 @@ public static Task StartSTATask(Func<Task> func)
6261
return taskCompletionSource.Task;
6362
}
6463

64+
[Obsolete($"Use {nameof(STATask)} instead.")]
6565
public static Task StartSTATask(Action action)
6666
{
6767
var taskCompletionSource = new TaskCompletionSource();
@@ -96,6 +96,7 @@ public static Task StartSTATask(Action action)
9696
return taskCompletionSource.Task;
9797
}
9898

99+
[Obsolete($"Use {nameof(STATask)} instead.")]
99100
public static Task<T?> StartSTATask<T>(Func<T> func)
100101
{
101102
var taskCompletionSource = new TaskCompletionSource<T?>();
@@ -131,6 +132,7 @@ public static Task StartSTATask(Action action)
131132
return taskCompletionSource.Task;
132133
}
133134

135+
[Obsolete($"Use {nameof(STATask)} instead.")]
134136
public static Task<T?> StartSTATask<T>(Func<Task<T>> func)
135137
{
136138
var taskCompletionSource = new TaskCompletionSource<T?>();
@@ -189,6 +191,7 @@ public static Task StartSTATask(Action action)
189191
return await GetUwpAssoc() ?? GetDesktopAssoc();
190192
}
191193

194+
[Obsolete($"Use {nameof(WindowsStorageHelpers.TryExtractStringFromExecutable)} instead.")]
192195
public static string ExtractStringFromDLL(string file, int number)
193196
{
194197
var lib = Kernel32.LoadLibrary(file);
@@ -228,12 +231,6 @@ public static string ExtractStringFromDLL(string file, int number)
228231

229232
private static readonly object _iconOverlayLock = new object();
230233

231-
/// <summary>
232-
/// Returns overlay for given file or folder
233-
/// </summary>
234-
/// <param name="path"></param>
235-
/// <param name="isDirectory"></param>
236-
/// <returns></returns>
237234
public static byte[]? GetIconOverlay(string path, bool isDirectory)
238235
{
239236
var shFileInfo = new Shell32.SHFILEINFO();
@@ -287,16 +284,7 @@ public static string ExtractStringFromDLL(string file, int number)
287284
/// <summary>
288285
/// Returns an icon if returnIconOnly is true, otherwise a thumbnail will be returned if available.
289286
/// </summary>
290-
/// <param name="path"></param>
291-
/// <param name="size"></param>
292-
/// <param name="isFolder"></param>
293-
/// <param name="iconOptions"></param>
294-
/// <returns></returns>
295-
public static byte[]? GetIcon(
296-
string path,
297-
int size,
298-
bool isFolder,
299-
IconOptions iconOptions)
287+
public static byte[]? GetIcon(string path, int size, bool isFolder, IconOptions iconOptions)
300288
{
301289
byte[]? iconData = null;
302290

@@ -454,6 +442,7 @@ public static bool RunPowershellCommand(string command, PowerShellExecutionOptio
454442

455443
private static readonly ConcurrentDictionary<(string File, int Index, int Size), IconFileInfo> _iconCache = new();
456444

445+
[Obsolete($"Use {nameof(WindowsStorageHelpers.TryExtractImageFromExecutable)} instead.")]
457446
public static IList<IconFileInfo> ExtractSelectedIconsFromDLL(string file, IList<int> indexes, int iconSize = 48)
458447
{
459448
var iconsList = new List<IconFileInfo>();
@@ -483,6 +472,7 @@ public static IList<IconFileInfo> ExtractSelectedIconsFromDLL(string file, IList
483472
return iconsList;
484473
}
485474

475+
[Obsolete($"Use {nameof(WindowsStorageHelpers.TryExtractImageFromExecutable)} instead.")]
486476
public static IList<IconFileInfo>? ExtractIconsFromDLL(string file)
487477
{
488478
var iconsList = new List<IconFileInfo>();
@@ -517,6 +507,7 @@ public static IList<IconFileInfo> ExtractSelectedIconsFromDLL(string file, IList
517507
return iconsList;
518508
}
519509

510+
[Obsolete($"Use {nameof(WindowsStorageHelpers.TrySetFolderIcon)} instead.")]
520511
public static bool SetCustomDirectoryIcon(string? folderPath, string? iconFile, int iconIndex = 0)
521512
{
522513
if (folderPath is null)
@@ -537,6 +528,7 @@ public static bool SetCustomDirectoryIcon(string? folderPath, string? iconFile,
537528
return success;
538529
}
539530

531+
[Obsolete($"Use {nameof(WindowsStorageHelpers.TrySetShortcutIcon)} instead.")]
540532
public static bool SetCustomFileIcon(string? filePath, string? iconFile, int iconIndex = 0)
541533
{
542534
if (filePath is null)
@@ -547,6 +539,7 @@ public static bool SetCustomFileIcon(string? filePath, string? iconFile, int ico
547539
return success;
548540
}
549541

542+
[Obsolete($"Use {nameof(WindowsStorageHelpers.TryShowFormatDriveDialog)} instead.")]
550543
public static Task OpenFormatDriveDialog(string drive)
551544
{
552545
// Format requires elevation
@@ -571,6 +564,7 @@ public static Task<bool> MountVhdDisk(string vhdPath)
571564
return RunPowershellCommandAsync($"-command \"Mount-DiskImage -ImagePath '{vhdPath}'\"", PowerShellExecutionOptions.Elevated | PowerShellExecutionOptions.Hidden);
572565
}
573566

567+
[Obsolete($"Don't use this method any more.")]
574568
public static Bitmap? GetBitmapFromHBitmap(HBITMAP hBitmap)
575569
{
576570
try

0 commit comments

Comments
 (0)