forked from coddec/Classic-Shell
- Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathModernSettingsContextMenu.cpp
210 lines (165 loc) · 4.65 KB
/
ModernSettingsContextMenu.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Context menu handler for Open-Shell Modern Settings shell folder
// Based on Explorer Data Provider Sample (https://docs.microsoft.com/en-us/windows/win32/shell/samples-explorerdataprovider)
#include"stdafx.h"
#include"ModernSettings.h"
#include"ModernSettingsContextMenu.h"
#include"ComHelper.h"
#defineMENUVERB_OPEN0
structICIVERBTOIDMAP
{
LPCWSTR pszCmd; // verbW
LPCSTR pszCmdA; // verbA
UINT idCmd; // hmenu id
};
staticconst ICIVERBTOIDMAP g_ContextMenuIDMap[] =
{
{ L"open", "open", MENUVERB_OPEN },
};
HRESULT _MapICIVerbToCmdID(LPCMINVOKECOMMANDINFO pici, UINT* pid)
{
if (IS_INTRESOURCE(pici->lpVerb))
{
*pid = LOWORD((UINT_PTR)pici->lpVerb);
return S_OK;
}
if (pici->fMask & CMIC_MASK_UNICODE)
{
for (constauto& i : g_ContextMenuIDMap)
{
if (StrCmpIC(((LPCMINVOKECOMMANDINFOEX)pici)->lpVerbW, i.pszCmd) == 0)
{
*pid = i.idCmd;
return S_OK;
}
}
}
else
{
for (constauto& i : g_ContextMenuIDMap)
{
if (StrCmpICA(pici->lpVerb, i.pszCmdA) == 0)
{
*pid = i.idCmd;
return S_OK;
}
}
}
return E_FAIL;
}
staticboolActivateModernSettingPage(const WCHAR* page)
{
CComPtr<IApplicationActivationManager> mgr;
mgr.CoCreateInstance(CLSID_ApplicationActivationManager);
if (mgr)
{
DWORD pid = 0;
returnSUCCEEDED(mgr->ActivateApplication(L"windows.immersivecontrolpanel_cw5n1h2txyewy!microsoft.windows.immersivecontrolpanel", page, AO_NONE, &pid));
}
returnfalse;
}
extern ModernSettings::Setting GetModernSetting(LPCITEMIDLIST pidl);
static HRESULT Execute(constwchar_t* cmd)
{
return (intptr_t)::ShellExecute(nullptr, L"open", cmd, nullptr, nullptr, SW_SHOWNORMAL) > 32 ? S_OK : E_FAIL;
}
static HRESULT OpenItemByPidl(LPCITEMIDLIST pidl)
{
auto child = ILFindLastID(pidl);
auto setting = GetModernSetting(child);
if (!setting)
return E_INVALIDARG;
if (setting.hostId == L"{6E6DDBCB-9C89-434B-A994-D5F22239523B}")
{
if (setting.deepLink.empty())
return E_INVALIDARG;
std::wstring cmd(L"windowsdefender://");
cmd += setting.deepLink;
returnExecute(cmd.c_str());
}
if (setting.hostId == L"{7E0522FC-1AC4-41CA-AFD0-3610417A9C41}")
{
if (setting.pageId.empty())
return E_INVALIDARG;
std::wstring cmd(L"shell:::");
cmd += setting.pageId;
returnExecute(cmd.c_str());
}
if (setting.hostId == L"{12B1697E-D3A0-4DBC-B568-CCF64A3F934D}")
{
if (setting.deepLink.empty())
return E_INVALIDARG;
std::wstring cmd(setting.deepLink);
if (cmd.compare(0, 8, L"shell:::") == 0)
returnExecute(cmd.c_str());
cmd.resize(MAX_PATH);
DoEnvironmentSubst(cmd.data(), (UINT)cmd.size());
STARTUPINFO startupInfo = { sizeof(startupInfo) };
PROCESS_INFORMATION processInfo{};
if (!CreateProcess(nullptr, cmd.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &startupInfo, &processInfo))
return E_FAIL;
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
return S_OK;
}
if (setting.pageId.empty())
return E_INVALIDARG;
std::wstring page;
page += L"page=";
page += setting.pageId;
if (!setting.settingId.empty())
{
page += L"&target=";
page += setting.settingId;
}
elseif (!setting.groupId.empty())
{
page += L"&group=";
page += setting.groupId;
}
page += L"&ActivationType=Search";
ActivateModernSettingPage(page.c_str());
return S_OK;
}
// CModernSettingsContextMenu
HRESULT CModernSettingsContextMenu::QueryContextMenu(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT /* idCmdLast */, UINT /* uFlags */)
{
InsertMenu(hmenu, indexMenu++, MF_BYPOSITION, idCmdFirst + MENUVERB_OPEN, L"Open");
// other verbs could go here...
// indicate that we added one verb.
returnMAKE_HRESULT(SEVERITY_SUCCESS, 0, (USHORT)(1));
}
HRESULT CModernSettingsContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
{
HRESULT hr = E_INVALIDARG;
UINT uID;
// Is this command for us?
if (SUCCEEDED(_MapICIVerbToCmdID(pici, &uID)))
{
if (uID == MENUVERB_OPEN && m_pdtobj)
{
CAbsolutePidl pidl;
hr = SHGetIDListFromObject(m_pdtobj, &pidl);
if (SUCCEEDED(hr))
hr = OpenItemByPidl(pidl);
}
}
return hr;
}
HRESULT CModernSettingsContextMenu::GetCommandString(UINT_PTR /* idCmd */, UINT /* uType */, UINT* /* pRes */, LPSTR /* pszName */, UINT /* cchMax */)
{
return E_NOTIMPL;
}
HRESULT CModernSettingsContextMenu::Initialize(PCIDLIST_ABSOLUTE /* pidlFolder */, IDataObject* pdtobj, HKEY /* hkeyProgID */)
{
m_pdtobj = pdtobj;
return S_OK;
}
HRESULT CModernSettingsContextMenu::SetSite(IUnknown* punkSite)
{
m_punkSite = punkSite;
return S_OK;
}
HRESULT CModernSettingsContextMenu::GetSite(REFIID riid, void** ppvSite)
{
return m_punkSite ? m_punkSite->QueryInterface(riid, ppvSite) : E_FAIL;
}