Sample Code
Windows Driver Samples/ Toaster Sample Driver/ C++/ umdf/ Toastmon/ Device.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 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 | /*++ Copyright (C) Microsoft Corporation, All Rights Reserved. Module Name: Device.cpp Abstract: This module contains the implementation of the UMDF sample driver's device callback object. This sample demonstrates how to register for PnP event notification for an interface class, and how to handle arrival events. Environment: Windows User-Mode Driver Framework (WUDF) --*/ #include "internal.h" #include "device.tmh" HRESULT CMyDevice::CreateInstance( _In_ IWDFDriver * FxDriver, _In_ IWDFDeviceInitialize * FxDeviceInit, _Out_ PCMyDevice * MyDevice ) /*++ Routine Description: This method creates and initializs an instance of the driver's device callback object. Arguments: FxDeviceInit - the settings for the device. MyDevice - a location to store the referenced pointer to the device object. Return Value: Status --*/ { PCMyDevice myDevice; HRESULT hr; // // Allocate a new instance of the device class. // myDevice = new CMyDevice(); if (NULL == myDevice) { return E_OUTOFMEMORY; } // // Initialize the instance. // hr = myDevice->Initialize(FxDriver, FxDeviceInit); if (SUCCEEDED(hr)) { *MyDevice = myDevice; } else { myDevice->Release(); } return hr; } HRESULT CMyDevice::Initialize( _In_ IWDFDriver * FxDriver, _In_ IWDFDeviceInitialize * FxDeviceInit ) /*++ Routine Description: This method initializes the device callback object and creates the partner device object. Then it registers for toaster device notifications. Arguments: FxDeviceInit - the settings for this device. Return Value: status. --*/ { CComPtr<IWDFDevice> fxDevice; HRESULT hr; // // Save a weak reference to the Fx driver object. We'll need it to create // CMyRemoteTarget objects. // m_FxDriver = FxDriver; // // QueryIUnknown references the IUnknown interface that it returns // (which is the same as referencing the device). We pass that to // CreateDevice, which takes its own reference if everything works. // { IUnknown *unknown = this ->QueryIUnknown(); // // Create a new FX device object and assign the new callback object to // handle any device level events that occur. // hr = FxDriver->CreateDevice(FxDeviceInit, unknown, &fxDevice); unknown->Release(); } // // If that succeeded then set our FxDevice member variable. // CComPtr<IWDFDevice2> fxDevice2; if (SUCCEEDED(hr)) { // // Q.I. for the latest version of this interface. // hr = fxDevice->QueryInterface(IID_PPV_ARGS(&fxDevice2)); } if (SUCCEEDED(hr)) { // // Store a weak reference to the IWDFDevice2 interface. Since this object // is partnered with the framework object they have the same lifespan - // there is no need for an additional reference. // m_FxDevice = fxDevice2; } return hr; } HRESULT CMyDevice::Configure( VOID ) /*++ Routine Description: This method is called after the device callback object has been initialized and returned to the driver. Return Value: status --*/ { HRESULT hr = S_OK; // // Register for TOASTER device interface change notification. // We will get OnRemoteInterfaceArrival() calls when a remote toaster // device is started. // // Arrival notification will be sent for all existing and future toaster // devices. // // The framework will take care of unregistration when the device unloads. // hr = m_FxDevice->RegisterRemoteInterfaceNotification(&GUID_DEVINTERFACE_TOASTER, true ); return hr; } HRESULT CMyDevice::QueryInterface( _In_ REFIID InterfaceId, _Out_ PVOID *Object ) /*++ Routine Description: This method is called to get a pointer to one of the object's callback interfaces. Arguments: InterfaceId - the interface being requested Object - a location to store the interface pointer if successful Return Value: S_OK or E_NOINTERFACE --*/ { HRESULT hr; if (IsEqualIID(InterfaceId, __uuidof(IPnpCallbackRemoteInterfaceNotification))) { *Object = QueryIPnpCallbackRemoteInterfaceNotification(); hr = S_OK; } else { hr = CUnknown::QueryInterface(InterfaceId, Object); } return hr; } // // IPnpCallbackRemoteInterfaceNotification // void STDMETHODCALLTYPE CMyDevice::OnRemoteInterfaceArrival( _In_ IWDFRemoteInterfaceInitialize * FxRemoteInterfaceInit ) /*++ Routine Description: This method is called by the framework when a new remote interface has come online. These calls will only occur one at a time. Arguments: FxRemoteInterfaceInit - An identifier for the remote interface. --*/ { HRESULT hr = S_OK; // // Create a new FX remote interface object and assign a NULL callback // object since we don't care to handle any remote interface level events // that occur. // CComPtr<IWDFRemoteInterface> fxRemoteInterface; hr = m_FxDevice->CreateRemoteInterface(FxRemoteInterfaceInit, NULL, &fxRemoteInterface); // // Create an instance of CMyRemoteTarget which will open the remote device // and post I/O requests to it. // PCMyRemoteTarget myRemoteTarget = NULL; if (SUCCEEDED(hr)) { hr = CMyRemoteTarget::CreateInstance( this , m_FxDriver, m_FxDevice, fxRemoteInterface, &myRemoteTarget); } if (SUCCEEDED(hr)) { if (myRemoteTarget != NULL) { // // Add to our list // InsertHeadList(&m_MyRemoteTargets, &myRemoteTarget->m_Entry); // // Release, since framework will keep a reference // myRemoteTarget->Release(); } } if (FAILED(hr)) { if (fxRemoteInterface != NULL) { // // We failed to create the CMyRemoteTarget, delete the // RemoteInterface object // fxRemoteInterface->DeleteWdfObject(); } } } |
Our Services
-
What our customers say about us?
Read our customer testimonials to find out why our clients keep returning for their projects.
View Testimonials