The WinUSB user-mode library uses device interface classes to communicate with the kernel-mode USB stack. The INF file that loads winusb.sys specifies a device interface class GUID in the registry. When winusb.sys loads, it calls IoRegisterDeviceInterface to register a device interface that belongs to the device interface class that is specified in the registry.

User-mode software must call SetupDiGetClassDevs to enumerate the registered device interfaces that are associated with one of these device interface classes. The user-mode software must then pass the symbolic link of a device interface to the WinUsb_Initialize routine to obtain a WinUSB handle for the device interface.

The code example in this topic illustrates how user-mode software can enumerate device interfaces and obtain a WinUSB handle. For this example, assume that the INF file stores a device interface class GUID in the registry as follows.

HKR,,DeviceInterfaceGUIDs, 0x10000,"{058815B2-9805-47d3-B7D5-ABC464D3CA06}"

The example user-mode software calls SetupDiGetClassDevs to query for information about all of the registered device interfaces in the device interface class that is associated with this GUID. SetupDiGetClassDevs then returns a handle to a device information set that contains information about the device interfaces.

#include initguid.h
#include setupapi.h

// Example GUID definition. This GUID must match 
// the GUID that was specified in the INF file 
// that loaded winusb.sys.

DEFINE_GUID(InterfaceClassGuidConstant,0x058815B2, 0x9805, 0x47d3, 0xB7, 0xD5, 0xAB, 0xC4, 0x64, 0xD3, 0xCA, 0x06);
GUID InterfaceClassGuid = InterfaceClassGuidConstant;

// Define a handle to a device information set.
HDEVINFO  hdevClassInfo;

