Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Replaced stack-allocated GUIDs with RVA-inlined GUIDs #16993

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.8.2" />
<PackageVersion Include="MSTest.TestFramework" Version="3.8.2" />
<PackageVersion Include="Dongle.GuidRVAGen" Version="1.0.4" />
</ItemGroup>
</Project>
5 changes: 2 additions & 3 deletions src/Files.App.CsWin32/ComPtr`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public readonly ComPtr<U> As<U>() where U : unmanaged, IComIID
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly HRESULT CoCreateInstance<U>(CLSCTX dwClsContext = CLSCTX.CLSCTX_LOCAL_SERVER)
public readonly HRESULT CoCreateInstance(Guid* rclsid, IUnknown* pUnkOuter = null, CLSCTX dwClsContext = CLSCTX.CLSCTX_LOCAL_SERVER)
{
Guid iid = typeof(T).GUID, clsid = typeof(U).GUID;
return PInvoke.CoCreateInstance(&clsid, null, dwClsContext, &iid, (void**)this.GetAddressOf());
return PInvoke.CoCreateInstance(rclsid, pUnkOuter, dwClsContext, (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in T.Guid)), (void**)this.GetAddressOf());
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
1 change: 1 addition & 0 deletions src/Files.App.CsWin32/Files.App.CsWin32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" PrivateAssets="all" />
<PackageReference Include="Dongle.GuidRVAGen" />
</ItemGroup>

</Project>
24 changes: 5 additions & 19 deletions src/Files.App.CsWin32/IStorageProviderQuotaUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

namespace Windows.Win32.System.WinRT
{
public unsafe struct IStorageProviderQuotaUI : IComIID
public unsafe partial struct IStorageProviderQuotaUI : IComIID
{
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
private void** lpVtbl;
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public HRESULT GetQuotaTotalInBytes(ulong* value)
Expand All @@ -25,23 +27,7 @@ public HRESULT GetQuotaUsedInBytes(ulong* value)
return ((delegate* unmanaged[Stdcall]<IStorageProviderQuotaUI*, ulong*, HRESULT>)(lpVtbl[8]))((IStorageProviderQuotaUI*)Unsafe.AsPointer(ref this), value);
}

public static ref readonly Guid Guid
{
get
{
// BA6295C3-312E-544F-9FD5-1F81B21F3649
ReadOnlySpan<byte> data =
[
0xC3, 0x95, 0x62, 0xBA,
0x2E, 0x31,
0x4F, 0x54,
0x9F, 0xD5,
0x1F, 0x81, 0xB2, 0x1F, 0x36, 0x49
];

Debug.Assert(data.Length == sizeof(Guid));
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
[GuidRVAGen.Guid("BA6295C3-312E-544F-9FD5-1F81B21F3649")]
public static partial ref readonly Guid Guid { get; }
}
}
24 changes: 5 additions & 19 deletions src/Files.App.CsWin32/IStorageProviderStatusUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,19 @@

namespace Windows.Win32.System.WinRT
{
public unsafe struct IStorageProviderStatusUI : IComIID
public unsafe partial struct IStorageProviderStatusUI : IComIID
{
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
private void** lpVtbl;
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public HRESULT GetQuotaUI(IStorageProviderQuotaUI** result)
{
return ((delegate* unmanaged[Stdcall]<IStorageProviderStatusUI*, IStorageProviderQuotaUI**, HRESULT>)lpVtbl[14])((IStorageProviderStatusUI*)Unsafe.AsPointer(ref this), result);
}

public static ref readonly Guid Guid
{
get
{
// d6b6a758-198d-5b80-977f-5ff73da33118
ReadOnlySpan<byte> data =
[
0x58, 0xa7, 0xb6, 0xd6,
0x8d, 0x19,
0x80, 0x5b,
0x97, 0x7f,
0x5f, 0xf7, 0x3d, 0xa3, 0x31, 0x18
];

Debug.Assert(data.Length == sizeof(Guid));
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
[GuidRVAGen.Guid("D6B6A758-198D-5B80-977F-5FF73DA33118")]
public static partial ref readonly Guid Guid { get; }
}
}
24 changes: 5 additions & 19 deletions src/Files.App.CsWin32/IStorageProviderStatusUISource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,19 @@

namespace Windows.Win32.System.WinRT
{
public unsafe struct IStorageProviderStatusUISource : IComIID
public unsafe partial struct IStorageProviderStatusUISource : IComIID
{
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
private void** lpVtbl;
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public HRESULT GetStatusUI(IStorageProviderStatusUI** result)
{
return ((delegate* unmanaged[Stdcall]<IStorageProviderStatusUISource*, IStorageProviderStatusUI**, HRESULT>)lpVtbl[6])((IStorageProviderStatusUISource*)Unsafe.AsPointer(ref this), result);
}

public static ref readonly Guid Guid
{
get
{
// A306C249-3D66-5E70-9007-E43DF96051FF
ReadOnlySpan<byte> data =
[
0x49, 0xc2, 0x06, 0xa3,
0x66, 0x3d,
0x70, 0x5e,
0x90, 0x07,
0xe4, 0x3d, 0xf9, 0x60, 0x51, 0xff
];

Debug.Assert(data.Length == sizeof(Guid));
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
[GuidRVAGen.Guid("A306C249-3D66-5E70-9007-E43DF96051FF")]
public static partial ref readonly Guid Guid { get; }
}
}
24 changes: 5 additions & 19 deletions src/Files.App.CsWin32/IStorageProviderStatusUISourceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,19 @@

namespace Windows.Win32.System.WinRT
{
public unsafe struct IStorageProviderStatusUISourceFactory : IComIID
public unsafe partial struct IStorageProviderStatusUISourceFactory : IComIID
{
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
private void** lpVtbl;
#pragma warning restore CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public HRESULT GetStatusUISource(nint syncRootId, IStorageProviderStatusUISource** result)
{
return ((delegate* unmanaged[Stdcall]<IStorageProviderStatusUISourceFactory*, nint, IStorageProviderStatusUISource**, HRESULT>)lpVtbl[6])((IStorageProviderStatusUISourceFactory*)Unsafe.AsPointer(ref this), syncRootId, result);
}

public static ref readonly Guid Guid
{
get
{
// 12E46B74-4E5A-58D1-A62F-0376E8EE7DD8
ReadOnlySpan<byte> data =
[
0x74, 0x6b, 0xe4, 0x12,
0x5a, 0x4e,
0xd1, 0x58,
0xa6, 0x2f,
0x03, 0x76, 0xe8, 0xee, 0x7d, 0xd8
];

Debug.Assert(data.Length == sizeof(Guid));
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
[GuidRVAGen.Guid("12E46B74-4E5A-58D1-A62F-0376E8EE7DD8")]
public static partial ref readonly Guid Guid { get; }
}
}
75 changes: 75 additions & 0 deletions src/Files.App.CsWin32/ManualGuid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using System;
using System.Runtime.CompilerServices;
using Windows.Win32.System.WinRT;

namespace Windows.Win32
{
public static unsafe partial class IID
{
public static Guid* IID_IStorageProviderStatusUISourceFactory
=> (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IStorageProviderStatusUISourceFactory.Guid));

[GuidRVAGen.Guid("000214E4-0000-0000-C000-000000000046")]
public static partial Guid* IID_IContextMenu { get; }

[GuidRVAGen.Guid("70629033-E363-4A28-A567-0DB78006E6D7")]
public static partial Guid* IID_IEnumShellItems { get; }

[GuidRVAGen.Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE")]
public static partial Guid* IID_IShellItem { get; }

[GuidRVAGen.Guid("7E9FB0D3-919F-4307-AB2E-9B1860310C93")]
public static partial Guid* IID_IShellItem2 { get; }

[GuidRVAGen.Guid("947AAB5F-0A5C-4C13-B4D6-4BF7836FC9F8")]
public static partial Guid* IID_IFileOperation { get; }

[GuidRVAGen.Guid("D57C7288-D4AD-4768-BE02-9D969532D960")]
public static partial Guid* IID_IFileOpenDialog { get; }

[GuidRVAGen.Guid("84BCCD23-5FDE-4CDB-AEA4-AF64B83D78AB")]
public static partial Guid* IID_IFileSaveDialog { get; }

[GuidRVAGen.Guid("B92B56A9-8B55-4E14-9A89-0199BBB6F93B")]
public static partial Guid* IID_IDesktopWallpaper { get; }

[GuidRVAGen.Guid("2E941141-7F97-4756-BA1D-9DECDE894A3D")]
public static partial Guid* IID_IApplicationActivationManager { get; }
}

public static unsafe partial class CLSID
{
[GuidRVAGen.Guid("3AD05575-8857-4850-9277-11B85BDB8E09")]
public static partial Guid* CLSID_FileOperation { get; }

[GuidRVAGen.Guid("DC1C5A9C-E88A-4DDE-A5A1-60F82A20AEF7")]
public static partial Guid* CLSID_FileOpenDialog { get; }

[GuidRVAGen.Guid("C0B4E2F3-BA21-4773-8DBA-335EC946EB8B")]
public static partial Guid* CLSID_FileSaveDialog { get; }

[GuidRVAGen.Guid("C2CF3110-460E-4FC1-B9D0-8A1C0C9CC4BD")]
public static partial Guid* CLSID_DesktopWallpaper { get; }

[GuidRVAGen.Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]
public static partial Guid* CLSID_ApplicationActivationManager { get; }
}

public static unsafe partial class BHID
{
[GuidRVAGen.Guid("3981E225-F559-11D3-8E3A-00C04F6837D5")]
public static partial Guid* BHID_SFUIObject { get; }

[GuidRVAGen.Guid("94F60519-2850-4924-AA5A-D15E84868039")]
public static partial Guid* BHID_EnumItems { get; }
}

public static unsafe partial class FOLDERID
{
[GuidRVAGen.Guid("B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC")]
public static partial Guid* FOLDERID_RecycleBinFolder { get; }
}
}
15 changes: 4 additions & 11 deletions src/Files.App/Services/Storage/StorageTrashBinService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,15 @@ private unsafe bool RestoreAllTrashesInternal()
{
// Get IShellItem for Recycle Bin folder
using ComPtr<IShellItem> pRecycleBinFolderShellItem = default;
var recycleBinFolderId = PInvoke.FOLDERID_RecycleBinFolder;
var shellItemGuid = typeof(IShellItem).GUID;
HRESULT hr = PInvoke.SHGetKnownFolderItem(&recycleBinFolderId, KNOWN_FOLDER_FLAG.KF_FLAG_DEFAULT, HANDLE.Null, &shellItemGuid, (void**)pRecycleBinFolderShellItem.GetAddressOf());
HRESULT hr = PInvoke.SHGetKnownFolderItem(FOLDERID.FOLDERID_RecycleBinFolder, KNOWN_FOLDER_FLAG.KF_FLAG_DEFAULT, HANDLE.Null, IID.IID_IShellItem, (void**)pRecycleBinFolderShellItem.GetAddressOf());

// Get IEnumShellItems for Recycle Bin folder
using ComPtr<IEnumShellItems> pEnumShellItems = default;
Guid enumShellItemGuid = typeof(IEnumShellItems).GUID;
var enumItemsBHID = PInvoke.BHID_EnumItems;
hr = pRecycleBinFolderShellItem.Get()->BindToHandler(null, &enumItemsBHID, &enumShellItemGuid, (void**)pEnumShellItems.GetAddressOf());
hr = pRecycleBinFolderShellItem.Get()->BindToHandler(null, BHID.BHID_EnumItems, IID.IID_IEnumShellItems, (void**)pEnumShellItems.GetAddressOf());

// Initialize how to perform the operation
using ComPtr<IFileOperation> pFileOperation = default;
var fileOperationIid = typeof(IFileOperation).GUID;
var fileOperationInstanceIid = typeof(FileOperation).GUID;
hr = PInvoke.CoCreateInstance(&fileOperationInstanceIid, null, CLSCTX.CLSCTX_LOCAL_SERVER, &fileOperationIid, (void**)pFileOperation.GetAddressOf());
hr = PInvoke.CoCreateInstance(CLSID.CLSID_FileOperation, null, CLSCTX.CLSCTX_LOCAL_SERVER, IID.IID_IFileOperation, (void**)pFileOperation.GetAddressOf());
hr = pFileOperation.Get()->SetOperationFlags(FILEOPERATION_FLAGS.FOF_NO_UI);
hr = pFileOperation.Get()->SetOwnerWindow(new(MainWindow.Instance.WindowHandle));

Expand All @@ -125,8 +119,7 @@ private unsafe bool RestoreAllTrashesInternal()
{
// Get the original path
using ComPtr<IShellItem2> pShellItem2 = default;
var shellItem2Iid = typeof(IShellItem2).GUID;
hr = pShellItem.Get()->QueryInterface(&shellItem2Iid, (void**)pShellItem2.GetAddressOf());
hr = pShellItem.Get()->QueryInterface(IID.IID_IShellItem2, (void**)pShellItem2.GetAddressOf());
hr = PInvoke.PSGetPropertyKeyFromName("System.Recycle.DeletedFrom", out var originalPathPropertyKey);
hr = pShellItem2.Get()->GetString(originalPathPropertyKey, out var szOriginalPath);

Expand Down
30 changes: 4 additions & 26 deletions src/Files.App/Services/Windows/WindowsDialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ public unsafe bool Open_FileOpenDialog(nint hWnd, bool pickFoldersOnly, string[]
try
{
using ComPtr<IFileOpenDialog> pDialog = default;
var dialogInstanceIid = typeof(FileOpenDialog).GUID;
var dialogIid = typeof(IFileOpenDialog).GUID;

// Get a new instance of the dialog
HRESULT hr = PInvoke.CoCreateInstance(
&dialogInstanceIid,
null,
CLSCTX.CLSCTX_INPROC_SERVER,
&dialogIid,
(void**)pDialog.GetAddressOf())
.ThrowOnFailure();
HRESULT hr = pDialog.CoCreateInstance(CLSID.CLSID_FileOpenDialog, null, CLSCTX.CLSCTX_INPROC_SERVER);

if (filters.Length is not 0 && filters.Length % 2 is 0)
{
Expand All @@ -56,13 +46,12 @@ public unsafe bool Open_FileOpenDialog(nint hWnd, bool pickFoldersOnly, string[]

// Get the default shell folder (My Computer)
using ComPtr<IShellItem> pDefaultFolderShellItem = default;
var shellItemIid = typeof(IShellItem).GUID;
fixed (char* pszDefaultFolderPath = Environment.GetFolderPath(defaultFolder))
{
hr = PInvoke.SHCreateItemFromParsingName(
pszDefaultFolderPath,
null,
&shellItemIid,
IID.IID_IShellItem,
(void**)pDefaultFolderShellItem.GetAddressOf())
.ThrowOnFailure();
}
Expand Down Expand Up @@ -104,17 +93,7 @@ public unsafe bool Open_FileSaveDialog(nint hWnd, bool pickFoldersOnly, string[]
try
{
using ComPtr<IFileSaveDialog> pDialog = default;
var dialogInstanceIid = typeof(FileSaveDialog).GUID;
var dialogIid = typeof(IFileSaveDialog).GUID;

// Get a new instance of the dialog
HRESULT hr = PInvoke.CoCreateInstance(
&dialogInstanceIid,
null,
CLSCTX.CLSCTX_INPROC_SERVER,
&dialogIid,
(void**)pDialog.GetAddressOf())
.ThrowOnFailure();
HRESULT hr = pDialog.CoCreateInstance(CLSID.CLSID_FileSaveDialog, null, CLSCTX.CLSCTX_INPROC_SERVER);

if (filters.Length is not 0 && filters.Length % 2 is 0)
{
Expand All @@ -137,13 +116,12 @@ public unsafe bool Open_FileSaveDialog(nint hWnd, bool pickFoldersOnly, string[]

// Get the default shell folder (My Computer)
using ComPtr<IShellItem> pDefaultFolderShellItem = default;
var shellItemIid = typeof(IShellItem).GUID;
fixed (char* pszDefaultFolderPath = Environment.GetFolderPath(defaultFolder))
{
hr = PInvoke.SHCreateItemFromParsingName(
pszDefaultFolderPath,
null,
&shellItemIid,
IID.IID_IShellItem,
(void**)pDefaultFolderShellItem.GetAddressOf())
.ThrowOnFailure();
}
Expand Down
Loading
Loading