2020-04-22 01:57:21 +08:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include "VirtualDesktopUtils.h"
|
|
|
|
|
2020-08-11 19:51:06 +08:00
|
|
|
// Non-Localizable strings
|
|
|
|
namespace NonLocalizable
|
2020-04-22 01:57:21 +08:00
|
|
|
{
|
2020-04-30 17:16:25 +08:00
|
|
|
const wchar_t RegCurrentVirtualDesktop[] = L"CurrentVirtualDesktop";
|
|
|
|
const wchar_t RegVirtualDesktopIds[] = L"VirtualDesktopIDs";
|
2020-05-01 22:13:16 +08:00
|
|
|
const wchar_t RegKeyVirtualDesktops[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops";
|
2020-11-17 19:39:31 +08:00
|
|
|
const wchar_t RegKeyVirtualDesktopsFromSession[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\%d\\VirtualDesktops";
|
2020-08-11 19:51:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace VirtualDesktopUtils
|
|
|
|
{
|
|
|
|
const CLSID CLSID_ImmersiveShell = { 0xC2F03A33, 0x21F5, 0x47FA, 0xB4, 0xBB, 0x15, 0x63, 0x62, 0xA2, 0xF2, 0x39 };
|
2020-04-30 17:16:25 +08:00
|
|
|
|
2020-04-22 01:57:21 +08:00
|
|
|
IServiceProvider* GetServiceProvider()
|
|
|
|
{
|
|
|
|
IServiceProvider* provider{ nullptr };
|
|
|
|
if (FAILED(CoCreateInstance(CLSID_ImmersiveShell, nullptr, CLSCTX_LOCAL_SERVER, __uuidof(provider), (PVOID*)&provider)))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
IVirtualDesktopManager* GetVirtualDesktopManager()
|
|
|
|
{
|
|
|
|
IVirtualDesktopManager* manager{ nullptr };
|
|
|
|
IServiceProvider* serviceProvider = GetServiceProvider();
|
|
|
|
if (serviceProvider == nullptr || FAILED(serviceProvider->QueryService(__uuidof(manager), &manager)))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetWindowDesktopId(HWND topLevelWindow, GUID* desktopId)
|
|
|
|
{
|
|
|
|
static IVirtualDesktopManager* virtualDesktopManager = GetVirtualDesktopManager();
|
|
|
|
return (virtualDesktopManager != nullptr) &&
|
|
|
|
SUCCEEDED(virtualDesktopManager->GetWindowDesktopId(topLevelWindow, desktopId));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetZoneWindowDesktopId(IZoneWindow* zoneWindow, GUID* desktopId)
|
|
|
|
{
|
|
|
|
// Format: <device-id>_<resolution>_<virtual-desktop-id>
|
|
|
|
std::wstring uniqueId = zoneWindow->UniqueId();
|
|
|
|
std::wstring virtualDesktopId = uniqueId.substr(uniqueId.rfind('_') + 1);
|
|
|
|
return SUCCEEDED(CLSIDFromString(virtualDesktopId.c_str(), desktopId));
|
|
|
|
}
|
2020-04-30 17:16:25 +08:00
|
|
|
|
2020-05-01 22:13:16 +08:00
|
|
|
bool GetDesktopIdFromCurrentSession(GUID* desktopId)
|
2020-04-30 17:16:25 +08:00
|
|
|
{
|
|
|
|
DWORD sessionId;
|
2020-11-17 19:39:31 +08:00
|
|
|
if (!ProcessIdToSessionId(GetCurrentProcessId(), &sessionId))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-30 17:16:25 +08:00
|
|
|
|
|
|
|
wchar_t sessionKeyPath[256]{};
|
2020-11-17 19:39:31 +08:00
|
|
|
if (FAILED(StringCchPrintfW(sessionKeyPath, ARRAYSIZE(sessionKeyPath), NonLocalizable::RegKeyVirtualDesktopsFromSession, sessionId)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-30 17:16:25 +08:00
|
|
|
|
|
|
|
wil::unique_hkey key{};
|
|
|
|
if (RegOpenKeyExW(HKEY_CURRENT_USER, sessionKeyPath, 0, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS)
|
|
|
|
{
|
2020-11-17 19:39:31 +08:00
|
|
|
GUID value{};
|
2020-04-30 17:16:25 +08:00
|
|
|
DWORD size = sizeof(GUID);
|
2020-08-11 19:51:06 +08:00
|
|
|
if (RegQueryValueExW(key.get(), NonLocalizable::RegCurrentVirtualDesktop, 0, nullptr, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS)
|
2020-04-30 17:16:25 +08:00
|
|
|
{
|
|
|
|
*desktopId = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:13:16 +08:00
|
|
|
bool GetCurrentVirtualDesktopId(GUID* desktopId)
|
|
|
|
{
|
2020-09-10 20:42:36 +08:00
|
|
|
// 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.
|
|
|
|
if (GetDesktopIdFromCurrentSession(desktopId))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-17 19:39:31 +08:00
|
|
|
// Fallback scenario is to get array of virtual desktops stored in registry, but not kept per session.
|
|
|
|
// Note that we are taking first element from virtual desktop array, which is primary desktop.
|
|
|
|
// If user has more than one virtual desktop, previous function should return correct value, as desktop
|
|
|
|
// switch occurred in current session.
|
2020-09-10 20:42:36 +08:00
|
|
|
else
|
2020-05-01 22:13:16 +08:00
|
|
|
{
|
|
|
|
std::vector<GUID> ids{};
|
2020-09-10 20:42:36 +08:00
|
|
|
if (GetVirtualDesktopIds(ids) && ids.size() > 0)
|
2020-05-01 22:13:16 +08:00
|
|
|
{
|
2020-09-10 20:42:36 +08:00
|
|
|
*desktopId = ids[0];
|
|
|
|
return true;
|
2020-05-01 22:13:16 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 20:42:36 +08:00
|
|
|
return false;
|
2020-05-01 22:13:16 +08:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:13:50 +08:00
|
|
|
bool GetVirtualDesktopIds(HKEY hKey, std::vector<GUID>& ids)
|
2020-04-30 17:16:25 +08:00
|
|
|
{
|
2020-05-01 22:13:16 +08:00
|
|
|
if (!hKey)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-30 17:16:25 +08:00
|
|
|
DWORD bufferCapacity;
|
|
|
|
// request regkey binary buffer capacity only
|
2020-08-11 19:51:06 +08:00
|
|
|
if (RegQueryValueExW(hKey, NonLocalizable::RegVirtualDesktopIds, 0, nullptr, nullptr, &bufferCapacity) != ERROR_SUCCESS)
|
2020-04-30 17:16:25 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::unique_ptr<BYTE[]> buffer = std::make_unique<BYTE[]>(bufferCapacity);
|
|
|
|
// request regkey binary content
|
2020-08-11 19:51:06 +08:00
|
|
|
if (RegQueryValueExW(hKey, NonLocalizable::RegVirtualDesktopIds, 0, nullptr, buffer.get(), &bufferCapacity) != ERROR_SUCCESS)
|
2020-04-30 17:16:25 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const size_t guidSize = sizeof(GUID);
|
|
|
|
std::vector<GUID> temp;
|
|
|
|
temp.reserve(bufferCapacity / guidSize);
|
|
|
|
for (size_t i = 0; i < bufferCapacity; i += guidSize)
|
|
|
|
{
|
|
|
|
GUID* guid = reinterpret_cast<GUID*>(buffer.get() + i);
|
|
|
|
temp.push_back(*guid);
|
|
|
|
}
|
|
|
|
ids = std::move(temp);
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-01 22:13:16 +08:00
|
|
|
|
2020-05-05 16:13:50 +08:00
|
|
|
bool GetVirtualDesktopIds(std::vector<GUID>& ids)
|
2020-05-01 22:13:16 +08:00
|
|
|
{
|
2020-05-05 16:13:50 +08:00
|
|
|
return GetVirtualDesktopIds(GetVirtualDesktopsRegKey(), ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetVirtualDesktopIds(std::vector<std::wstring>& ids)
|
|
|
|
{
|
|
|
|
std::vector<GUID> guids{};
|
|
|
|
if (GetVirtualDesktopIds(guids))
|
|
|
|
{
|
|
|
|
for (auto& guid : guids)
|
|
|
|
{
|
|
|
|
wil::unique_cotaskmem_string guidString;
|
|
|
|
if (SUCCEEDED(StringFromCLSID(guid, &guidString)))
|
|
|
|
{
|
|
|
|
ids.push_back(guidString.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-05-01 22:13:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
HKEY OpenVirtualDesktopsRegKey()
|
|
|
|
{
|
|
|
|
HKEY hKey{ nullptr };
|
2020-08-11 19:51:06 +08:00
|
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER, NonLocalizable::RegKeyVirtualDesktops, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
|
2020-05-01 22:13:16 +08:00
|
|
|
{
|
|
|
|
return hKey;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HKEY GetVirtualDesktopsRegKey()
|
|
|
|
{
|
|
|
|
static wil::unique_hkey virtualDesktopsKey{ OpenVirtualDesktopsRegKey() };
|
|
|
|
return virtualDesktopsKey.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleVirtualDesktopUpdates(HWND window, UINT message, HANDLE terminateEvent)
|
|
|
|
{
|
|
|
|
HKEY virtualDesktopsRegKey = GetVirtualDesktopsRegKey();
|
|
|
|
if (!virtualDesktopsRegKey)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HANDLE regKeyEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
|
|
HANDLE events[2] = { regKeyEvent, terminateEvent };
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (RegNotifyChangeKeyValue(virtualDesktopsRegKey, TRUE, REG_NOTIFY_CHANGE_LAST_SET, regKeyEvent, TRUE) != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (WaitForMultipleObjects(2, events, FALSE, INFINITE) != (WAIT_OBJECT_0 + 0))
|
|
|
|
{
|
|
|
|
// if terminateEvent is signalized or WaitForMultipleObjects failed, terminate thread execution
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PostMessage(window, message, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 01:57:21 +08:00
|
|
|
}
|