WorkspacesWindowPropertyUtils.h
1 #pragma once 2 3 #include <Windows.h> 4 5 namespace WorkspacesWindowProperties 6 { 7 namespace Properties 8 { 9 const wchar_t LaunchedByWorkspacesID[] = L"PowerToys_LaunchedByWorkspaces"; 10 const wchar_t WorkspacesAppID[] = L"PowerToys_WorkspacesAppId"; 11 } 12 13 inline void StampWorkspacesLaunchedProperty(HWND window) 14 { 15 ::SetPropW(window, Properties::LaunchedByWorkspacesID, reinterpret_cast<HANDLE>(1)); 16 } 17 18 inline void StampWorkspacesGuidProperty(HWND window, const std::wstring& appId) 19 { 20 GUID guid; 21 HRESULT hr = CLSIDFromString(appId.c_str(), static_cast<LPCLSID> (&guid)); 22 if (hr != S_OK) 23 { 24 return; 25 } 26 27 const size_t workspacesAppIDLength = wcslen(Properties::WorkspacesAppID); 28 wchar_t* workspacesAppIDPart = new wchar_t[workspacesAppIDLength + 2]; 29 std::memcpy(&workspacesAppIDPart[0], &Properties::WorkspacesAppID, workspacesAppIDLength * sizeof(wchar_t)); 30 workspacesAppIDPart[workspacesAppIDLength + 1] = 0; 31 32 // the size of the HANDLE type can vary on different systems: 4 or 8 bytes. As we can set only a HANDLE as a property, we need more properties (2 or 4) to be able to store a GUID (16 bytes) 33 const int numberOfProperties = sizeof(GUID) / sizeof(HANDLE); 34 35 uint64_t parts[numberOfProperties]; 36 std::memcpy(&parts[0], &guid, sizeof(GUID)); 37 for (unsigned char partIndex = 0; partIndex < numberOfProperties; partIndex++) 38 { 39 workspacesAppIDPart[workspacesAppIDLength] = '0' + partIndex; 40 ::SetPropW(window, workspacesAppIDPart, reinterpret_cast<HANDLE>(parts[partIndex])); 41 } 42 } 43 44 inline const std::wstring GetGuidFromHwnd(HWND window) 45 { 46 const size_t workspacesAppIDLength = wcslen(Properties::WorkspacesAppID); 47 wchar_t* workspacesAppIDPart = new wchar_t[workspacesAppIDLength + 2]; 48 std::memcpy(&workspacesAppIDPart[0], &Properties::WorkspacesAppID, workspacesAppIDLength * sizeof(wchar_t)); 49 workspacesAppIDPart[workspacesAppIDLength + 1] = 0; 50 51 // the size of the HANDLE type can vary on different systems: 4 or 8 bytes. As we can set only a HANDLE as a property, we need more properties (2 or 4) to be able to store a GUID (16 bytes) 52 const int numberOfProperties = sizeof(GUID) / sizeof(HANDLE); 53 54 uint64_t parts[numberOfProperties]; 55 for (unsigned char partIndex = 0; partIndex < numberOfProperties; partIndex++) 56 { 57 workspacesAppIDPart[workspacesAppIDLength] = '0' + partIndex; 58 HANDLE rawData = GetPropW(window, workspacesAppIDPart); 59 if (rawData) 60 { 61 parts[partIndex] = reinterpret_cast<uint64_t>(rawData); 62 } 63 else 64 { 65 return L""; 66 } 67 } 68 69 GUID guid; 70 std::memcpy(&guid, &parts[0], sizeof(GUID)); 71 WCHAR* guidString; 72 StringFromCLSID(guid, &guidString); 73 74 return guidString; 75 } 76 }