Sample Code
Windows Driver Samples/ Toaster Sample Driver/ C++/ umdf/ func/ Driver.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 | /*++ Copyright (c) Microsoft Corporation, All Rights Reserved Module Name: Driver.cpp Abstract: This file contains the implementation for the driver object. Environment: Windows User-Mode Driver Framework (WUDF) --*/ #include "stdafx.h" #include "Driver.h" #include "Device.h" #include "Queue.h" #include "internal.h" #include "driver.tmh" //Idle setting for the Toaster device #define IDLEWAKE_TIMEOUT_MSEC 6000 HRESULT CDriver::OnDeviceAdd( _In_ IWDFDriver* pDriver, _In_ IWDFDeviceInitialize* pDeviceInit ) /*++ Routine Description: The framework calls this function when a device is being added to the driver stack. Arguments: IWDFDriver - Framework interface. The driver uses this interface to create device objects. IWDFDeviceInitialize - Framework interface. The driver uses this interface to set device parameters before creating the device obeject. Return Value: HRESULT S_OK - Device added successfully --*/ { IUnknown *pDeviceCallback = NULL; IWDFDevice *pIWDFDevice = NULL; IWDFDevice2 *pIWDFDevice2 = NULL; IUnknown *pIUnkQueue = NULL; // // UMDF Toaster is a function driver so set is as the power policy owner (PPO) // pDeviceInit->SetPowerPolicyOwnership(TRUE); // // Create our device callback object. // HRESULT hr = CDevice::CreateInstance(&pDeviceCallback); // // Ask the framework to create a device object for us. // We pass in the callback object and device init object // as creation parameters. // if (SUCCEEDED(hr)) { hr = pDriver->CreateDevice(pDeviceInit, pDeviceCallback, &pIWDFDevice); } // // Create the queue callback object. // if (SUCCEEDED(hr)) { hr = CQueue::CreateInstance(&pIUnkQueue); } // // Configure the default queue. We pass in our queue callback // object to inform the framework about the callbacks we want. // if (SUCCEEDED(hr)) { IWDFIoQueue * pDefaultQueue = NULL; hr = pIWDFDevice->CreateIoQueue( pIUnkQueue, TRUE, // bDefaultQueue WdfIoQueueDispatchParallel, TRUE, // bPowerManaged FALSE, // bAllowZeroLengthRequests &pDefaultQueue); SAFE_RELEASE(pDefaultQueue); } // // Enable the device interface. // if (SUCCEEDED(hr)) { hr = pIWDFDevice->CreateDeviceInterface(&GUID_DEVINTERFACE_TOASTER, NULL); } // // IWDFDevice2 interface is an extension of IWDFDevice interface that enables // Idle and Wake support. // // // Get a pointer to IWDFDevice2 interface // if (SUCCEEDED(hr)) { hr = pIWDFDevice->QueryInterface(__uuidof(IWDFDevice2), ( void **) &pIWDFDevice2); } // // Since this is a virtual device we tell the framework that we cannot wake // ourself if we sleep in S0. Only way the device can be brought to D0 is if // the device recieves an I/O from the system. // if (SUCCEEDED(hr)) { hr = pIWDFDevice2->AssignS0IdleSettings( IdleCannotWakeFromS0, PowerDeviceD3, //the lowest-powered device sleeping state IDLEWAKE_TIMEOUT_MSEC, //idle timeout IdleAllowUserControl, //user can control the device's idle behavior. WdfTrue); } // // TODO: Add the Idle and Wake suupport specific for your hardware // SAFE_RELEASE(pDeviceCallback); SAFE_RELEASE(pIWDFDevice); SAFE_RELEASE(pIWDFDevice2); SAFE_RELEASE(pIUnkQueue); return hr; } VOID CDriver::OnDeinitialize( _In_ IWDFDriver * /* pDriver */ ) /*++ Routine Description: The framework calls this function just before de-initializing itself. All WDF framework resources should be released by driver before returning from this call. Arguments: Return Value: --*/ { return ; } HRESULT CDriver::OnInitialize( _In_ IWDFDriver * /* pDriver */ ) /*++ Routine Description: The framework calls this function just after loading the driver. The driver can perform any global, device independent intialization in this routine. Arguments: Return Value: --*/ { return S_OK; } |
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