WinUSB
+ -

WinUSB接口封装

2022-08-17 468 0

主要还是SetupAPI函数的封装



#include <SetupAPI.h>


//获取设备的实例路径
HRESULT
RetrieveDevicePath(
    _Out_bytecap_(BufLen) LPTSTR DevicePath,
    _In_                  ULONG  BufLen,
    _Out_opt_             PBOOL  FailureDeviceNotFound
    );

HRESULT
OpenDevice(
    _Out_     PDEVICE_DATA DeviceData,
    _Out_opt_ PBOOL        FailureDeviceNotFound
    )
/*++

Routine description:

    Open all needed handles to interact with the device.

    If the device has multiple USB interfaces, this function grants access to
    only the first interface.

    If multiple devices have the same device interface GUID, there is no
    guarantee of which one will be returned.

Arguments:

    DeviceData - Struct filled in by this function. The caller should use the
        WinusbHandle to interact with the device, and must pass the struct to
        CloseDevice when finished.

    FailureDeviceNotFound - TRUE when failure is returned due to no devices
        found with the correct device interface (device not connected, driver
        not installed, or device is disabled in Device Manager); FALSE
        otherwise.

Return value:

    HRESULT

--*/
{
    HRESULT hr = S_OK;
    BOOL    bResult;

    DeviceData->HandlesOpen = FALSE;

    hr = RetrieveDevicePath(DeviceData->DevicePath,
                            sizeof(DeviceData->DevicePath),
                            FailureDeviceNotFound);

    if (FAILED(hr)) {

        return hr;
    }
    //CreateFile创建文件句柄
    DeviceData->DeviceHandle = CreateFile(DeviceData->DevicePath,//所要打开的she别名
                                          GENERIC_WRITE | GENERIC_READ,//访问模式
                                          FILE_SHARE_WRITE | FILE_SHARE_READ,//共享模式
                                          NULL,
                                          OPEN_EXISTING,//创建方式
                                          FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,//文件属性和标志
                                          NULL);

    if (INVALID_HANDLE_VALUE == DeviceData->DeviceHandle) {

        hr = HRESULT_FROM_WIN32(GetLastError());
        return hr;
    }
    //WinUsb_Initialize为指定的file创建一个winusb句柄
    bResult = WinUsb_Initialize(DeviceData->DeviceHandle,
                                &DeviceData->WinusbHandle);

    if (FALSE == bResult) {

        hr = HRESULT_FROM_WIN32(GetLastError());
        CloseHandle(DeviceData->DeviceHandle);
        return hr;
    }

    DeviceData->HandlesOpen = TRUE;
    return hr;
}

VOID
CloseDevice(
    _Inout_ PDEVICE_DATA DeviceData
    )
/*++

Routine description:

    Perform required cleanup when the device is no longer needed.

    If OpenDevice failed, do nothing.

Arguments:

    DeviceData - Struct filled in by OpenDevice

Return value:

    None

--*/
{
    if (FALSE == DeviceData->HandlesOpen) {

        //
        // Called on an uninitialized DeviceData
        //
        return;
    }

    WinUsb_Free(DeviceData->WinusbHandle);//释放WinUsb_Initialize创建的句柄
    CloseHandle(DeviceData->DeviceHandle);//释放CreateFile创建的句柄
    DeviceData->HandlesOpen = FALSE;

    return;
}

HRESULT
RetrieveDevicePath(
    _Out_bytecap_(BufLen) LPTSTR DevicePath,
    _In_                  ULONG  BufLen,
    _Out_opt_             PBOOL  FailureDeviceNotFound
    )
/*++

Routine description:

    Retrieve the device path that can be used to open the WinUSB-based device.

    If multiple devices have the same device interface GUID, there is no
    guarantee of which one will be returned.

Arguments:

    DevicePath - On successful return, the path of the device (use with CreateFile).

    BufLen - The size of DevicePath's buffer, in bytes

    FailureDeviceNotFound - TRUE when failure is returned due to no devices
        found with the correct device interface (device not connected, driver
        not installed, or device is disabled in Device Manager); FALSE
        otherwise.

Return value:

    HRESULT

--*/
{
    BOOL                             bResult = FALSE;
    HDEVINFO                         deviceInfo;
    SP_DEVICE_INTERFACE_DATA         interfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = NULL;
    ULONG                            length;
    ULONG                            requiredLength=0;
    HRESULT                          hr;

    if (NULL != FailureDeviceNotFound) {

        *FailureDeviceNotFound = FALSE;
    }

    //
    // Enumerate all devices exposing the interface
    /*获取“设备信息集”的句柄,“设备信息集”是一个阵列,
包含有关与指定的设备接口类匹配的所有安装的设备的信息。
阵列中名为“设备接口”的每个元素对应于一个已经安装并向系统注册的设备。
设备接口类通过传递在 INF 文件中定义的设备接口 GUID 进行标识。该函数将 HDEVINFO 句柄返回到设备信息集。*/
    //

    GUID guid;

     CLSIDFromString(TEXT("{F72FE0D4-CBCB-407d-8814-9ED673D0DD6B}"), &guid);

    deviceInfo = SetupDiGetClassDevs(&guid,//&GUID_DEVINTERFACE_USBApplication2,
                                     NULL,
                                     NULL,
                                     DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    if (deviceInfo == INVALID_HANDLE_VALUE) {

        hr = HRESULT_FROM_WIN32(GetLastError());
        return hr;
    }

    interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    //
    // Get the first interface (index 0) in the result set
    //
    /*枚举设备信息集中的设备接口并获取有关设备接口的信息。
此调用需要以下各项:

已初始化的调用程序分配的 SP_DEVICE_INTERFACE_DATA 结构,其 cbSize 成员设置为结构的大小。
步骤 1 中的 HDEVINFO 句柄。
在 INF 文件中定义的接口 GUID。*/
    bResult = SetupDiEnumDeviceInterfaces(deviceInfo,
                                          NULL,
                                          &guid,//&GUID_DEVINTERFACE_USBApplication2,
                                          0,
                                          &interfaceData);

    if (FALSE == bResult) {

        //
        // We would see this error if no devices were found
        //
        if (ERROR_NO_MORE_ITEMS == GetLastError() &&
            NULL != FailureDeviceNotFound) {

            *FailureDeviceNotFound = TRUE;
        }

        hr = HRESULT_FROM_WIN32(GetLastError());
        SetupDiDestroyDeviceInfoList(deviceInfo);
        return hr;
    }

    //
    // Get the size of the path string
    // We expect to get a failure with insufficient buffer
    //
    /*获取设备接口的详细信息*/
    bResult = SetupDiGetDeviceInterfaceDetail(deviceInfo,
                                              &interfaceData,
                                              NULL,
                                              0,
                                              &requiredLength,
                                              NULL);

    if (FALSE == bResult && ERROR_INSUFFICIENT_BUFFER != GetLastError()) {

        hr = HRESULT_FROM_WIN32(GetLastError());
        SetupDiDestroyDeviceInfoList(deviceInfo);
        return hr;
    }

    //
    // Allocate temporary space for SetupDi structure
    //
    detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)
        LocalAlloc(LMEM_FIXED, requiredLength);

    if (NULL == detailData)
    {
        hr = E_OUTOFMEMORY;
        SetupDiDestroyDeviceInfoList(deviceInfo);
        return hr;
    }

    detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    length = requiredLength;

    //
    // Get the interface's path string
    //
    /*其中包含在SP_DEVICE_INTERFACE_DETAIL_DATA 结构的 DevicePath 成员中接收的设备路径。 
将设备路径传递到 CreateFile 以获取设备的文件句柄。
确保设置了 FILE_FLAG_OVERLAPPED 标志,因为 WinUSB 信赖于此设置。*/
    bResult = SetupDiGetDeviceInterfaceDetail(deviceInfo,
                                              &interfaceData,
                                              detailData,
                                              length,
                                              &requiredLength,
                                              NULL);

    if(FALSE == bResult)
    {
        hr = HRESULT_FROM_WIN32(GetLastError());
        LocalFree(detailData);
        SetupDiDestroyDeviceInfoList(deviceInfo);
        return hr;
    }

    //
    // Give path to the caller. SetupDiGetDeviceInterfaceDetail ensured
    // DevicePath is NULL-terminated.
    //
    hr = StringCbCopy(DevicePath,
                      BufLen,
                      detailData->DevicePath);

    LocalFree(detailData);
    SetupDiDestroyDeviceInfoList(deviceInfo);

    return hr;
}
HID人机交互QQ群:564808376    UAC音频QQ群:218581009    UVC相机QQ群:331552032    BOT&UASP大容量存储QQ群:258159197    STC-USB单片机QQ群:315457461    USB技术交流QQ群2:580684376    USB技术交流QQ群:952873936   

0 篇笔记 写笔记

