CMyDevice::OnCleanup( _In_ IWDFObject* pWdfObject ) /*++ Routine Description: This device callback method is invoked by the framework when the WdfObject is about to be released by the framework. This will free the context memory associated with the device object. Arguments: pWdfObject - the framework device object for which OnCleanup. Return Value: None --*/ { Trace(TRACE_LEVEL_INFORMATION, "%!FUNC!"); HRESULT hr ; DeviceContext *pContext = NULL; WUDF_SAMPLE_DRIVER_ASSERT(pWdfObject == m_FxDevice); hr = pWdfObject->RetrieveContext((void**)&pContext); if (SUCCEEDED(hr) && (pContext != NULL)) { if (pContext->hostStrA != NULL) { delete pContext->hostStrA; } if (pContext->portStrA != NULL) { delete pContext->portStrA; } delete pContext; } // //CMyDevice has a reference to framework device object via m_Device. //Framework device object has a reference to CMyDevice object via the callbacks. //This leads to circular reference and both the objects can't be destroyed until this circular reference is broken. //To break the circular reference we release the reference to the framework device object here in OnCleanup. m_FxDevice = NULL; }
HRESULT CMyDevice::Initialize( _In_ IWDFDriver* FxDriver, _In_ IWDFDeviceInitialize* FxDeviceInit ) /*++ Routine Description: This method initializes the device callback object and creates the partner device object. The method should perform any device-specific configuration that: * could fail (these can't be done in the constructor) * must be done before the partner object is created -or- * can be done after the partner object is created and which aren't influenced by any device-level parameters the parent (the driver in this case) might set. Arguments: FxDeviceInit - the settings for this device. FxDriver - IWDF Driver for this device. Return Value: status. --*/ { Trace(TRACE_LEVEL_INFORMATION, "%!FUNC!"); CComPtr<IWDFDevice> fxDevice; HRESULT hr; BOOL bFilter = FALSE; // // Configure things like the locking model before we go to create our // partner device. // // // Set the locking model // FxDeviceInit->SetLockingConstraint(None); // // Mark filter if we are a filter // if (bFilter) { FxDeviceInit->SetFilter(); } // // TODO: Any per-device initialization which must be done before // creating the partner object. // // // Create a new FX device object and assign the new callback object to // handle any device level events that occur. // // // 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. // CComPtr<IUnknown> pUnk; HRESULT hrQI = this->QueryInterface(__uuidof(IUnknown),(void**)&pUnk); WUDF_SAMPLE_DRIVER_ASSERT(SUCCEEDED(hrQI)); hr = FxDriver->CreateDevice(FxDeviceInit, pUnk, &fxDevice); // // If that succeeded then set our FxDevice member variable. // if (SUCCEEDED(hr)) { m_FxDevice = fxDevice; } return hr; }
HRESULT CMyQueue::Initialize( _In_ CMyDevice * Device ) /*++ Routine Description: Queue Initialize helper routine. This routine will Create a default parallel queue associated with the Fx device object and pass the IUnknown for this queue Aruments: Device - Device object pointer Return Value: S_OK if Initialize succeeds --*/ { Trace( TRACE_LEVEL_INFORMATION, "%!FUNC!" ); CComPtr<IWDFIoQueue> fxQueue; HRESULT hr; m_Device = Device; // // Create the I/O Queue object. // { CComPtr<IUnknown> pUnk; HRESULT hrQI = this->QueryInterface(__uuidof(IUnknown),(void**)&pUnk); WUDF_SAMPLE_DRIVER_ASSERT(SUCCEEDED(hrQI)); hr = m_Device->GetFxDevice()->CreateIoQueue( pUnk, TRUE, WdfIoQueueDispatchParallel, TRUE, FALSE, &fxQueue ); } if (FAILED(hr)) { Trace( TRACE_LEVEL_ERROR, "Failed to initialize driver queue %!hresult!", hr ); goto Exit; } m_FxQueue = fxQueue; Exit: return hr; }