共用方式為


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 會傳回下列其中一個值:

傳回碼 Description
S_OK

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

E_OUTOFMEMORY

CreateUsbTargetDevice 發生配置失敗。

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

備註

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

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

注意CreateUsbTargetDevice 會繼承 IWDFIoTarget 介面的所有方法。
 
若要在裝置堆疊中使用新建立的USB I/O目標對象,安裝UMDF驅動程式的INF檔案必須包含 UmdfDispatcher 指示詞,並將 UmdfDispatcher 設定為 WinUsb (UmdfDispatcher=WinUsb) DDInstall.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