[FancyZones][Hotfix] Fix VirtualDesktopManager init (#14793)

This commit is contained in:
Seraphima Zykova 2021-12-03 09:59:05 +00:00 committed by GitHub
parent 287bc99a0f
commit 9d10426295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 29 deletions

View File

@ -12,31 +12,6 @@ namespace NonLocalizable
const wchar_t RegKeyVirtualDesktopsFromSession[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\%d\\VirtualDesktops";
}
const CLSID CLSID_ImmersiveShell = { 0xC2F03A33, 0x21F5, 0x47FA, 0xB4, 0xBB, 0x15, 0x63, 0x62, 0xA2, 0xF2, 0x39 };
IServiceProvider* GetServiceProvider()
{
IServiceProvider* provider{ nullptr };
if (FAILED(CoCreateInstance(CLSID_ImmersiveShell, nullptr, CLSCTX_LOCAL_SERVER, __uuidof(provider), (PVOID*)&provider)))
{
Logger::error("Failed to get ServiceProvider for VirtualDesktopManager");
return nullptr;
}
return provider;
}
IVirtualDesktopManager* GetVirtualDesktopManager()
{
IVirtualDesktopManager* manager{ nullptr };
IServiceProvider* serviceProvider = GetServiceProvider();
if (serviceProvider == nullptr || FAILED(serviceProvider->QueryService(__uuidof(manager), &manager)))
{
Logger::error("Failed to get VirtualDesktopManager");
return nullptr;
}
return manager;
}
std::optional<GUID> NewGetCurrentDesktopId()
{
wil::unique_hkey key{};
@ -99,9 +74,21 @@ HKEY GetVirtualDesktopsRegKey()
VirtualDesktop::VirtualDesktop(const std::function<void()>& vdInitCallback, const std::function<void()>& vdUpdatedCallback) :
m_vdInitCallback(vdInitCallback),
m_vdUpdatedCallback(vdUpdatedCallback),
m_vdManager(GetVirtualDesktopManager())
m_vdUpdatedCallback(vdUpdatedCallback)
{
auto res = CoCreateInstance(CLSID_VirtualDesktopManager, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&m_vdManager));
if (FAILED(res))
{
Logger::error("Failed to create VirtualDesktopManager instance");
}
}
VirtualDesktop::~VirtualDesktop()
{
if (m_vdManager)
{
m_vdManager->Release();
}
}
void VirtualDesktop::Init()
@ -203,7 +190,7 @@ std::optional<GUID> VirtualDesktop::GetDesktopId(HWND window) const
{
GUID id;
BOOL isWindowOnCurrentDesktop = false;
if (m_vdManager->IsWindowOnCurrentVirtualDesktop(window, &isWindowOnCurrentDesktop) == S_OK && isWindowOnCurrentDesktop)
if (m_vdManager && m_vdManager->IsWindowOnCurrentVirtualDesktop(window, &isWindowOnCurrentDesktop) == S_OK && isWindowOnCurrentDesktop)
{
// Filter windows such as Windows Start Menu, Task Switcher, etc.
if (m_vdManager->GetWindowDesktopId(window, &id) == S_OK && id != GUID_NULL)

View File

@ -7,7 +7,7 @@ class VirtualDesktop
{
public:
VirtualDesktop(const std::function<void()>& vdInitCallback, const std::function<void()>& vdUpdatedCallback);
~VirtualDesktop() = default;
~VirtualDesktop();
inline bool IsVirtualDesktopIdSavedInRegistry(GUID id) const
{