forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspacesWindowPropertyUtils.h
76 lines (64 loc) · 2.96 KB
/
WorkspacesWindowPropertyUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include <Windows.h>
namespace WorkspacesWindowProperties
{
namespace Properties
{
const wchar_t LaunchedByWorkspacesID[] = L"PowerToys_LaunchedByWorkspaces";
const wchar_t WorkspacesAppID[] = L"PowerToys_WorkspacesAppId";
}
inline void StampWorkspacesLaunchedProperty(HWND window)
{
::SetPropW(window, Properties::LaunchedByWorkspacesID, reinterpret_cast<HANDLE>(1));
}
inline void StampWorkspacesGuidProperty(HWND window, const std::wstring& appId)
{
GUID guid;
HRESULT hr = CLSIDFromString(appId.c_str(), static_cast<LPCLSID> (&guid));
if (hr != S_OK)
{
return;
}
const size_t workspacesAppIDLength = wcslen(Properties::WorkspacesAppID);
wchar_t* workspacesAppIDPart = new wchar_t[workspacesAppIDLength + 2];
std::memcpy(&workspacesAppIDPart[0], &Properties::WorkspacesAppID, workspacesAppIDLength * sizeof(wchar_t));
workspacesAppIDPart[workspacesAppIDLength + 1] = 0;
// 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)
const int numberOfProperties = sizeof(GUID) / sizeof(HANDLE);
uint64_t parts[numberOfProperties];
std::memcpy(&parts[0], &guid, sizeof(GUID));
for (unsigned char partIndex = 0; partIndex < numberOfProperties; partIndex++)
{
workspacesAppIDPart[workspacesAppIDLength] = '0' + partIndex;
::SetPropW(window, workspacesAppIDPart, reinterpret_cast<HANDLE>(parts[partIndex]));
}
}
inline const std::wstring GetGuidFromHwnd(HWND window)
{
const size_t workspacesAppIDLength = wcslen(Properties::WorkspacesAppID);
wchar_t* workspacesAppIDPart = new wchar_t[workspacesAppIDLength + 2];
std::memcpy(&workspacesAppIDPart[0], &Properties::WorkspacesAppID, workspacesAppIDLength * sizeof(wchar_t));
workspacesAppIDPart[workspacesAppIDLength + 1] = 0;
// 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)
const int numberOfProperties = sizeof(GUID) / sizeof(HANDLE);
uint64_t parts[numberOfProperties];
for (unsigned char partIndex = 0; partIndex < numberOfProperties; partIndex++)
{
workspacesAppIDPart[workspacesAppIDLength] = '0' + partIndex;
HANDLE rawData = GetPropW(window, workspacesAppIDPart);
if (rawData)
{
parts[partIndex] = reinterpret_cast<uint64_t>(rawData);
}
else
{
return L"";
}
}
GUID guid;
std::memcpy(&guid, &parts[0], sizeof(GUID));
WCHAR* guidString;
StringFromCLSID(guid, &guidString);
return guidString;
}
}