共用方式為


IWDFUsbTargetFactory::CreateUsbTargetDevice 方法 (wudfusb.h)

[警告: UMDF 2 是最新版本的 UMDF,並取代 UMDF 1。 所有新的UMDF驅動程式都應該使用UMDF 2撰寫。 未將新功能新增至 UMDF 1,而且較新版本的 Windows 10 上對 UMDF 1 的支援有限。 通用 Windows 驅動程式必須使用 UMDF 2。 如需詳細資訊,請參閱 開始使用 UMDF。]

CreateUsbTargetDevice 方法會建立也是 I/O 目標的 USB 裝置物件。

語法

HRESULT CreateUsbTargetDevice(
  [out] IWDFUsbTargetDevice **ppDevice
);

參數

[out] ppDevice

緩衝區的指標,接收USB目標裝置物件的 IWDFUsbTargetDevice 介面指標。

傳回值

CreateUsbTargetDevice 會傳回下列其中一個值:

傳回碼 描述
S_OK

CreateUsbTargetDevice 已成功建立也是 I/O 目標的 USB 裝置物件。

E_OUTOFMEMORY

CreateUsbTargetDevice 發生配置失敗。

Winerror.h 中定義的錯誤碼
這個值對應於 WinUsb_Initialize 函式傳回的錯誤碼。

言論

UMDF 驅動程式應該釋放 IWDFUsbTargetDevice 介面指標,CreateUsbTargetDevice 方法在 ppDevice 參數中使用 介面完成時傳回。

如果需要與建立的 I/O 目標對象相關聯的檔案對象,驅動程式應該呼叫 IWDFIoTarget::GetTargetFile 方法。 如需此檔案物件的詳細資訊,請參閱 USB I/O 目標建立檔案

NoteCreateUsbTargetDevice 會繼承 IWDFIoTarget 介面 的所有方法。
 
若要在裝置堆疊中使用新建立的USB I/O目標對象,安裝UMDF驅動程式的INF檔案必須包含 UmdfDispatcher 指示詞,並將 UmdfDispatcher 設定為 WinUsbUmdfDispatcher=WinUsbDDInstall.WDF 區段中。 需要 UmdfDispatcher,才能通知 UMDF 平臺,它可以允許存取 USB I/O 目標。 如需 UmdfDispatcher的詳細資訊,請參閱 指定 WDF 指示詞

例子

下列程式代碼範例示範如何在 UMDF 驅動程式的 IPnpCallbackHardware::OnPrepareHardware 方法實作中建立及使用 USB 裝置物件。

HRESULT
CUmdfHidDevice::OnPrepareHardware(
    __in IWDFDevice* WdfDevice
    )
{
    CComPtr<IWDFUsbTargetFactory> factory;
    USB_INTERFACE_DESCRIPTOR interfaceDescriptor;
    bool hidInterfaceFound = false;
    PUSB_HID_DESCRIPTOR hidDescriptor;
    NTSTATUS status;
    HRESULT hr = S_OK;
    //
    // Get the USB I/O target factory interface.
    //
    hr = WdfDevice->QueryInterface(IID_PPV_ARGS(&factory));
    //
    // Create the USB I/O target.
    //
    hr = factory->CreateUsbTargetDevice(&m_UsbTargetDevice);
    //
    // Get the configuration descriptor for the target device.
    //
    if (SUCCEEDED(hr))
    {
        hr = RetrieveConfigDescriptor(&m_ConfigDescriptor, 
                                      &m_ConfigDescriptorCb);
    }
    //
    // Iterate through the interfaces on the device and find the HID interface.
    //
    if (SUCCEEDED(hr))
    {
        CComPtr<IWDFUsbInterface> usbInterface;
        UCHAR index;
        bool found = true;
        for (index = 0; index < m_ConfigDescriptor->bNumInterfaces; index += 1)
        {
            hr = m_UsbTargetDevice->RetrieveUsbInterface(index, &usbInterface);
            if (SUCCEEDED(hr))
            {
                usbInterface->GetInterfaceDescriptor(&interfaceDescriptor);
                if (interfaceDescriptor.bInterfaceClass == 0x3)
                {
                    hidInterfaceFound = true;
                    break;
                }
            }
            else
            {
                break;
            }
        }
        if (SUCCEEDED(hr) && (hidInterfaceFound == false))
        {
            hr = E_FAIL;
        }
    }
    //
    // Get the HID descriptor associated with this interface.
    //
    if (SUCCEEDED(hr))
    {
        hr = ParseHidDescriptor(
                                m_ConfigDescriptor,
                                m_ConfigDescriptorCb,
                                interfaceDescriptor.bInterfaceNumber
                                );
    }
    //
    // Process the HID information from the device and setup 
    // the collection data structures.
    //
    if (SUCCEEDED(hr))
    {
        hr = SetupCollections();
    }
    return hr;
}

要求

要求 價值
終止支援 UMDF 2.0 和更新版本中無法使用。
目標平臺 桌面
最低 UMDF 版本 1.5
標頭 wudfusb.h (包括 Wudfusb.h)
DLL WUDFx.dll

另請參閱

IWDFIoTarget

IWDFIoTarget::GetTargetFile

IWDFUsbTargetDevice

IWDFUsbTargetFactory

WinUsb_Initialize