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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
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.
Module Name:
Luminous.c
Abstract:
Library for managing the state of all firefly devices
Environment:
User Mode library
--*/
#include "luminous.h"
CLuminous::CLuminous(
VOID
)
{
m_pIWbemServices = NULL;
m_pIWbemClassObject = NULL;
m_bCOMInitialized = FALSE;
}
CLuminous::~CLuminous(
VOID
)
{
Close();
}
BOOL
CLuminous::Open(
VOID
)
{
HRESULT hResult;
BOOL bRet = FALSE;
if (m_pIWbemServices) {
return TRUE;
}
//
// Initialize COM library. Must be done before invoking any
// other COM function.
//
hResult = CoInitialize( NULL );
if ( FAILED (hResult)) {
_tprintf( TEXT("Error %lx: Failed to initialize COM library\n"), hResult );
goto OpenCleanup;
}
m_bCOMInitialized = TRUE;
m_pIWbemServices = ConnectToNamespace( NAME_SPACE);
if (!m_pIWbemServices ) {
_tprintf( TEXT("Could not connect name.\n") );
goto OpenCleanup;
}
m_pIWbemClassObject = GetInstanceReference( m_pIWbemServices, CLASS_NAME);
if ( !m_pIWbemClassObject ) {
_tprintf( TEXT("Could not find the instance.\n") );
goto OpenCleanup;
}
bRet = TRUE;
OpenCleanup:
if(!bRet) {
if(m_pIWbemClassObject) {
m_pIWbemClassObject->Release();
m_pIWbemClassObject = NULL;
}
if(m_pIWbemServices) {
m_pIWbemServices->Release();
m_pIWbemServices = NULL;
}
if(m_bCOMInitialized) {
CoUninitialize();
m_bCOMInitialized = FALSE;
}
}
return bRet;
}
VOID
CLuminous::Close(
VOID
)
{
if (m_pIWbemServices) {
m_pIWbemServices->Release();
m_pIWbemServices = NULL;
}
if (m_pIWbemClassObject) {
m_pIWbemClassObject->Release();
m_pIWbemClassObject = NULL;
}
if(m_bCOMInitialized) {
CoUninitialize();
m_bCOMInitialized = FALSE;
}
}
BOOL
CLuminous::Get(
_In_ BOOL *Enabled
)
{
VARIANT varPropVal;
BSTR bstrPropertyName = NULL;
HRESULT hResult;
CIMTYPE cimType;
BOOL bRet= FALSE;
if (!m_pIWbemServices || !m_pIWbemClassObject) {
return FALSE;
}
VariantInit( &varPropVal );
bstrPropertyName = AnsiToBstr( PROPERTY_NAME, -1 );
if ( !bstrPropertyName ) {
_tprintf( TEXT("Error out of memory.\n") );
VariantClear( &varPropVal );
return FALSE ;
}
//
// Get the property value.
//
hResult = m_pIWbemClassObject->Get(
bstrPropertyName,
0,
&varPropVal,
&cimType,
NULL );
if ( hResult != WBEM_S_NO_ERROR ) {
_tprintf( TEXT("Error %lX: Failed to read property value of %s.\n"),
hResult, PROPERTY_NAME);
} else {
if(varPropVal.vt == VT_BOOL) {
*Enabled = varPropVal.boolVal;
bRet = TRUE;
}
}
if(bstrPropertyName) {
SysFreeString( bstrPropertyName );
}
VariantClear( &varPropVal );
return bRet;
}
BOOL
CLuminous::Set(
_In_ BOOL Enabled
)
{
VARIANT varPropVal;
BSTR bstrPropertyName = NULL;
HRESULT hResult;
CIMTYPE cimType;
BOOL bRet = FALSE;
LPTSTR lpProperty = PROPERTY_NAME;
if (!m_pIWbemServices || !m_pIWbemClassObject) {
return FALSE;
}
VariantInit( &varPropVal );
bstrPropertyName = AnsiToBstr( lpProperty, -1 );
if ( !bstrPropertyName ) {
_tprintf( TEXT("Error out of memory.\n") );
goto End ;
}
//
// Get the property value.
//
hResult = m_pIWbemClassObject->Get(
bstrPropertyName,
0,
&varPropVal,
&cimType,
NULL );
if ( hResult != WBEM_S_NO_ERROR ) {
_tprintf( TEXT("Error %lX: Failed to read property value of %s.\n"),
hResult, lpProperty );
goto End;
}
if(varPropVal.vt == VT_BOOL) {
varPropVal.boolVal = (VARIANT_BOOL)Enabled;
//
// Set the property value
//
hResult = m_pIWbemClassObject->Put(
bstrPropertyName,
0,
&varPropVal,
cimType
);
if ( hResult == WBEM_S_NO_ERROR ) {
hResult = m_pIWbemServices->PutInstance(
m_pIWbemClassObject,
WBEM_FLAG_UPDATE_ONLY,
NULL,
NULL );
if ( hResult != WBEM_S_NO_ERROR ) {
_tprintf( TEXT("Failed to save the instance,")
TEXT(" %s will not be updated.\n"),
lpProperty );
} else {
bRet = TRUE;
}
}
else {
_tprintf( TEXT("Error %lX: Failed to set property value of %s.\n"),
hResult, lpProperty );
}
}
End:
if(bstrPropertyName) {
SysFreeString( bstrPropertyName );
}
VariantClear( &varPropVal );
return bRet;
}
//
// The function connects to the namespace specified by the user.
//
IWbemServices *ConnectToNamespace (_In_ LPTSTR chNamespace)
{
IWbemServices *pIWbemServices = NULL;
IWbemLocator *pIWbemLocator = NULL;
BSTR bstrNamespace;
HRESULT hResult;
//
// Create an instance of WbemLocator interface.
//
hResult = CoCreateInstance(
CLSID_WbemLocator,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *)&pIWbemLocator );
if ( hResult != S_OK ) {
_tprintf( TEXT("Error %lX: Could not create instance of IWbemLocator interface.\n"),
hResult );
return NULL;
}
//
// Namespaces are passed to COM in BSTRs.
//
bstrNamespace = AnsiToBstr( chNamespace, -1 );
if ( !bstrNamespace ) {
_tprintf( TEXT("Out of memory.\n") );
pIWbemLocator->Release( );
return NULL;
}
//
// Using the locator, connect to COM in the given namespace.
//
hResult = pIWbemLocator->ConnectServer(
bstrNamespace,
NULL, // NULL means current account, for simplicity.
NULL, // NULL means current password, for simplicity.
0L, // locale
0L, // securityFlags
NULL, // authority (domain for NTLM)
NULL, // context
&pIWbemServices ); // Returned IWbemServices.
//
// Done with Namespace.
//
SysFreeString( bstrNamespace );
if ( hResult != WBEM_S_NO_ERROR) {
_tprintf( TEXT("Error %lX: Failed to connect to namespace %s.\n"),
hResult, chNamespace );
pIWbemLocator->Release( );
return NULL;
}
//
// Switch the security level to IMPERSONATE so that provider(s)
// will grant access to system-level objects, and so that
// CALL authorization will be used.
//
hResult = CoSetProxyBlanket(
(IUnknown *)pIWbemServices, // proxy
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // server principle name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // identity of the client
0 ); // capability flags
if ( hResult != S_OK ) {
_tprintf( TEXT("Error %lX: Failed to impersonate.\n"), hResult );
pIWbemLocator->Release();
pIWbemServices->Release();
return NULL;
}
pIWbemLocator->Release();
return pIWbemServices;
}
//
// The function returns an interface pointer to the instance given its
// list-index.
//
IWbemClassObject *GetInstanceReference (
IWbemServices *pIWbemServices,
_In_ LPTSTR lpClassName)
{
IWbemClassObject *pInst = NULL;
IEnumWbemClassObject *pEnumInst;
BSTR bstrClassName;
BOOL bFound;
ULONG ulCount;
HRESULT hResult;
bstrClassName = AnsiToBstr( lpClassName, -1 );
if ( !bstrClassName ) {
_tprintf( TEXT("Out of memory.\n") );
return NULL;
}
//
// Get Instance Enumerator Interface.
//
pEnumInst = NULL;
hResult = pIWbemServices->CreateInstanceEnum(
bstrClassName, // Name of the root class.
WBEM_FLAG_SHALLOW | // Enumerate at current root only.
WBEM_FLAG_FORWARD_ONLY, // Forward-only enumeration.
NULL, // Context.
&pEnumInst ); // pointer to class enumerator
if ( hResult != WBEM_S_NO_ERROR || pEnumInst == NULL ) {
_tprintf( TEXT("Error %lX: Failed to get a reference")
TEXT(" to instance enumerator.\n"), hResult );
}
else {
//
// Get pointer to the instance.
//
hResult = WBEM_S_NO_ERROR;
bFound = FALSE;
while ( (hResult == WBEM_S_NO_ERROR) && (bFound == FALSE) ) {
hResult = pEnumInst->Next(
2000, // two seconds timeout
1, // return just one instance.
&pInst, // pointer to instance.
&ulCount); // Number of instances returned.
if ( ulCount > 0 ) {
bFound = TRUE;
break;
}
}
if ( bFound == FALSE && pInst) {
pInst->Release( );
pInst = NULL;
}
}
//
// Done with the instance enumerator.
//
if(pEnumInst) {
pEnumInst->Release( );
}
SysFreeString( bstrClassName );
return pInst;
}
//
// The function converts an ANSI string into BSTR and returns it in an
// allocated memory. The memory must be freed by the caller using free()
// function. If nLenSrc is -1, the string is null terminated.
//
BSTR AnsiToBstr (_In_ LPTSTR lpSrc, _In_ int nLenSrc)
{
BSTR lpDest;
//
// In case of ANSI version, we need to change the ANSI string to UNICODE since
// BSTRs are essentially UNICODE strings.
//
#ifndef UNICODE
int nLenDest;
nLenDest = MultiByteToWideChar( CP_ACP, 0, lpSrc, nLenSrc, NULL, 0);
lpDest = SysAllocStringLen( NULL, nLenDest );
if ( lpDest ) {
MultiByteToWideChar( CP_ACP, 0, lpSrc, nLenSrc, lpDest, nLenDest );
}
//
// In case of UNICODE version, we simply allocate memory and copy the string.
//
#else
if ( lpSrc == NULL ) {
nLenSrc = 0;
}
else {
if ( nLenSrc == - 1 ) {
size_t temp = _tcslen( lpSrc );
if (temp > INT_MAX - 1) {
return NULL;
}
nLenSrc = (int) temp + 1;
}
}
lpDest = SysAllocStringLen( lpSrc, nLenSrc );
#endif
return lpDest;
}
|