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 编辑 ]