使用SetupApi函数通过GUID枚举系统中所有关联设备
Windows的设备在驱动中大部分都会IoSetRegisterInterface,这其中一个重要的参数是GUID,这样系统会根据这个GUID给设备创建一个symbolicname,这个名字在应用层可以通过CreateFile打开。这里分享一个在应用层通过SetuApi枚举该类型GUID下的所有设备......
使用WinUSB读写USB设备
Windows为WinUSB设备提供了API,主要通过以下几个步骤访问设备。通过扩展描述符中的GUID查看接口的路径用接口的路径作为参数,调用CreateFile打开接口使用WinUsb_Initialize得到WinUSB句柄通过WinUsb_WritePipe和WinUsb_ReadPipe对......
WinUSB 简介
WinUSB是Windows从Vista操作系统版本以来,新增加的一个USB设备功能驱动程序。使用该驱动,可以将设备变成一个自定义通讯的设备,当然该驱动也可应用于自定义设备驱动程序。WinUSB的组成WinUSB包括两大部分,分别为内核部分和应用层部分:内核部分,主要是WinUSB.sys,该......
WinUSB的安装方法
使用工具zadig进行安装winusb以管理员方式运行zadig-2.4.exe,然后在菜单的选择中选择列出所有设备。这会软件会枚举系统中所有的USB设备,并显示在下列组合对话框中。我们选中我们需要安装winusb的设备,然后点击ReInstallDriver按钮进行安装。如本人选中一个USB扬声器......
windows通过GUID枚举系统中所有USB设备并且显示该设备的所有属性信息
微信群里有同学问怎么获取USB设备的位置信息,这个学名叫做SPDRP_LOCATION_PATHS,刚好自己以前写过这个代码,就分享了一下。代码如下:#include#include #include
典型的设备连接方式监听 PC 上运行 USB Packet Viewer 协议解析软件,通过 Type-C 数据线与抓包设备相连,同时也向抓包设备提供电源。待测试的 USB 主设备通过 Type-C 数据线与抓包设备的 Host 接口相连待测试的 USB 从设备通过 USB-A 数据线与抓包设备......
WinUSB是Microsoft提供的通用USB 驱动程序,适用于从Windows Vista开始但也可用于 Windows XP 的操作系统。它针对的是一次只能由一个应用程序访问的简单设备。它使应用程序能够通过一个简单的软件库直接访问设备。该库提供对设备管道的访问。WinUSB 公开了一个客户端 ......
当USB设备使用WinUSB驱动之后,可以使用WinUSB提供的应用层库winusb.lig来进行应用程序的开发,这样在应用层可以通过Winusb驱动程序与USB设备进行通讯。开发WinUSB应用程序时需要使用VS开发,不过在建一个示例WinUSB应用程序时,首先得安装WDK,安装完成之后会在创建......
WinUSB提供了检测USB设备的速率代码,详见:// Device Information types#define DEVICE_SPEED 0x01// Device Speeds#define LowSpeed 0x01#def......
使用WinUsb_QueryInterfaceSettings函数获取指定接口下的所有信息,然后解析端点信息并打印。struct PIPE_ID{ UCHAR PipeInId; UCHAR PipeOutId;};BOOL QueryDeviceEndpoints (......
向默认端点发出控制请求来与设备通信。除了与接口关联的端点外,所有 USB 设备还有一个默认端点。 默认端点的主要用途是为主机提供可用来配置设备的信息。 不过,设备还可以将默认端点用于设备特定的用途。控制命令包含一个 8 字节设置数据包,其中包括指定特定请求的请求代码和可选的数据缓冲区。 请求代码......
WinUSB 提供了以下用于发送写入请求请求的函数:WinUsb_WritePipe写数据WinUsb_WritePipe分配一个缓冲区并使用要写入到设备的数据进行填充。 如果应用程序未将缓冲区大小设置为管道的策略RAW_IO,则缓冲区大小没有限制。 如有必要,WinUSB 会将缓冲区划分为......
完成对设备的全部所需调用后,需要释放设备的文件句柄和 WinUSB 接口句柄。 为此,请调用以下函数:CloseHandle 释放由 CreateFile 创建的句柄。WinUsb_Free释放设备的 WinUSB 接口句柄,该句柄由 WinUsb_Initialize。VOIDClose......
WinUSB 提供了以下用于接收读取请求请求的函数:WinUsb_ReadPipe调用 WinUsb_ReadPipe 从设备的批量终结点读取数据。 传递设备的 WinUSB 接口句柄、用于批量传入终结点的管道标识符,以及适当大小的空缓冲区。 当函数返回时,缓冲区会包含已从设备读取的数据。 已......
int _tmain(int argc, _TCHAR* argv[]){ GUID guidDeviceInterface = OSR_DEVICE_INTERFACE; //in the INF file BOOL bResult = TRUE; PIPE_ID ......
关注公众号
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!