-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathProgram.cpp
395 lines (327 loc) · 11.3 KB
/
Program.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// <copyright file="Program.cpp" company="Microsoft Corporation">
// Copyright (C) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
// </copyright>
#include "stdafx.h"
using namespace std;
// Use smart pointers (without ATL) to release objects when they fall out of scope.
_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
_COM_SMARTPTR_TYPEDEF(ISetupPackageReference, __uuidof(ISetupPackageReference));
_COM_SMARTPTR_TYPEDEF(ISetupPropertyStore, __uuidof(ISetupPropertyStore));
_COM_SMARTPTR_TYPEDEF(ISetupInstanceCatalog, __uuidof(ISetupInstanceCatalog));
void PrintInstance(
_In_ ISetupInstance* pInstance,
_In_ ISetupHelper* pHelper,
bstr_t& MaxVsInstanceVersion
);
//void PrintPackageReference(
// _In_ ISetupPackageReference* pPackage
//);
//
//void PrintProperties(
// _In_ ISetupPropertyStore* pStore
//);
BOOL FindVcInWorkloads(
_In_ LPSAFEARRAY psaPackages
);
HRESULT GetMaxVersionVsInstallationPath(bstr_t& bstrVsInstallationPath, ULONGLONG& ullVersion)
{
try
{
CoInitializer init;
ISetupConfigurationPtr query;
std::vector<ISetupInstancePtr> SetupInstances;
bstr_t MaxVsInstanceVersion;
auto hr = query.CreateInstance(__uuidof(SetupConfiguration));
if (REGDB_E_CLASSNOTREG == hr)
{
cout << "The query API is not registered. Assuming no instances are installed." << endl;
return hr;
}
else if (FAILED(hr))
{
throw win32_exception(hr, "failed to create query class");
}
ISetupConfiguration2Ptr query2(query);
IEnumSetupInstancesPtr e;
hr = query2->EnumAllInstances(&e);
if (FAILED(hr))
{
throw win32_exception(hr, "failed to query all instances");
}
ISetupHelperPtr helper(query);
ISetupInstance* pInstances[1] = {};
hr = e->Next(1, pInstances, nullptr);
while (S_OK == hr)
{
// Wrap instance without AddRef'ing.
ISetupInstancePtr instance(pInstances[0], false);
PrintInstance(instance, helper, MaxVsInstanceVersion);
SetupInstances.push_back(instance);
hr = e->Next(1, pInstances, nullptr);
}
if (FAILED(hr))
{
throw win32_exception(hr, "failed to enumerate all instances");
}
for (auto instance : SetupInstances)
{
HRESULT hr;
bstr_t bstrVersion;
if (FAILED(hr = instance->GetInstallationVersion(bstrVersion.GetAddress())))
{
throw win32_exception(hr, "failed to get InstallationVersion");
}
if (!wcscmp(bstrVersion, MaxVsInstanceVersion))
{
if (FAILED(hr = helper->ParseVersion(bstrVersion, &ullVersion)))
{
throw win32_exception(hr, "failed to parse InstallationVersion");
}
bstr_t bstrInstallationPath;
if (FAILED(hr = instance->GetInstallationPath(bstrInstallationPath.GetAddress())))
{
throw win32_exception(hr, "failed to get InstallationPath");
}
//wcout << L"Max Vs InstallationPath: " << bstrInstallationPath << endl;
bstrVsInstallationPath = bstrInstallationPath;
}
}
SetupInstances.clear();
}
catch (win32_exception& ex)
{
cerr << hex << "Error 0x" << ex.code() << ": " << ex.what() << endl;
return ex.code();
}
catch (exception& ex)
{
cerr << "Error: " << ex.what() << endl;
return E_FAIL;
}
return ERROR_SUCCESS;
}
void PrintInstance(
_In_ ISetupInstance* pInstance,
_In_ ISetupHelper* pHelper,
bstr_t& MaxVsInstanceVersion
)
{
HRESULT hr = S_OK;
ISetupInstance2Ptr instance(pInstance);
bstr_t bstrId;
if (FAILED(hr = instance->GetInstanceId(bstrId.GetAddress())))
{
throw win32_exception(hr, "failed to get InstanceId");
}
InstanceState state;
if (FAILED(hr = instance->GetState(&state)))
{
throw win32_exception(hr, "failed to get State");
}
//wcout << L"InstanceId: " << bstrId << L" (" << (eComplete == state ? L"Complete" : L"Incomplete") << L")" << endl;
bstr_t bstrVersion;
if (FAILED(hr = instance->GetInstallationVersion(bstrVersion.GetAddress())))
{
throw win32_exception(hr, "failed to get InstallationVersion");
}
//ULONGLONG ullVersion;
//if (FAILED(hr = pHelper->ParseVersion(bstrVersion, &ullVersion)))
//{
// throw win32_exception(hr, "failed to parse InstallationVersion");
//}
//wcout << L"InstallationVersion: " << bstrVersion << L" (" << ullVersion << L")" << endl;
// Reboot may have been required before the installation path was created.
//if ((eLocal & state) == eLocal)
//{
// bstr_t bstrInstallationPath;
// if (FAILED(hr = instance->GetInstallationPath(bstrInstallationPath.GetAddress())))
// {
// throw win32_exception(hr, "failed to get InstallationPath");
// }
// wcout << L"InstallationPath: " << bstrInstallationPath << endl;
//}
ISetupInstanceCatalogPtr catalog;
if (SUCCEEDED(instance->QueryInterface(&catalog)))
{
VARIANT_BOOL fIsPrerelease;
if (FAILED(hr = catalog->IsPrerelease(&fIsPrerelease)))
{
throw win32_exception(hr, "failed to get IsPrerelease");
}
const auto wzIsPrerelease = VARIANT_FALSE == fIsPrerelease ? FALSESTRING : TRUESTRING;
//wcout << L"IsPrerelease: " << wzIsPrerelease << endl;
}
// Reboot may have been required before the product package was registered (last).
if ((eRegistered & state) == eRegistered)
{
ISetupPackageReferencePtr product;
if (FAILED(hr = instance->GetProduct(&product)))
{
throw win32_exception(hr, "failed to get Product");
}
//wcout << L"Product: ";
//PrintPackageReference(product);
//wcout << endl;
LPSAFEARRAY psa = nullptr;
if (FAILED(hr = instance->GetPackages(&psa)))
{
throw win32_exception(hr, "failed to get Packages");
}
// Make sure the SAFEARRAY is freed when it falls out of scope.
safearray_ptr psa_ptr(&psa);
//wcout << L"Workloads:" << endl;
BOOL bFind = FindVcInWorkloads(psa);
if (bFind)
{
if (MaxVsInstanceVersion.length() == 0)
{
MaxVsInstanceVersion = bstrVersion;
}
else if (CompareFileVersion(bstrVersion, MaxVsInstanceVersion) > 0)
{
MaxVsInstanceVersion = bstrVersion;
}
}
}
//ISetupPropertyStorePtr properties;
//if (SUCCEEDED(instance->GetProperties(&properties)) && properties)
//{
// wcout << L"Custom properties:" << endl;
// PrintProperties(properties);
//}
//if (catalog && SUCCEEDED(catalog->GetCatalogInfo(&properties)) && properties)
//{
// wcout << L"Catalog properties:" << endl;
// PrintProperties(properties);
//}
//wcout << endl;
}
BOOL FindPackageReference(
_In_ ISetupPackageReference* pPackage, LPOLESTR pszId
)
{
BOOL bFind = FALSE;
HRESULT hr = S_OK;
ISetupPackageReferencePtr ref(pPackage);
bstr_t bstrId;
if (FAILED(hr = ref->GetId(bstrId.GetAddress())))
{
throw win32_exception(hr, "failed to get reference Id");
}
// Check that an ID is registered; unexpected otherwise, but would throw in RCW.
if (!!bstrId)
{
//wcout << bstrId;
if (!wcscmp(bstrId, pszId))
{
bFind = TRUE;
}
}
return bFind;
}
void PrintProperties(
_In_ ISetupPropertyStore* pStore
)
{
HRESULT hr = S_OK;
LPSAFEARRAY psaNames = nullptr;
if (FAILED(hr = pStore->GetNames(&psaNames)))
{
throw win32_exception(hr, "failed to get property names");
}
// Make sure the SAFEARRAY is freed when it falls out of scope.
safearray_ptr psaNames_ptr(&psaNames);
// Lock the SAFEARRAY to get the raw pointer array.
if (FAILED(hr = ::SafeArrayLock(psaNames)))
{
throw win32_exception(hr, "failed to lock property name array");
}
auto rgpNames = reinterpret_cast<BSTR*>(psaNames->pvData);
auto cNames = psaNames->rgsabound[0].cElements;
if (0 == cNames)
{
return;
}
vector<BSTR> names(rgpNames, rgpNames + cNames);
sort(names.begin(), names.end(), [&](const BSTR bstrA, const BSTR bstrB) -> bool
{
return 0 > _wcsicmp((LPCWSTR)bstrA, (LPCWSTR)bstrB);
});
for_each(names.begin(), names.end(), [&](const BSTR bstrName)
{
variant_t var;
if (FAILED(hr = pStore->GetValue((LPCWSTR)bstrName, &var)))
{
throw win32_exception(hr, "failed to get property value");
}
wcout << L" " << bstrName << L": " << var << endl;
});
// SafeArrayDeleter will unlock if exception thrown.
::SafeArrayUnlock(psaNames);
}
BOOL FindVcInWorkloads(
_In_ LPSAFEARRAY psaPackages
)
{
// Lock the SAFEARRAY to get the raw pointer array.
auto hr = ::SafeArrayLock(psaPackages);
if (FAILED(hr))
{
throw win32_exception(hr, "failed to lock package arrays");
}
auto rgpPackages = reinterpret_cast<ISetupPackageReference**>(psaPackages->pvData);
auto cPackages = psaPackages->rgsabound[0].cElements;
if (0 == cPackages)
{
return FALSE;
}
vector<ISetupPackageReference*> packages(rgpPackages, rgpPackages + cPackages);
const WCHAR wzType[] = L"Workload";
const size_t cchType = sizeof(wzType) / sizeof(WCHAR) - 1;
// Find all the workload package types.
vector<ISetupPackageReference*> workloads;
for (auto pPackage : packages)
{
bstr_t bstrType;
if (SUCCEEDED(hr = pPackage->GetType(bstrType.GetAddress())))
{
if (cchType == bstrType.length() && 0 == _wcsnicmp(wzType, bstrType, cchType))
{
workloads.push_back(pPackage);
}
}
}
//sort(workloads.begin(), workloads.end(), [&](ISetupPackageReference* pA, ISetupPackageReference* pB) -> bool
//{
// bstr_t bstrA;
// bstr_t bstrB;
// if (SUCCEEDED(hr = pA->GetId(bstrA.GetAddress())))
// {
// if (SUCCEEDED(hr = pB->GetId(bstrB.GetAddress())))
// {
// return 0 > _wcsicmp(bstrA, bstrB);
// }
// }
// return 0 > _wcsicmp(__nameof(bstrA), __nameof(bstrB));
//});
BOOL bFind = FALSE;
for(ISetupPackageReference* pWorkload: workloads)
{
//wcout << L" ";
bFind = FindPackageReference(pWorkload, L"Microsoft.VisualStudio.Workload.NativeDesktop");
if (bFind)
{
break;
}
//wcout << endl;
}
// SafeArrayDeleter will unlock if exception thrown.
::SafeArrayUnlock(psaPackages);
return bFind;
}