Skip to content

Commit

Permalink
[FancyZones][Hotfix] Fix VirtualDesktopManager init (#14793)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeraphimaZykova authored and jaimecbernardo committed Dec 3, 2021
1 parent 903b393 commit 0a4ff5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
43 changes: 15 additions & 28 deletions src/modules/fancyzones/FancyZonesLib/VirtualDesktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/FancyZonesLib/VirtualDesktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 0a4ff5e

Please sign in to comment.