Sample Code

Windows Driver Samples/ Toaster Sample Driver/ C++/ exe/ wmi/ wmigetset.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
#include "wmitoast.h"
 
#define TOASTER_DEVICE_INFO_CLASS   L"ToasterControl"
#define TOASTER_VAR1                L"ControlValue"
 
 
HRESULT
GetAndSetValuesInClass(
    _In_     IWbemServices* WbemServices,
    _In_opt_ PWSTR UserId,
    _In_opt_ PWSTR Password,
    _In_opt_ PWSTR DomainName
    )
 
/*++
 
Routine Description:
 
    This routine enumerates the instances of the Toaster Device Information
    class and gets/sets the value of one of its Properties.
 
Arguments:
 
    WbemServices - Pointer to the WBEM services interface used for accessing
        the WMI services.
 
    UserId - Pointer to the user id information or NULL.
 
    Password - Pointer to password or NULL. If the user id is not specified,
        this parameter is ignored.
 
    DomainName - Pointer to domain name or NULL. If the user id is not specified,
        this parameter is ignored.
 
Return Value:
 
    HRESULT Status code.
 
--*/
 
{
    HRESULT status = S_OK;
 
    IEnumWbemClassObject* enumerator  = NULL;
    IWbemClassObject* classObj        = NULL;
    IWbemClassObject* instanceObj     = NULL;
 
    const BSTR className   = SysAllocString(TOASTER_DEVICE_INFO_CLASS);
 
    VARIANT instProperty;
    ULONG   nbrObjsSought = 1;
    ULONG   nbrObjsReturned;
 
    //
    // Create an Enumeration object to enumerate the instances of the given class.
    //
    status = WbemServices->CreateInstanceEnum(className,
                                              WBEM_FLAG_SHALLOW | WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
                                              NULL,
                                              &enumerator);
    if (FAILED(status)) {
        goto exit;
    }
 
    //
    // Set authentication information for the interface.
    //
    status = SetInterfaceSecurity(enumerator, UserId, Password, DomainName);
    if (FAILED(status)) {
        goto exit;
    }
 
    do {
 
        //
        // Get the instance object for each instance of the class.
        //
        status = enumerator->Next(WBEM_INFINITE,
                                  nbrObjsSought,
                                  &instanceObj,
                                  &nbrObjsReturned);
 
        if (status == WBEM_S_FALSE) {
            status = S_OK;
            break;
        }
 
        if (FAILED(status)) {
            if (status == WBEM_E_INVALID_CLASS) {
                printf("ERROR: Toaster driver may not be active on the system.\n");
            }
            goto exit;
        }
 
        //
        // To obtain the object path of the object for which the method has to be
        // executed, query the "__PATH" property of the WMI instance object.
        //
        status = instanceObj->Get(_bstr_t(L"__PATH"), 0, &instProperty, 0, 0);
        if (FAILED(status)) {
            goto exit;
        }
 
        printf("\n");
        printf("Instance Path .: %ws\n", (wchar_t*)instProperty.bstrVal);
 
        //
        // Get the current value of the DummyValue property.
        //
        status = instanceObj->Get(TOASTER_VAR1, 0, &instProperty, NULL, NULL);
        if (FAILED(status)) {
            goto exit;
        }
 
        printf("  Property ....: %ws\n", TOASTER_VAR1);
        printf("  Old Value ...: %d\n", instProperty.lVal);
 
        //
        // Set a new value for the DummyValue property.
        //
        instProperty.lVal++;
 
        status = instanceObj->Put(TOASTER_VAR1, 0, &instProperty, 0);
        if (FAILED(status)) {
            goto exit;
        }
 
        status = WbemServices->PutInstance(instanceObj,
                                           WBEM_FLAG_UPDATE_ONLY,
                                           NULL,
                                           NULL);
        if (FAILED(status)) {
            goto exit;
        }
 
        status = instanceObj->Get(_bstr_t(TOASTER_VAR1), 0, &instProperty, NULL, NULL);
        if (FAILED(status)) {
            goto exit;
        }
 
        printf("  New Value ...: %d\n", instProperty.lVal);
 
        instanceObj->Release();
        instanceObj = NULL;
 
    } while (!FAILED(status));
 
exit:
 
    if (className != NULL) {
        SysFreeString(className);
    }
 
    if (classObj != NULL) {
        classObj->Release();
    }
 
    if (enumerator != NULL) {
        enumerator->Release();
    }
 
    if (instanceObj != NULL) {
        instanceObj->Release();
    }
 
    return status;
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS