Sample Code

Windows Driver Samples/ Toaster Sample Driver/ C++/ kmdf/ toastmon/ wmi.c/

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
/*++
 
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:
 
    wmi.c
 
Abstract: This module demonstrates how to receive WMI notification fired by
          another driver. The code here basically registers for toaster
          device arrival WMI notification fired by the toaster function driver.
          You can use similar technique to receive media sense notification
          (GUID_NDIS_STATUS_MEDIA_CONNECT/GUID_NDIS_STATUS_MEDIA_DISCONNECT)
          fired by NDIS whenever the network cable is plugged or unplugged.
 
Environment:
 
    Kernel mode
 
 
--*/
 
#include "toastmon.h"
#include "public.h"
#include <wmistr.h>
 
//
// These typedefs required to avoid compilation errors in Win2K build environment.
//
typedef
VOID
(*WMI_NOTIFICATION_CALLBACK)( // Copied from WDM.H
    PVOID Wnode,
    PVOID Context
    );
 
typedef
NTSTATUS
(*PFN_IO_WMI_OPEN_BLOCK)(
    IN  GUID   * DataBlockGuid,
    IN  ULONG    DesiredAccess,
    OUT PVOID  * DataBlockObject
    );
 
typedef
NTSTATUS
(*PFN_IO_WMI_SET_NOTIFICATION_CALLBACK)(
    IN PVOID                      Object,
    IN WMI_NOTIFICATION_CALLBACK  Callback,
    IN PVOID                      Context
    );
 
NTSTATUS
GetTargetFriendlyName(
    WDFIOTARGET Target,
    IN WDFMEMORY* TargetName
    );
 
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, RegisterForWMINotification)
#pragma alloc_text(PAGE, UnregisterForWMINotification)
#pragma alloc_text(PAGE, GetTargetFriendlyName)
#pragma alloc_text(PAGE, WmiNotificationCallback)
#endif
 
 
NTSTATUS
RegisterForWMINotification(
    PDEVICE_EXTENSION DeviceExt
    )
{
    NTSTATUS           status = STATUS_SUCCESS;
    GUID               wmiGuid;
    UNICODE_STRING     funcName;
 
    PFN_IO_WMI_OPEN_BLOCK                 WmiOpenBlock;
    PFN_IO_WMI_SET_NOTIFICATION_CALLBACK  WmiSetNotificationCallback;
 
    PAGED_CODE();
 
    //
    // APIs  to open WMI interfaces are available on XP and beyond, so let us
    // first check to see whether there are exported in the kernel we are
    // running before using them.
    //
    RtlInitUnicodeString(&funcName, L"IoWMIOpenBlock");
 
    WmiOpenBlock =
        (PFN_IO_WMI_OPEN_BLOCK) (ULONG_PTR)
            MmGetSystemRoutineAddress(&funcName);
 
    RtlInitUnicodeString(&funcName, L"IoWMISetNotificationCallback");
 
    WmiSetNotificationCallback =
        (PFN_IO_WMI_SET_NOTIFICATION_CALLBACK) (ULONG_PTR)
            MmGetSystemRoutineAddress(&funcName);
 
    if(WmiOpenBlock == NULL || WmiSetNotificationCallback == NULL) {
        //
        // Not available.
        //
        return STATUS_NOT_SUPPORTED;
    }
 
    //
    // Check to make sure we are not called multiple times.
    //
    ASSERT(DeviceExt->WMIDeviceArrivalNotificationObject == NULL);
 
    //
    // Register WMI callbacks for toaster device arrival events
    //
    wmiGuid = TOASTER_NOTIFY_DEVICE_ARRIVAL_EVENT;
 
    status = WmiOpenBlock(
                 &wmiGuid,
                 WMIGUID_NOTIFICATION,
                 &DeviceExt->WMIDeviceArrivalNotificationObject
                 );
    if (!NT_SUCCESS(status)) {
 
        KdPrint(("Unable to open wmi data block status 0x%x\n", status));
        DeviceExt->WMIDeviceArrivalNotificationObject = NULL;
 
    } else {
 
        status = WmiSetNotificationCallback(
                     DeviceExt->WMIDeviceArrivalNotificationObject,
                     WmiNotificationCallback,
                     DeviceExt
                     );
        if (!NT_SUCCESS(status)) {
            KdPrint(("Unable to register for wmi notification 0x%x\n", status));
            ObDereferenceObject(DeviceExt->WMIDeviceArrivalNotificationObject);
            DeviceExt->WMIDeviceArrivalNotificationObject = NULL;
        }
    }
 
    return status;
}
 
 
VOID
UnregisterForWMINotification(
    PDEVICE_EXTENSION DeviceExt
)
{
    PAGED_CODE();
 
    if (DeviceExt->WMIDeviceArrivalNotificationObject != NULL) {
        ObDereferenceObject(DeviceExt->WMIDeviceArrivalNotificationObject);
        DeviceExt->WMIDeviceArrivalNotificationObject = NULL;
    }
}
 
 
NTSTATUS
GetTargetFriendlyName(
    WDFIOTARGET Target,
    IN WDFMEMORY* TargetName
    )
/*++
 
Routine Description:
 
    Return the friendly name associated with the given device object.
 
Arguments:
 
Return Value:
 
    NT status
 
--*/
{
    NTSTATUS status;
 
    PAGED_CODE();
 
    //
    // First get the length of the string. If the FriendlyName
    // is not there then get the lenght of device description.
    //
    status = WdfIoTargetAllocAndQueryTargetProperty(Target,
                                                    DevicePropertyFriendlyName,
                                                    NonPagedPool,
                                                    WDF_NO_OBJECT_ATTRIBUTES,
                                                    TargetName);
 
    if (!NT_SUCCESS(status) && status != STATUS_INSUFFICIENT_RESOURCES) {
        status = WdfIoTargetAllocAndQueryTargetProperty(Target,
                                                        DevicePropertyDeviceDescription,
                                                        NonPagedPool,
                                                        WDF_NO_OBJECT_ATTRIBUTES,
                                                        TargetName);
 
    }
 
    if (!NT_SUCCESS(status)) {
        KdPrint(("WdfDeviceQueryProperty returned %x\n", status));
    }
 
    return status;
}
 
VOID
WmiNotificationCallback(
    IN PVOID Wnode,
    IN PVOID Context
    )
/*++
 
Routine Description:
 
    WMI calls this function to notify the caller that the specified event has occurred.
 
Arguments:
 
    Wnode - points to the WNODE_EVENT_ITEM structure returned by the driver triggering the event.
 
    Context - points to the value specified in the Context parameter of the
                    IoWMISetNotificationCallback routine.
 
Return Value:
 
    NT status
 
--*/
{
    PWNODE_SINGLE_INSTANCE wnode = (PWNODE_SINGLE_INSTANCE) Wnode;
    WDFMEMORY memory;
    UNICODE_STRING deviceName;
    PDEVICE_OBJECT devobj;
    NTSTATUS status;
    PDEVICE_EXTENSION deviceExt = Context;
    WDFCOLLECTION hCollection = deviceExt->TargetDeviceCollection;
    WDFIOTARGET ioTarget;
    ULONG i;
 
    PAGED_CODE();
 
    WdfWaitLockAcquire(deviceExt->TargetDeviceCollectionLock, NULL);
 
    for(i=0; i< WdfCollectionGetCount(hCollection); i++){
 
        ioTarget = WdfCollectionGetItem(hCollection, i);
 
        //
        // Before calling WdfIoTargetWdmGetTargetDeviceObject, make sure the target is still open.
        // The WdfIoTargetWdmGetXxxDeviceObject APIs can only be called while the target is opened, otherwise
        // they can return undefined values.
        //
        if (GetTargetDeviceInfo(ioTarget)->Opened == FALSE) {
            KdPrint(("WDFIOTARGET %p not in an opened state.\n", ioTarget));
            continue;
        }
 
        devobj = WdfIoTargetWdmGetTargetDeviceObject(ioTarget);
 
        if(devobj &&
            IoWMIDeviceObjectToProviderId(devobj) == wnode->WnodeHeader.ProviderId) {
 
            if( IsEqualGUID( (LPGUID)&(wnode->WnodeHeader.Guid),
                          (LPGUID)&TOASTER_NOTIFY_DEVICE_ARRIVAL_EVENT) ) {
                //
                // found the device. Just for demonstration, get the friendlyname of the
                // target device and print it.
                //
                status = GetTargetFriendlyName(ioTarget, &memory);
                if (!NT_SUCCESS(status)) {
                    KdPrint(("GetDeviceFriendlyName returned %x\n", status));
                    break;
                }
 
                RtlInitUnicodeString(&deviceName, (PWSTR) WdfMemoryGetBuffer(memory, NULL));
                KdPrint(("%wZ fired a device arrival event\n", &deviceName));
 
                //
                // Free the memory allocated by GetDeviceFriendlyName.
                //
                WdfObjectDelete(memory);
 
                break;
 
            } else {
                KdPrint(("Unknown event.\n"));
            }
        }
 
    }
 
    WdfWaitLockRelease(deviceExt->TargetDeviceCollectionLock);
}

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