- Using COM to Develop UMDF Drivers (Part 1 of 2)
- Returns from a COM Method (<tt>HRESULT</tt> Type)/Using UMDF COM Objects
- Obtaining an Interface on a UMDF Object
Obtaining an Interface on a UMDF Object
You can obtain an interface on a UMDF object in one of three ways:
- Option 1. The UMDF runtime passes an interface pointer to one of the driver's callback methods.
- Option 2. The driver creates a new WDF object by calling a UMDF object-creation method.
- Option 3. The driver calls IUnknown::QueryInterface to request a new interface from an existing WDF object.
Option 1: The first case is the simplest. For example, when the UMDF runtime calls a driver's IDriverEntry::OnDeviceAdd method, it passes a pointer to the device object's IWDFDriver interface.
The following example shows this activity:
HRESULT CMyDriver ::OnDeviceAdd( __in IWDFDriver *FxWdfDriver, __in IWDFDeviceInitialize *FxDeviceInit) { // Install the driver in the device stack }
You can then use FxWdfDriver to access the methods on the driver object's IWDFDriver interface. Don't release FxWdfDriver when you're finished with it. The caller ensures that the object remains valid during the scope of the method call.
Option 2: Another way to create a WDF object is by calling the appropriate UMDF object-creation method. For example, to create a request object, call the UMDF device object's IWDFDevice::CreateRequest method. If you look at the UMDF reference in the Windows Driver Kit (WDK), you'll find syntax like that for IWDFDevice::CreateRequest:
HRESULT CreateRequest( IN IUnknown* pCallbackInterface, IN IWdfObject* pParentObject, OUT IWDFIoRequest** ppRequest );
ppRequest is an OUT parameter that provides an address at which the CreateRequest method can store a pointer to the newly created request object's IWDFObject interface. The following procedure and sample show how to handle such parameters, by using a call to CreateRequest by the UMDF's fx2_driver sample as an example. We would declare a variable, pWdfRequest to hold a pointer to IWDFIoRequest.
Then we would pass a reference to pWdfRequest to CreateRequest as follows:
IWDFIoRequest *pWdfRequest = NULL; .... hr = m_FxDevice->CreateRequest(NULL, NULL, &pWdfRequest);
When CreateRequest returns, pWdfRequest holds a pointer to an IWDFIoRequest interface. When the caller has finished with pWdfRequest, it should release the interface pointer by calling IUnknown::Release.
What's Next?
In this article, we've considered the approach and design for using COM to create a user mode driver. In part 2 of this series, we'll wrap up the details for the overall creation of a user mode driver. Part 2 also completes this series of articles on the creation of a user mode driver.