Fix CurrentVirtualDesktop fetching from registry (#11760)

* fix(#11125): fallback to canonical regkey session unaware

* chore(#11125): use wil::unique_hkey instead of HKEY for fallback CurrentVirtualDesktop

* refactor: extract new explorer current virtual desktop behavior into a separate method
This commit is contained in:
Manuel Serra 2021-06-18 16:53:23 +02:00 committed by GitHub
parent f662c062d2
commit 5679d48073
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -51,6 +51,23 @@ namespace VirtualDesktopUtils
return SUCCEEDED(CLSIDFromString(virtualDesktopId.c_str(), desktopId));
}
bool NewGetCurrentDesktopId(GUID* desktopId)
{
wil::unique_hkey key{};
if (RegOpenKeyExW(HKEY_CURRENT_USER, NonLocalizable::RegKeyVirtualDesktops, 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS)
{
GUID value{};
DWORD size = sizeof(GUID);
if (RegQueryValueExW(key.get(), NonLocalizable::RegCurrentVirtualDesktop, 0, nullptr, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS)
{
*desktopId = value;
return true;
}
}
return false;
}
bool GetDesktopIdFromCurrentSession(GUID* desktopId)
{
DWORD sessionId;
@ -76,11 +93,19 @@ namespace VirtualDesktopUtils
return true;
}
}
return false;
}
bool GetCurrentVirtualDesktopId(GUID* desktopId)
{
// On newer Windows builds, the current virtual desktop is persisted to
// a totally different reg key. Look there first.
if (NewGetCurrentDesktopId(desktopId))
{
return true;
}
// Explorer persists current virtual desktop identifier to registry on a per session basis, but only
// after first virtual desktop switch happens. If the user hasn't switched virtual desktops in this
// session, value in registry will be empty.