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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
// Exit macros
#define ShelExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_SHELUTIL, x, s, __VA_ARGS__)
#define ShelExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_SHELUTIL, x, s, __VA_ARGS__)
#define ShelExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_SHELUTIL, x, s, __VA_ARGS__)
#define ShelExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_SHELUTIL, x, s, __VA_ARGS__)
#define ShelExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_SHELUTIL, x, s, __VA_ARGS__)
#define ShelExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_SHELUTIL, x, s, __VA_ARGS__)
#define ShelExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_SHELUTIL, p, x, e, s, __VA_ARGS__)
#define ShelExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_SHELUTIL, p, x, s, __VA_ARGS__)
#define ShelExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_SHELUTIL, p, x, e, s, __VA_ARGS__)
#define ShelExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_SHELUTIL, p, x, s, __VA_ARGS__)
#define ShelExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_SHELUTIL, e, x, s, __VA_ARGS__)
#define ShelExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_SHELUTIL, g, x, s, __VA_ARGS__)
static PFN_SHELLEXECUTEEXW vpfnShellExecuteExW = ::ShellExecuteExW;
static HRESULT GetDesktopShellView(
__in REFIID riid,
__out void **ppv
);
static HRESULT GetShellDispatchFromView(
__in IShellView *psv,
__in REFIID riid,
__out void **ppv
);
/********************************************************************
ShelFunctionOverride - overrides the shell functions. Typically used
for unit testing.
*********************************************************************/
extern "C" void DAPI ShelFunctionOverride(
__in_opt PFN_SHELLEXECUTEEXW pfnShellExecuteExW
)
{
vpfnShellExecuteExW = pfnShellExecuteExW ? pfnShellExecuteExW : ::ShellExecuteExW;
}
/********************************************************************
ShelExec() - executes a target.
*******************************************************************/
extern "C" HRESULT DAPI ShelExec(
__in_z LPCWSTR wzTargetPath,
__in_z_opt LPCWSTR wzParameters,
__in_z_opt LPCWSTR wzVerb,
__in_z_opt LPCWSTR wzWorkingDirectory,
__in int nShowCmd,
__in_opt HWND hwndParent,
__out_opt HANDLE* phProcess
)
{
HRESULT hr = S_OK;
SHELLEXECUTEINFOW shExecInfo = {};
shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shExecInfo.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS;
shExecInfo.hwnd = hwndParent;
shExecInfo.lpVerb = wzVerb;
shExecInfo.lpFile = wzTargetPath;
shExecInfo.lpParameters = wzParameters;
shExecInfo.lpDirectory = wzWorkingDirectory;
shExecInfo.nShow = nShowCmd;
if (!vpfnShellExecuteExW(&shExecInfo))
{
ShelExitWithLastError(hr, "ShellExecEx failed with return code: %d", Dutil_er);
}
if (phProcess)
{
*phProcess = shExecInfo.hProcess;
shExecInfo.hProcess = NULL;
}
LExit:
ReleaseHandle(shExecInfo.hProcess);
return hr;
}
/********************************************************************
ShelExecUnelevated() - executes a target unelevated.
*******************************************************************/
extern "C" HRESULT DAPI ShelExecUnelevated(
__in_z LPCWSTR wzTargetPath,
__in_z_opt LPCWSTR wzParameters,
__in_z_opt LPCWSTR wzVerb,
__in_z_opt LPCWSTR wzWorkingDirectory,
__in int nShowCmd
)
{
HRESULT hr = S_OK;
BSTR bstrTargetPath = NULL;
VARIANT vtParameters = { };
VARIANT vtVerb = { };
VARIANT vtWorkingDirectory = { };
VARIANT vtShow = { };
IShellView* psv = NULL;
IShellDispatch2* psd = NULL;
bstrTargetPath = ::SysAllocString(wzTargetPath);
ShelExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate target path BSTR.");
if (wzParameters && *wzParameters)
{
vtParameters.vt = VT_BSTR;
vtParameters.bstrVal = ::SysAllocString(wzParameters);
ShelExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate parameters BSTR.");
}
if (wzVerb && *wzVerb)
{
vtVerb.vt = VT_BSTR;
vtVerb.bstrVal = ::SysAllocString(wzVerb);
ShelExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate verb BSTR.");
}
if (wzWorkingDirectory && *wzWorkingDirectory)
{
vtWorkingDirectory.vt = VT_BSTR;
vtWorkingDirectory.bstrVal = ::SysAllocString(wzWorkingDirectory);
ShelExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate working directory BSTR.");
}
vtShow.vt = VT_INT;
vtShow.intVal = nShowCmd;
hr = GetDesktopShellView(IID_PPV_ARGS(&psv));
ShelExitOnFailure(hr, "Failed to get desktop shell view.");
hr = GetShellDispatchFromView(psv, IID_PPV_ARGS(&psd));
ShelExitOnFailure(hr, "Failed to get shell dispatch from view.");
hr = psd->ShellExecute(bstrTargetPath, vtParameters, vtWorkingDirectory, vtVerb, vtShow);
if (S_FALSE == hr)
{
hr = HRESULT_FROM_WIN32(ERROR_CANCELLED);
}
ShelExitOnRootFailure(hr, "Failed to launch unelevate executable: %ls", bstrTargetPath);
LExit:
ReleaseObject(psd);
ReleaseObject(psv);
ReleaseBSTR(vtWorkingDirectory.bstrVal);
ReleaseBSTR(vtVerb.bstrVal);
ReleaseBSTR(vtParameters.bstrVal);
ReleaseBSTR(bstrTargetPath);
return hr;
}
/********************************************************************
ShelGetFolder() - gets a folder by CSIDL.
*******************************************************************/
extern "C" HRESULT DAPI ShelGetFolder(
__out_z LPWSTR* psczFolderPath,
__in int csidlFolder
)
{
HRESULT hr = S_OK;
WCHAR wzPath[MAX_PATH];
hr = ::SHGetFolderPathW(NULL, csidlFolder | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, wzPath);
ShelExitOnFailure(hr, "Failed to get folder path for CSIDL: %d", csidlFolder);
hr = StrAllocString(psczFolderPath, wzPath, 0);
ShelExitOnFailure(hr, "Failed to copy shell folder path: %ls", wzPath);
hr = PathBackslashTerminate(psczFolderPath);
ShelExitOnFailure(hr, "Failed to backslash terminate shell folder path: %ls", *psczFolderPath);
LExit:
return hr;
}
/********************************************************************
ShelGetKnownFolder() - gets a folder by KNOWNFOLDERID.
Note: return E_NOTIMPL if called on pre-Vista operating systems.
*******************************************************************/
#ifndef REFKNOWNFOLDERID
#define REFKNOWNFOLDERID REFGUID
#endif
#ifndef KF_FLAG_CREATE
#define KF_FLAG_CREATE 0x00008000 // Make sure that the folder already exists or create it and apply security specified in folder definition
#endif
EXTERN_C typedef HRESULT (STDAPICALLTYPE *PFN_SHGetKnownFolderPath)(
REFKNOWNFOLDERID rfid,
DWORD dwFlags,
HANDLE hToken,
PWSTR *ppszPath
);
extern "C" HRESULT DAPI ShelGetKnownFolder(
__out_z LPWSTR* psczFolderPath,
__in REFKNOWNFOLDERID rfidFolder
)
{
HRESULT hr = S_OK;
HMODULE hShell32Dll = NULL;
PFN_SHGetKnownFolderPath pfn = NULL;
LPWSTR pwzPath = NULL;
hr = LoadSystemLibrary(L"shell32.dll", &hShell32Dll);
if (E_MODNOTFOUND == hr)
{
TraceError(hr, "Failed to load shell32.dll");
ExitFunction1(hr = E_NOTIMPL);
}
ShelExitOnFailure(hr, "Failed to load shell32.dll.");
pfn = reinterpret_cast<PFN_SHGetKnownFolderPath>(::GetProcAddress(hShell32Dll, "SHGetKnownFolderPath"));
ShelExitOnNull(pfn, hr, E_NOTIMPL, "Failed to find SHGetKnownFolderPath entry point.");
hr = pfn(rfidFolder, KF_FLAG_CREATE, NULL, &pwzPath);
ShelExitOnFailure(hr, "Failed to get known folder path.");
hr = StrAllocString(psczFolderPath, pwzPath, 0);
ShelExitOnFailure(hr, "Failed to copy shell folder path: %ls", pwzPath);
hr = PathBackslashTerminate(psczFolderPath);
ShelExitOnFailure(hr, "Failed to backslash terminate shell folder path: %ls", *psczFolderPath);
LExit:
if (pwzPath)
{
::CoTaskMemFree(pwzPath);
}
if (hShell32Dll)
{
::FreeLibrary(hShell32Dll);
}
return hr;
}
// Internal functions.
static HRESULT GetDesktopShellView(
__in REFIID riid,
__out void **ppv
)
{
HRESULT hr = S_OK;
IShellWindows* psw = NULL;
HWND hwnd = NULL;
IDispatch* pdisp = NULL;
VARIANT vEmpty = {}; // VT_EMPTY
IShellBrowser* psb = NULL;
IShellFolder* psf = NULL;
IShellView* psv = NULL;
// use the shell view for the desktop using the shell windows automation to find the
// desktop web browser and then grabs its view
// returns IShellView, IFolderView and related interfaces
hr = ::CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw));
ShelExitOnFailure(hr, "Failed to get shell view.");
hr = psw->FindWindowSW(&vEmpty, &vEmpty, SWC_DESKTOP, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp);
if (S_OK == hr)
{
hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, IID_PPV_ARGS(&psb));
ShelExitOnFailure(hr, "Failed to get desktop window.");
hr = psb->QueryActiveShellView(&psv);
ShelExitOnFailure(hr, "Failed to get active shell view.");
hr = psv->QueryInterface(riid, ppv);
ShelExitOnFailure(hr, "Failed to query for the desktop shell view.");
}
else if (S_FALSE == hr)
{
//Windows XP
hr = SHGetDesktopFolder(&psf);
ShelExitOnFailure(hr, "Failed to get desktop folder.");
hr = psf->CreateViewObject(NULL, IID_IShellView, ppv);
ShelExitOnFailure(hr, "Failed to query for the desktop shell view.");
}
else
{
ShelExitOnFailure(hr, "Failed to get desktop window.");
}
LExit:
ReleaseObject(psv);
ReleaseObject(psb);
ReleaseObject(psf);
ReleaseObject(pdisp);
ReleaseObject(psw);
return hr;
}
static HRESULT GetShellDispatchFromView(
__in IShellView *psv,
__in REFIID riid,
__out void **ppv
)
{
HRESULT hr = S_OK;
IDispatch *pdispBackground = NULL;
IShellFolderViewDual *psfvd = NULL;
IDispatch *pdisp = NULL;
// From a shell view object, gets its automation interface and from that get the shell
// application object that implements IShellDispatch2 and related interfaces.
hr = psv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&pdispBackground));
ShelExitOnFailure(hr, "Failed to get the automation interface for shell.");
hr = pdispBackground->QueryInterface(IID_PPV_ARGS(&psfvd));
ShelExitOnFailure(hr, "Failed to get shell folder view dual.");
hr = psfvd->get_Application(&pdisp);
ShelExitOnFailure(hr, "Failed to application object.");
hr = pdisp->QueryInterface(riid, ppv);
ShelExitOnFailure(hr, "Failed to get IShellDispatch2.");
LExit:
ReleaseObject(pdisp);
ReleaseObject(psfvd);
ReleaseObject(pdispBackground);
return hr;
}
|