Skip to content

Code Quality: Improved WindowsStorables #17191

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

0x5bfa
Copy link
Member

@0x5bfa 0x5bfa commented Jun 14, 2025

Resolved / Related Issues

Steps used to test these changes

  • Launch the app
  • Load Home page

@0x5bfa 0x5bfa force-pushed the 5bfa/CQ-WindowsStorable branch from 671e280 to a9ef2a8 Compare June 14, 2025 17:49
@0x5bfa 0x5bfa marked this pull request as ready for review June 14, 2025 17:49
Comment on lines 14 to 21
public IShellItem* ThisPtr
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected set;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongle-the-gadget Could you review for real quick? I unwrapped the ptr-s where they are passed to constructors of WindowsFile or WindowsFolder, which means they shouldnt be disposed outside the block where defined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanna make sure pointer managements around windows storables are done appropriately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have some code where the storables are used? In general you shouldn't call Release on the pointers you pass to the constructor as it looks like those objects take ownership.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've ensured all of the instantiation is done witout releasing.

// Instead of:

ComPtr<IShellItem> pShellItem = ...;
var folder = new WindowsFolder(pShellItem.Get());

// Did this. because no reason to use ComPtr.

IShellItem* pShellItem = ...;
var folder = new WindowsFolder(pShellItem);

public static WindowsStorable? TryParse(string szPath)
{
HRESULT hr = default;
IShellItem* pShellItem = null;
fixed (char* pszPath = szPath)
hr = PInvoke.SHCreateItemFromParsingName(pszPath, null, IID.IID_IShellItem, (void**)&pShellItem);
if (pShellItem is null)
return null;
return TryParse(pShellItem);
}
public static WindowsStorable? TryParse(IShellItem* pShellItem)
{
bool isFolder = pShellItem->GetAttributes(SFGAO_FLAGS.SFGAO_FOLDER, out var returnedAttributes).Succeeded && returnedAttributes == SFGAO_FLAGS.SFGAO_FOLDER;
return isFolder ? new WindowsFolder(pShellItem) : new WindowsFile(pShellItem);
}

unsafe
{
hr = PInvoke.RoGetAgileReference(AgileReferenceOptions.AGILEREFERENCE_DEFAULT, IID.IID_IShellItem, (IUnknown*)folderCardItem.Item.ThisPtr, pAgileReference.GetAddressOf());
}
// Unpin from Quick Access on Windows
hr = await STATask.Run(() =>
{
unsafe
{
IShellItem* pShellItem = null;
hr = pAgileReference.Get()->Resolve(IID.IID_IShellItem, (void**)&pShellItem);
using var windowsFile = new WindowsFile(pShellItem);
// NOTE: "unpinfromhome" is an undocumented verb, which calls an undocumented COM class, windows.storage.dll!CRemoveFromFrequentPlacesExecute : public IExecuteCommand, ...
// NOTE: "remove" is for some shell folders where the "unpinfromhome" may not work
return windowsFile.TryInvokeContextMenuVerbs(["unpinfromhome", "remove"], true);
}
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is everything then disposed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is everything then disposed?

@yaira2
Copy link
Member

yaira2 commented Jun 15, 2025

Steps used to test these changes

@yaira2 yaira2 added the ready to merge Pull requests that are approved and ready to merge label Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to merge Pull requests that are approved and ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants