diff --git a/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.cpp b/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.cpp index 462feee63f..6794881e78 100644 --- a/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.cpp +++ b/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.cpp @@ -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 NewGetCurrentDesktopId() { wil::unique_hkey key{}; @@ -99,9 +74,21 @@ HKEY GetVirtualDesktopsRegKey() VirtualDesktop::VirtualDesktop(const std::function& vdInitCallback, const std::function& 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 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) diff --git a/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.h b/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.h index 5abb354020..768be9ab18 100644 --- a/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.h +++ b/src/modules/fancyzones/FancyZonesLib/VirtualDesktop.h @@ -7,7 +7,7 @@ class VirtualDesktop { public: VirtualDesktop(const std::function& vdInitCallback, const std::function& vdUpdatedCallback); - ~VirtualDesktop() = default; + ~VirtualDesktop(); inline bool IsVirtualDesktopIdSavedInRegistry(GUID id) const {