| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1167 人关注过本帖
标题:USB通讯问题
只看楼主 加入收藏
wqywqy
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-3-31
结帖率:50%
收藏
已结贴  问题点数:6 回复次数:2 
USB通讯问题
下面是一段打开USB 设备的程序 调试的时候按F11发现执行到 PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);
时停止了,是什么原因呢。。
程序代码:
HANDLE OpenMyHIDDevice(int overlapped)
{
    HANDLE     hidHandle;
    GUID     hidGuid;
   
    HidD_GetHidGuid(&hidGuid);
   
    HDEVINFO hDevInfo = SetupDiGetClassDevs(
                    &hidGuid,
                    NULL,
                    NULL,
                    (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
    if (hDevInfo == INVALID_HANDLE_VALUE)
    {
     return INVALID_HANDLE_VALUE;
    }
   
    SP_DEVICE_INTERFACE_DATA devInfoData;
    devInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
    int deviceNo = 0;
   
    SetLastError(NO_ERROR);
   
    while (GetLastError() != ERROR_NO_MORE_ITEMS)
    {
        if (SetupDiEnumInterfaceDevice (hDevInfo,
                    0,
                    &hidGuid,
                    deviceNo,
                    &devInfoData))
        {
            ULONG requiredLength = 0;
            SetupDiGetInterfaceDeviceDetail(hDevInfo,
                                            &devInfoData,
                                            NULL,
                                            0,
                                            &requiredLength,
                                            NULL);

 

            PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);
        //----????

            devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

            if(!SetupDiGetInterfaceDeviceDetail(hDevInfo,
                        &devInfoData,
                        devDetail,
                        requiredLength,
                        NULL,
                        NULL))
            {
                free(devDetail);
                           SetupDiDestroyDeviceInfoList(hDevInfo);
                return INVALID_HANDLE_VALUE;
            }

                  if (overlapped)
                  {
                         hidHandle = CreateFile(devDetail->DevicePath,
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                        NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_OVERLAPPED,
                        NULL);
                  }
                  else
                  {
                         hidHandle = CreateFile(devDetail->DevicePath,
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                        NULL,
                        OPEN_EXISTING,
                        0,
                        NULL);
                  }

            free(devDetail);

            if (hidHandle==INVALID_HANDLE_VALUE)
            {
                SetupDiDestroyDeviceInfoList(hDevInfo);
                free(devDetail);
                return INVALID_HANDLE_VALUE;
            }

            _HIDD_ATTRIBUTES hidAttributes;
            if(!HidD_GetAttributes(hidHandle, &hidAttributes))
            {
                CloseHandle(hidHandle);
                           SetupDiDestroyDeviceInfoList(hDevInfo);
                return INVALID_HANDLE_VALUE;
            }

            if (USB_VID == hidAttributes.VendorID && USB_PID == hidAttributes.ProductID)
            {
                break;
            }
            else
            {
                CloseHandle(hidHandle);
                ++deviceNo;
            }
        }
    }
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return hidHandle;
}

 

下面是我的头函数
程序代码:
#include "stdafx.h"
#include "xxxx.h"
#include "xxxxDlg.h"
#include "malloc.h"
#include "setupapi.h"
extern "C"
{
#include "hidsdi.h"
}
#pragma comment(lib,"setupapi")
#pragma comment(lib,"hid")


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define USB_VID 0xE6A
#define USB_PID 0x103 


[ 本帖最后由 wqywqy 于 2014-12-30 17:54 编辑 ]
2010-06-17 09:51
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:4 
PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc (requiredLength);
--->
PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA) malloc (requiredLength);

★★★★★为人民服务★★★★★
2010-06-17 20:53
wqywqy
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-3-31
收藏
得分:0 
回复 2楼 cnfarer
试过了~!照你说的那样做编译通不过。。
2010-06-18 15:07
快速回复:USB通讯问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039638 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved