-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
WscApiSample.cpp
291 lines (264 loc) · 7.72 KB
/
WscApiSample.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
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
/**
* This sample demonstrates how to query Security Center for the product name,
* product state, and product signature status for each security product
* registered.
*/
#include <stdio.h>
#include <atlbase.h>
#include <atlstr.h>
#include <wscapi.h>
#include <iwscapi.h>
HRESULT
GetSecurityProducts(
_In_ WSC_SECURITY_PROVIDER provider
)
{
HRESULT hr = S_OK;
IWscProduct* PtrProduct = nullptr;
IWSCProductList* PtrProductList = nullptr;
BSTR PtrVal = nullptr;
LONG ProductCount = 0;
WSC_SECURITY_PRODUCT_STATE ProductState;
WSC_SECURITY_SIGNATURE_STATUS ProductStatus;
if (provider != WSC_SECURITY_PROVIDER_FIREWALL &&
provider != WSC_SECURITY_PROVIDER_ANTIVIRUS &&
provider != WSC_SECURITY_PROVIDER_ANTISPYWARE)
{
hr = E_INVALIDARG;
goto exit;
}
//
// Initialize can only be called once per instance, so you need to
// CoCreateInstance for each security product type you want to query.
//
hr = CoCreateInstance(
__uuidof(WSCProductList),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IWSCProductList),
reinterpret_cast<LPVOID*> (&PtrProductList));
if(FAILED(hr))
{
wprintf(L"CoCreateInstance returned error = 0x%d \n", hr);
goto exit;
}
//
// Initialize the product list with the type of security product you're
// interested in.
//
hr = PtrProductList->Initialize(provider);
if(FAILED(hr))
{
wprintf(L"Initialize failed with error: 0x%d\n", hr);
goto exit;
}
//
// Get the number of security products of that type.
//
hr = PtrProductList->get_Count(&ProductCount);
if (FAILED(hr))
{
wprintf(L"get_Count failed with error: 0x%d\n", hr);
goto exit;
}
if (provider == WSC_SECURITY_PROVIDER_FIREWALL)
{
wprintf(L"\n\nFirewall Products:\n");
}
else if (provider == WSC_SECURITY_PROVIDER_ANTIVIRUS)
{
wprintf(L"\n\nAntivirus Products:\n");
}
else
{
wprintf(L"\n\nAntispyware Products:\n");
}
//
// Loop over each product, querying the specific attributes.
//
for (LONG i = 0; i < ProductCount; i++)
{
//
// Get the next security product
//
hr = PtrProductList->get_Item(i, &PtrProduct);
if(FAILED(hr))
{
wprintf(L"get_Item failed with error: 0x%d\n", hr);
goto exit;
}
//
// Get the product name
//
hr = PtrProduct->get_ProductName(&PtrVal);
if (FAILED(hr))
{
wprintf(L"get_ProductName failed with error: 0x%d\n", hr);
goto exit;
}
wprintf(L"\nProduct name: %s\n", PtrVal);
// Caller is responsible for freeing the string
SysFreeString(PtrVal);
PtrVal = nullptr;
//
// Get the product state
//
hr = PtrProduct->get_ProductState(&ProductState);
if (FAILED(hr))
{
wprintf(L"get_ProductState failed with error: 0x%d\n", hr);
goto exit;
}
LPWSTR pszState;
if (ProductState == WSC_SECURITY_PRODUCT_STATE_ON)
{
pszState = L"On";
}
else if (ProductState == WSC_SECURITY_PRODUCT_STATE_OFF)
{
pszState = L"Off";
}
else if (ProductState == WSC_SECURITY_PRODUCT_STATE_SNOOZED)
{
pszState = L"Snoozed";
}
else
{
pszState = L"Expired";
}
wprintf(L"Product state: %s\n", pszState);
//
// Get the signature status (not applicable to firewall products)
//
if (provider != WSC_SECURITY_PROVIDER_FIREWALL)
{
hr = PtrProduct->get_SignatureStatus(&ProductStatus);
if (FAILED(hr))
{
wprintf(L"get_SignatureStatus failed with error: 0x%d\n", hr);
goto exit;
}
LPWSTR pszStatus = (ProductStatus == WSC_SECURITY_PRODUCT_UP_TO_DATE) ?
L"Up-to-date" : L"Out-of-date";
wprintf(L"Product status: %s\n", pszStatus);
}
//
// Get the remediation path for the security product
//
hr = PtrProduct->get_RemediationPath(&PtrVal);
if (FAILED(hr))
{
wprintf(L"get_RemediationPath failed with error: 0x%d\n", hr);
goto exit;
}
wprintf(L"Product remediation path: %s\n", PtrVal);
// Caller is responsible for freeing the string
SysFreeString(PtrVal);
PtrVal = nullptr;
//
// Get the product state timestamp (updated when product changes its
// state), and only applicable for AV products (NULL is returned for
// AS and FW products)
//
if (provider == WSC_SECURITY_PROVIDER_ANTIVIRUS)
{
hr = PtrProduct->get_ProductStateTimestamp(&PtrVal);
if (FAILED(hr))
{
wprintf(L"get_ProductStateTimestamp failed with error: 0x%d\n", hr);
goto exit;
}
wprintf(L"Product state timestamp: %s\n", PtrVal);
// Caller is responsible for freeing the string
SysFreeString(PtrVal);
PtrVal = nullptr;
}
PtrProduct->Release();
PtrProduct = nullptr;
}
exit:
if (nullptr != PtrVal)
{
SysFreeString(PtrVal);
}
if (nullptr != PtrProductList)
{
PtrProductList->Release();
}
if (nullptr != PtrProduct)
{
PtrProduct->Release();
}
return hr;
}
void PrintUsage()
{
wprintf(L"Usage: WscApiSample.exe [-av | -as | -fw]\n");
wprintf(L" av: Query Antivirus programs\n");
wprintf(L" as: Query Antispyware programs\n");
wprintf(L" fw: Query Firewall programs\n\n");
}
int
__cdecl
wmain(
_In_ int argc,
_In_reads_(argc) LPCWSTR argv[]
)
{
int ret = 0;
HRESULT hr = S_OK;
int iProviderCount = 0;
WSC_SECURITY_PROVIDER providers[3];
if (argc < 2 || argc > 4)
{
PrintUsage();
return -1;
}
//
// Parse command line arguments
//
for (int i = 1; i < argc; i++)
{
if (_wcsnicmp(argv[i], L"-av", MAX_PATH) == 0)
{
providers[iProviderCount] = WSC_SECURITY_PROVIDER_ANTIVIRUS;
iProviderCount ++;
}
else if (_wcsnicmp(argv[i], L"-as", MAX_PATH) == 0)
{
providers[iProviderCount] = WSC_SECURITY_PROVIDER_ANTISPYWARE;
iProviderCount ++;
}
else if (_wcsnicmp(argv[i], L"-fw", MAX_PATH) == 0)
{
providers[iProviderCount] = WSC_SECURITY_PROVIDER_FIREWALL;
iProviderCount ++;
}
else
{
PrintUsage();
return -1;
}
}
CoInitializeEx(0, COINIT_APARTMENTTHREADED );
for (int i = 0; i < iProviderCount; i++)
{
//
// Query security products of the specified type (AV, AS, or FW)
//
hr = GetSecurityProducts(providers[i]);
if (FAILED(hr))
{
ret = -1;
break;
}
}
CoUninitialize();
return ret;
}