forked from mluparu/vs-setup-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cpp
175 lines (145 loc) · 4.72 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
// Setup.Configuration.ZeroDeps.VC.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "Setup.Configuration.h"
using namespace ATL;
void PrintInstance(CComPtr<ISetupInstance2>& instance2, CComPtr<ISetupHelper>& setupHelper);
void PrintPackageReference(CComPtr<ISetupPackageReference>& package);
void PrintWorkloads(CComSafeArray<IUnknown*>& packages);
int main()
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CComPtr<ISetupConfiguration> setupConfig = nullptr;
auto hresult = setupConfig.CoCreateInstance(__uuidof(SetupConfiguration), nullptr, CLSCTX_INPROC_SERVER);
if (!SUCCEEDED(hresult) || setupConfig == nullptr)
{
std::wcout << L"Visual Studio '15' may not be installed (Component creation failed)" << std::endl;
return 0;
}
CComQIPtr<ISetupConfiguration2> setupConfig2(setupConfig);
CComQIPtr<ISetupHelper> setupHelper(setupConfig);
if (!setupConfig2 || !setupHelper)
{
std::wcout << L"Unsupported version of Visual Studio '15' may be installed (ISetupConfiguration2 or ISetupHelper unavailable)" << std::endl;
return 1;
}
CComPtr<IEnumSetupInstances> enumInstances;
setupConfig2->EnumAllInstances(&enumInstances);
if (!enumInstances)
{
std::wcout << L"No VS '15' version is installed (EnumAllInstances returned null)" << std::endl;
return 1;
}
CComPtr<ISetupInstance> instance;
std::wcout << L"Listing VS '15' instances:" << std::endl;
std::wcout << L"-------------------------------------------" << std::endl;
while (SUCCEEDED(enumInstances->Next(1, &instance, nullptr)) && instance)
{
CComQIPtr<ISetupInstance2> instance2(instance);
if (!instance2)
{
std::wcout << L"Error querying instance (ISetupInstance2 unavailable)" << std::endl;
continue;
}
//Print instance
PrintInstance(instance2, setupHelper);
instance = nullptr;
std::wcout << L"*" << std::endl;
}
return 0;
}
void PrintInstance(CComPtr<ISetupInstance2>& instance2, CComPtr<ISetupHelper>& setupHelper)
{
USES_CONVERSION;
CComBSTR bstrId;
if (FAILED(instance2->GetInstanceId(&bstrId)))
{
std::wcout << L"Error reading instance id" << std::endl;
}
InstanceState state;
if (FAILED(instance2->GetState(&state)))
{
std::wcout << L"Error reading instance state" << std::endl;
}
std::wcout << L"InstanceId: " << OLE2T(bstrId)
<< L" (" << (eComplete == state ? L"Complete" : L"Incomplete") << L")"
<< std::endl;
CComBSTR bstrVersion;
if (FAILED(instance2->GetInstallationVersion(&bstrVersion)))
{
std::wcout << L"Error reading version" << std::endl;
}
else
{
ULONGLONG ullVersion;
if (FAILED(setupHelper->ParseVersion(bstrVersion, &ullVersion)))
{
std::wcout << L"Error parsing version " << OLE2T(bstrVersion) << std::endl;
}
std::wcout << L"InstallationVersion: " << OLE2T(bstrVersion) << L" (" << ullVersion << L")" << std::endl;
}
// Reboot may have been required before the installation path was created.
if ((eLocal & state) == eLocal)
{
CComBSTR bstrInstallationPath;
if (FAILED(instance2->GetInstallationPath(&bstrInstallationPath)))
{
std::wcout << L"Error getting installation path" << std::endl;
}
std::wcout << L"InstallationPath: " << OLE2T(bstrInstallationPath) << std::endl;
}
// Reboot may have been required before the product package was registered (last).
if ((eRegistered & state) == eRegistered)
{
CComPtr<ISetupPackageReference> product;
instance2->GetProduct(&product);
if (!product)
{
std::wcout << L"Error getting product info (ISetupPackageReference missing)" << std::endl;
return;
}
PrintPackageReference(product);
std::wcout << std::endl;
LPSAFEARRAY lpsaPackages;
if (FAILED(instance2->GetPackages(&lpsaPackages)))
{
std::wcout << L"Error enumerating packages (GetPackages failed)" << std::endl;
return;
}
CComSafeArray<IUnknown*> packages;
packages.Attach(lpsaPackages);
PrintWorkloads(packages);
std::wcout << std::endl;
}
}
void PrintPackageReference(CComPtr<ISetupPackageReference>& package)
{
USES_CONVERSION;
CComBSTR bstrId;
if (FAILED(package->GetId(&bstrId)))
{
std::wcout << L"Error getting reference id (GetId failed)" << std::endl;
}
CComBSTR bstrType;
if (FAILED(package->GetType(&bstrType)))
{
std::wcout << L"Error getting reference type (GetType failed)" << std::endl;
}
std::wcout << OLE2T(bstrId) << L" (" << OLE2T(bstrType) << L")" << std::endl;
}
void PrintWorkloads(CComSafeArray<IUnknown*>& packages)
{
ULONG cPackages = packages.GetCount();
for (ULONG i = 0; i < cPackages; ++i)
{
CComQIPtr<ISetupPackageReference> package = packages.GetAt(i);
if (!package)
{
std::wcout << L"Error querying package (QI for ISetupPackageReference failed)" << std::endl;
continue;
}
std::wcout << L" ";
PrintPackageReference(package);
}
}