// Retrieve a device information set.
hdevClassInfo = SetupDiGetClassDevs
        (&InterfaceClassGuid, NULL, NULL,
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hdevClassInfo == INVALID_HANDLE_VALUE) { 
        // ERROR }

The call to SetupDiGetClassDevs in the preceding code
example requests registered device interfaces (DIGCF_DEVICEINTERFACE) for the
device interface class that the InterfaceClassGuid variable specifies.
Only device interfaces for presently installed devices (DIGCF_PRESENT) are
returned.

Next, the example application must iteratively call SetupDiEnumDeviceInterfaces
to determine how many elements are in the device information set.

// Initialize variables.
SP_DEVICE_INTERFACE_DATA  DeviceData;
DeviceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

for (nMemberIndex = 0; TRUE; nMemberIndex++) {
    nStatus = SetupDiEnumDeviceInterfaces (hdevClassInfo, NULL,
    (LPGUID)&InterfaceGuid, nMemberIndex,
    &DeviceData);
    if (nStatus != TRUE) break;
}

Now, the example application must enumerate the registered device interfaces
that are associated with the device interface class. The example application
calls SetupDiEnumDeviceInterfaces iteratively, once again; but
now it retrieves hardware identifiers (IDs) and symbolic links for each
registered interface. The application will use the symbolic links to obtain a
WinUSB device interface handle.

// Allocate array that will hold information about the devices that
// are associated with this interface class.
struct DeviceData {
  TCHAR  *HardwareId;
  TCHAR  *Path; // symbolic link
  TCHAR  *FriendlyName;
  DWORD  DeviceInstance;
} *DeviceList;
DeviceList = (struct DeviceData *) malloc 
    ((nMemberIndex + 1) * sizeof(struct DeviceData));
if (DeviceList == NULL) { // ERROR }

// Enumerate devices that are associated with the interface.
for (i = 0; i < nMemberIndex; i++) {
    nStatus = SetupDiEnumDeviceInterfaces
                 (hdevClassInfo, NULL,
                 (LPGUID)&InterfaceGuid, i,
                 &DeviceData);
    if (nStatus != TRUE) break;

// Retrieve the size of the device data.
    nStatus = SetupDiGetDeviceInterfaceDetail
                 (hdevClassInfo, &DeviceData, 
                 NULL, 0, &nSize, NULL);                  
    if (nStatus != TRUE) break;

// Allocate memory for the device detail buffer. 
    SP_DEVICE_INTERFACE_DATA  pBuffer;
    pBuffer = (PSP_INTERFACE_DEVICE_DETAIL_DATA)
              malloc (nSize));
    if (pBuffer == NULL) { // ERROR }

// Initialize variables.
    DeviceData.cbSize =
        sizeof(SP_INTERFACE_DEVICE_DATA);
    nStatus = SetupDiGetDeviceInterfaceDetail
                 (hdevClassInfo, &DeviceData,
                 pBuffer, nSize, NULL,&DeviceData));
    if (nStatus = FALSE)) { // ERROR }

//****************************************
    // Save the device interface path: 
    //           This path can be used to open 
    //           the interface with CreateFile.
    //****************************************

// Calculate the length of the path string. 
    // Add 1 for the terminating NULL character.
    nLen = strlen(DeviceData.DevicePath) + 1; 
    DeviceList[i].Path = 
       (TCHAR *) malloc (nLen * sizeof(TCHAR));
    StringCchCopy(DeviceList[i].Path, nLen, 
       DeviceData.DevicePath);

// Save the device instance.
    DeviceList[i].DeviceInstance = 
       DeviceInfoData.DevInst;

//****************************************
    // Retrieve registry values.
    //****************************************

// Initialize variables that are used in registry
    // operations.
    SP_DEVINFO_DATA DevInfoData;

//****************************************
    // Retrieve the device friendly name.
    //****************************************

// Query for the size of the friendly name.
    nStatus = SetupDiGetDeviceRegistryProperty
                (hdevClassInfo, &DevnfoData,
                SPDRP_FRIENDLYNAME, &dwRegType,
                NULL, 0, &dwRegSize);
    if (nStatus == FALSE) { // ERROR }

// Allocate buffer for the friendly name.
    pBuffer = (TCHAR *) malloc 
                  (dwRegSize * sizeof(TCHAR));
    if (pBuffer =  == NULL) { // ERROR }

// Retrieve the friendly name.
    nStatus = SetupDiGetDeviceRegistryProperty
                (hdevClassInfo, &DevInfoData,
                SPDRP_FRIENDLYNAME, NULL,
                (PBYTE) *pBuffer, dwRegSize, NULL);
    if (nStatus == FALSE) { // ERROR }

// Store the friendly name for this device.
    DeviceList[i].FriendlyName = pBuffer;

// ************************************
    // Retrieve the hardware ID.
    // ************************************

// Query for the size of the hardware ID.
    nStatus = SetupDiGetDeviceRegistryProperty
                (hdevClassInfo, &DevnfoData,
                SPDRP_HARDWAREID, &dwRegType,
                NULL, 0, &dwRegSize);
    if (nStatus == FALSE) { // ERROR }

// Allocate a buffer for the hardware ID.
    pBuffer = (TCHAR *) malloc 
       (dwRegSize * sizeof(TCHAR));
    if (pBuffer =  == NULL) { // ERROR }

// Retrieve the hardware ID.
    nStatus = SetupDiGetDeviceRegistryProperty
                (hdevClassInfo, &DevInfoData,
                SPDRP_HARDWAREID, NULL,
                (PBYTE) *pBuffer, dwRegSize, NULL);
    if (nStatus == FALSE) { // ERROR }

// Store the hardware ID for this device.
    DeviceList[i].HardwareId = pBuffer;
}

The example software next opens the first device interface in the device
interface list. The device interface must be opened with the overlapped
flag.

// Create a symbolic link to device interface
LPCTSTR Path; 
HANDLE DeviceInterfaceHandle;

Path = DeviceList[0].Path;
DeviceInterfaceHandle = CreateFile (Path,
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ |
                        FILE_SHARE_WRITE,
                        NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_OVERLAPPED,
                        NULL);
if (DeviceInterfaceHandle == INVALID_HANDLE_VALUE) { // ERROR }

After the example software opens the device interface, the application must
call WinUsb_Initialize to obtain a WinUSB handle.

WINUSB_INTERFACE_HANDLE WinUSBHandle;
nStatus = WinUsb_Initialize (DeviceInterfaceHandle, &WinUSBHandle);
if (nStatus = FALSE) { // ERROR }

The example software manages a composite USB device, whose first interface
(interface 0) contains bulk transfer endpoints. In the following code example,
the example software:

  1. Calls WinUsb_QueryInterfaceSettings to retrieve the
    interface descriptor (USB_INTERFACE_DESCRIPTOR)
    for interface 0.
  2. Calls WinUsb_QueryPipe to query for information (WINUSB_PIPE_INFORMATION) about the pipe for endpoint 0.
  3. Verifies that the pipe is a bulk transfer pipe.
  4. Calls WinUsb_SetPipePolicy to set the time-out
    interval for the pipe.

// ********************************************
// The first interface is for bulk transfers.
// ********************************************

USB_INTERFACE_DESCRIPTOR interface;
nStatus = WinUsb_QueryInterfaceSettings
            (WinUSBHandle, 0, &interface);
if (nStatus = FALSE) { // ERROR }

WINUSB_PIPE_INFORMATION pipe;
nStatus = WinUsb_QueryPipe (WinUSBHandle, 0, 
            (UCHAR) 0, &pipe);
if (nStatus == FALSE) { // ERROR }

Verify that the pipe type is set to the correct USBD_PIPE_TYPE
enumeration value before calling WinUsb_SetPipePolicy.

if (pipe.PipeType != UsbdPipeTypeBulk) { // ERROR }

// Set the transfer time-out interval for endpoint 0
// to 10000 milliseconds.
UCHAR timeout = 10000;
nStatus = WinUsb_SetPipePolicy (WinUSBHandle,
            pipe.PipeId, PIPE_TRANSFER_TIMEOUT,
            sizeof (timeout), &timeout);
if (nStatus == FALSE) { // ERROR }

The second interface (interface 1) contains interrupt transfer endpoints. In
the following code example, the example software

  1. Calls WinUsb_GetAssociatedInterface to obtain a
    handle to interface 1.
  2. Calls WinUsb_QueryInterfaceSettings to retrieve the
    interface descriptor for interface 1.
  3. Calls WinUsb_QueryPipe to query for information about the
    pipe for endpoint 0.
  4. Verifies that the pipe is an interrupt transfer pipe.

// *************************************************
// The second interface is for interrupt transfers
// *************************************************

WINUSB_INTERFACE_HANDLE WinUSBHandleAlt;
nStatus = WinUsb_GetAssociatedInterface (WinUSBHandle, 0, 
                        &WinUSBHandleAlt);
if (nStatus == FALSE) { // ERROR }

nStatus = WinUsb_QueryInterfaceSettings (WinUSBHandleAlt, 0,
                        &interface);
if (nStatus == FALSE) { // ERROR }

nStatus = WinUsb_QueryPipe (WinUSBHandleAlt, 0, (UCHAR) 0, &pipe);
if (nStatus == FALSE) { // ERROR }

if (pipe.PipeType != UsbdPipeTypeInterrupt) { // ERROR }

MSDN WinUSB Example的更多相关文章

  1. Visual Studio 2010的MSDN帮助文档离线使用

    如果没有在安装vs过程中安装帮助,也可通过Visual Studio帮助菜单中的Manage Help Settings来对帮助进行设置或安装. 可以选择从磁盘安装内容,如果选择从磁盘安装可能会要求提 ...

  2. MSDN文档篇

    很多人网上下载3~10G不等的MSDN文档,发现,下载完成了不会用 很多人每次都得在线下载文档,手上万千PC,都重新下载不是得疯了? so==> 先看几张图 推荐一个工具:https://vsh ...

  3. "Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated )

    "Installation failed !" in GUI but not in CLI (/usr/bin/winusb: line 78: 18265 Terminated ...

  4. 使用SHFB(Sandcastle Help File Builder)建立MSDN风格的代码文档

    使用SHFB(Sandcastle Help File Builder)建立MSDN风格的代码文档 下载地址:http://sandcastle.codeplex.com/ 下载地址2:http:// ...

  5. MSDN杂志上Wix相关的文章

    使用 MSBuild 和 Windows Installer XML 执行自动发布: http://msdn.microsoft.com/zh-cn/magazine/cc163456.aspx 用于 ...

  6. 使用 Async 和 Await 的异步编程(C# 和 Visual Basic)[msdn.microsoft.com]

    看到Microsoft官方一篇关于异步编程的文章,感觉挺好,不敢独享,分享给大家. 原文地址:https://msdn.microsoft.com/zh-cn/library/hh191443.asp ...

  7. [已解决]从微软合作伙伴资源和MSDN下载系统和软件Microsoft download Manager无效

    有个itellyou,更新了所有MSDN的软件包.如果自己有微软的注册账户,还是从微软官网下载比较好.而且对自己账户里的系统和itellyou里的做了对比.发现SHA1码不相同,估计官方分配的序列号也 ...

  8. diff/merge configuration in Team Foundation - common Command and Argument values - MSDN Blogs

    One of the extensibility points we have in Team Foundation V1 is that you can configure any other di ...

  9. 源码网站(msdn.itellyou.cn) good

    verysource 100万源码http://www.verysource.com/category/delphi-vcl/ MSDN DOWNLOADhttp://msdn.itellyou.cn ...

随机推荐

  1. TC-572-D1L2 (双向搜索+记忆化)

    solution: 这一题是比较难实现的双向搜索题:(字符串+双向搜索+hash记忆化) 我们可以先把K的前半部分枚举出来,并将得出的所有结果和题目给的n个数的每一个数的前半部分都比对一遍,得到它和每 ...

  2. 使用 CasperJS 构建 Web 爬虫

    转载:https://www.oschina.net/translate/building-your-own-web-scraper-in-nodejs 从你的应用中收集数据有时候可能有点困难和艰辛. ...

  3. 用Python连接SQLServer抓取分析数据、监控 (pymssql)

    Python 环境:python3 服务器环境: centos6.5 数据库: Mysql 大概流程:在装有Python服务器,利用pymssql库连接MSSQL生产数据库取出数据然后写进mysql数 ...

  4. springcloud使用Zuul构建微服务网关入门

    为什么要使用微服务网关 不同的微服务一般会经过不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求. 如果让客户端直接与各个微服务通信,会有以下的问题: 客户端会多次请求不同的微 ...

  5. 转:vue-router 2.0 常用基础知识点之router.push()

    转载地址:http://www.jianshu.com/p/ee7ff3d1d93d router.push(location) 除了使用 <router-link> 创建 a 标签来定义 ...

  6. SSH(Struts、Spring、Hibernate)三大框架整合

    1. 新建数据库ssh_db -> 新建表user_tb(id为主键,自动递增) 2. 导入jar包(struts.hibernate 和 spring) 3. 注册页面reg.jsp,将表单的 ...

  7. finall和set和构造方法的参数意义

    package demo04; /* * 形状 */public abstract class Shape { // 求周长 public abstract double getGrith(); // ...

  8. 数学之美——HMM模型(二)解码和Forward算法

    上一篇讨论了HMM的基本概念和一些性质,HMM在现实中还是比较常见的,因此也带来一了一系列的HMM应用问题.HMM应用主要面向三个方面:预测.解码和学习.这篇主要讨论预测. 简单来说,预测就是给定HM ...

  9. Sqoop的安装及简单使用

    SQOOP是用于对数据进行导入导出的. (1)把MySQL.Oracle等数据库中的数据导入到HDFS.Hive.HBase中   (2)把HDFS.Hive.HBase中的数据导出到MySQL.Or ...

  10. Thinkphp分布式数据库连接代码分析

    Thinkphp作为国内的一款流行框架,相信使用的人一定不在少数.本篇我们来分析一下Thinkphp中比较重要的一部分——分布式数据库的连接. 当然了,我们在这里不是去将如何使用模型去对数据库进行增删 ...