首页 新闻 搜索 专区 学院

WinUsb_Initialize error6

0
悬赏园豆:80 [已解决问题] 解决于 2013-06-19 17:50

求助各位:

我想给电脑安装一个自己做的设备的winusb驱动,目前的状况是,上位机已经可以通过INF识别设备,也就是在设备管理器中已经有该设备的标志,但该设备还不在通用串行总线的结点之下,我查了手册,说:在设备枚举过程中,USB 驱动程序堆栈会从设备读取兼容 ID。如果兼容 ID 是 "WINUSB",Windows 会将其用作设备标识符,并在更新的内置 Winusb.inf 中查找匹配,然后将 Winusb.sys 作为设备的功能驱动程序加载。

所以我开始做设备枚举,按照[http://msdn.microsoft.com/zh-cn/library/windows/hardware/ff540174(v=vs.85).aspx]中的代码,已完成 

创建设备的文件句柄一步,函数的返回值已为1,而且没有任何error信息,但第二步WinUsb_Initialize时就出错了,错误代码为error6,我查了一下手册,是ERROR_INVALID_HANDLE,说是 FILE_FLAG_OVERLAPPED 没设置,但我的CreateFile是设置了

CreateFile (
        lpDevicePath,
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_OVERLAPPED,
        NULL);

两段代码如下:

#include "stdafx.h"

BOOL GetDeviceHandle (GUID guidDeviceInterface, PHANDLE hDeviceHandle);
BOOL GetWinUSBHandle(HANDLE hDeviceHandle, PWINUSB_INTERFACE_HANDLE phWinUSBHandle);

int _tmain(int argc, _TCHAR* argv[])
{
    /*9d7debbc-c85d-11d1-9eb4-006008c3a19a*/
    static const GUID OSR_DEVICE_INTERFACE = { 0x9d7debbc, 0xc85d, 0x11d1, { 0x9e, 0xb4, 0x00, 0x60, 0x08, 0xc3, 0xa1, 0x9a } };
    
    HANDLE h;
    PHANDLE hDeviceHandle = &h;
    BOOL getDeviceHandle = GetDeviceHandle (OSR_DEVICE_INTERFACE, hDeviceHandle);

    PVOID p;
    PWINUSB_INTERFACE_HANDLE phWinUSBHandle = &p;
    BOOL getWinUSBHandle = GetWinUSBHandle(hDeviceHandle, phWinUSBHandle);
    
    std::cout << "getDeviceHandle = " << getDeviceHandle << std::endl;
    std::cout << "getWinUSBHandle = " << getWinUSBHandle << std::endl;
    
    system("PAUSE");
    return 0;
    
}
/**********************************************************************************/
/*下面的示例代码创建一个支持对设备进行同步读写访问的文件句柄。有关如何打开异步 I/O 的文件句柄的详细信息,请参阅 CreateFile 中的“备注”部分。*/
/**********************************************************************************/
BOOL GetDeviceHandle (GUID guidDeviceInterface, PHANDLE hDeviceHandle)
{
    if (guidDeviceInterface==GUID_NULL)
    {
        return FALSE;
    }

    BOOL bResult = TRUE;
    HDEVINFO hDeviceInfo;
    SP_DEVINFO_DATA DeviceInfoData;

    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = NULL;

    ULONG requiredLength=0;

    LPTSTR lpDevicePath = NULL;

    DWORD index = 0;

    // Get information about all the installed devices for the specified
    // device interface class.
    hDeviceInfo = SetupDiGetClassDevs( 
        &guidDeviceInterface,
        NULL, 
        NULL,
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    if (hDeviceInfo == INVALID_HANDLE_VALUE) 
    { 
        // ERROR 
        printf("Error SetupDiGetClassDevs: %d.\n", GetLastError());
        goto done;
    }

    //Enumerate all the device interfaces in the device information set.
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    for (index = 0; SetupDiEnumDeviceInfo(hDeviceInfo, index, &DeviceInfoData); index++)
    {
        //Reset for this iteration
        if (lpDevicePath)
        {
            LocalFree(lpDevicePath);
        }
        if (pInterfaceDetailData)
        {
            LocalFree(pInterfaceDetailData);
        }

        deviceInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

        //Get information about the device interface.
        bResult = SetupDiEnumDeviceInterfaces( 
           hDeviceInfo,
           &DeviceInfoData,
           &guidDeviceInterface,
           0, 
           &deviceInterfaceData);

        // Check if last item
        if (GetLastError () == ERROR_NO_MORE_ITEMS)
        {
            break;
        }

        //Check for some other error
        if (!bResult) 
        {
            printf("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
            goto done;
        }

        //Interface data is returned in SP_DEVICE_INTERFACE_DETAIL_DATA
        //which we need to allocate, so we have to call this function twice.
        //First to get the size so that we know how much to allocate
        //Second, the actual call with the allocated buffer
        
        bResult = SetupDiGetDeviceInterfaceDetail(
            hDeviceInfo,
            &deviceInterfaceData,
            NULL, 0,
            &requiredLength,
            NULL);


        //Check for some other error
        if (!bResult) 
        {
            if ((ERROR_INSUFFICIENT_BUFFER==GetLastError()) && (requiredLength>0))
            {
                //we got the size, allocate buffer
                pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, requiredLength);
                
                if (!pInterfaceDetailData) 
                { 
                    // ERROR 
                    printf("Error allocating memory for the device detail buffer.\n");
                    goto done;
                }
            }
            else
            {
                printf("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
                goto done;
            }
        }

        //get the interface detailed data
        pInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        //Now call it with the correct size and allocated buffer
        bResult = SetupDiGetDeviceInterfaceDetail(
                hDeviceInfo,
                &deviceInterfaceData,
                pInterfaceDetailData,
                requiredLength,
                NULL,
                &DeviceInfoData);
        
        //Check for some other error
        if (!bResult) 
        {
            printf("Error SetupDiGetDeviceInterfaceDetail: %d.\n", GetLastError());
            goto done;
        }

        //copy device path
                
        size_t nLength = wcslen (pInterfaceDetailData->DevicePath) + 1;  
        lpDevicePath = (TCHAR *) LocalAlloc (LPTR, nLength * sizeof(TCHAR));
        StringCchCopy(lpDevicePath, nLength, pInterfaceDetailData->DevicePath);
        lpDevicePath[nLength-1] = 0;
                        
        printf("Device path:  %s\n", lpDevicePath);

    }

    if (!lpDevicePath)
    {
        //Error.
        printf("Error %d.", GetLastError());
        goto done;
    }

    //Open the device
    *hDeviceHandle = CreateFile (
        lpDevicePath,
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_OVERLAPPED,
        NULL);

    if (*hDeviceHandle == INVALID_HANDLE_VALUE)
    {
        //Error.
        printf("Error %d.", GetLastError());
        goto done;
    }



done:
    LocalFree(lpDevicePath);
    LocalFree(pInterfaceDetailData);    
    bResult = SetupDiDestroyDeviceInfoList(hDeviceInfo);
    
    return bResult;
}

/**********************************************************************************/
/*以下示例代码使用上一步中创建的文件句柄初始化 WinUSB,并检索设备接口的 WinUSB 接口句柄的指针。*/
/**********************************************************************************/
BOOL GetWinUSBHandle(HANDLE hDeviceHandle, PWINUSB_INTERFACE_HANDLE phWinUSBHandle)
{
    if (hDeviceHandle == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }

    BOOL bResult = WinUsb_Initialize(hDeviceHandle, phWinUSBHandle);
    if(!bResult)
    {
        //Error.
        printf("WinUsb_Initialize Error %d.", GetLastError());
        return FALSE;
    }

    return bResult;
}

求助这是咋回事啊

< >
分享
最佳答案
0

可能是我代码写得太长了,所以大家都不愿意看

我自己发现了问

PVOID p;
    PWINUSB_INTERFACE_HANDLE phWinUSBHandle = &p;
    BOOL getWinUSBHandle = GetWinUSBHandle(hDeviceHandle, phWinUSBHandle);
应改为
PVOID p;
    PWINUSB_INTERFACE_HANDLE phWinUSBHandle = &p;
    BOOL getWinUSBHandle = GetWinUSBHandle(hDeviceHandle, p);
就对了!不好意思,打扰大家了。
微软给的官方文档下面有如何调用的例子,是我没看仔细。
 
郑蕊 | 初学一级 |园豆:134 | 2013-06-19 17:47
其他回答(1)
0

哈哈,好复杂的一个问题,不知道有人解决没?

 

收获园豆:80
SOUTHER | 园豆:825 (小虾三级) | 2013-06-18 20:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册