C语言 编写的usb枚举 有问题 请高手帮忙解决
#include <windows.h>
#include <basetyps.h>
#include <windowsx.h>
#include <initguid.h>
#include <devioctl.h>
#include <usbioctl.h>
#include <dbt.h>
#include <stdio.h>
#include <setupapi.h>
char* GetUsbDeviceLabel(char* key_name)
{
HDEVINFO di = SetupDiGetClassDevs(&GUID_CLASS_USB_DEVICE, 0, 0, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
if(di == INVALID_HANDLE_VALUE)
{
MyPrintf("没有找到");
return 0;
}
for(int i = 0; ; i++)
{
BYTE tmp_buf1[513];
//主控的描述字符串
SP_DEVINFO_DATA DevInfoData;
DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if(! SetupDiEnumDeviceInfo(di, i, &DevInfoData))
break;
if(! SetupDiGetDeviceRegistryProperty(di, &DevInfoData, SPDRP_DRIVER, 0, tmp_buf1, 513, 0))
continue;
if(strcmp(key_name, (char*)tmp_buf1) != 0)
continue;
if(SetupDiGetDeviceRegistryProperty(di, &DevInfoData, SPDRP_DEVICEDESC, 0, tmp_buf1, 513, 0))
{
strcpy(key_name, (char*)tmp_buf1);
return key_name;
}
else
{
return 0;
}
}
return 0;
}
void GetPortDevDescString(HANDLE hub, int port_idx, USB_DEVICE_DESCRIPTOR* dev_desc)
{
//读语言的种类
BYTE tmp_buf1[513] = {0};
DWORD data_len;
USB_DESCRIPTOR_REQUEST* str_req = (USB_DESCRIPTOR_REQUEST*)tmp_buf1;
USB_STRING_DESCRIPTOR* str_desc = (USB_STRING_DESCRIPTOR*)(tmp_buf1 + sizeof(USB_DESCRIPTOR_REQUEST) - 1);
int LanIDs_num = 0;
WORD* LanIDs = 0;
int i;
str_req->ConnectionIndex = port_idx;
str_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | 0;
str_req->SetupPacket.wIndex = 0;
str_req->SetupPacket.wLength = (USHORT)(513 - sizeof(USB_DESCRIPTOR_REQUEST) + 1);
if(! DeviceIoControl(hub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, str_req, 513, str_req, 513, &data_len, 0))
{
LanIDs_num = 0;
LanIDs = 0;
}
else
{
LanIDs_num = (str_desc->bLength - 2) / 2;
LanIDs = str_desc->bString;
}
//读语言
BYTE tmp_buf2[513] = {0};
str_req = (USB_DESCRIPTOR_REQUEST*)tmp_buf2;
str_desc = (USB_STRING_DESCRIPTOR*)(tmp_buf2 + sizeof(USB_DESCRIPTOR_REQUEST) - 1);
//输出
MyPrintf("Device Descriptor:");
MyPrintf("bcdUSB: 0x%04X", dev_desc->bcdUSB);
MyPrintf("bDeviceClass: 0x%02X", dev_desc->bDeviceClass);
MyPrintf("bDeviceSubClass: 0x%02X", dev_desc->bDeviceSubClass);
MyPrintf("bDeviceProtocol: 0x%02X", dev_desc->bDeviceProtocol);
MyPrintf("bMaxPacketSize0: 0x%02X (%d)", dev_desc->bMaxPacketSize0, dev_desc->bMaxPacketSize0);
MyPrintf("idVendor: 0x%04X", dev_desc->idVendor);
MyPrintf("idProduct: 0x%04X", dev_desc->idProduct);
MyPrintf("bcdDevice: 0x%04X", dev_desc->bcdDevice);
MyPrintf("iManufacturer: 0x%02X", dev_desc->iManufacturer);
if(dev_desc->iManufacturer != 0)
{
for (i=0; i< LanIDs_num; i++)
{
str_req->ConnectionIndex = port_idx;
str_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | dev_desc->iManufacturer;
str_req->SetupPacket.wIndex = LanIDs[i];
str_req->SetupPacket.wLength = (USHORT)(513 - sizeof(USB_DESCRIPTOR_REQUEST) + 1);
if(! DeviceIoControl(hub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, str_req, 513, str_req, 513, &data_len, 0))
MyPrintf("0x%04X: 失败", LanIDs[i]);
else
MyPrintf("0x%04X: "%S"", LanIDs[i], str_desc->bString);
}
}
MyPrintf("iProduct: 0x%02X", dev_desc->iProduct);
if(dev_desc->iProduct != 0)
{
for (i=0; i< LanIDs_num; i++)
{
str_req->ConnectionIndex = port_idx;
str_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | dev_desc->iProduct;
str_req->SetupPacket.wIndex = LanIDs[i];
str_req->SetupPacket.wLength = (USHORT)(513 - sizeof(USB_DESCRIPTOR_REQUEST) + 1);
if(! DeviceIoControl(hub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, str_req, 513, str_req, 513, &data_len, 0))
MyPrintf("0x%04X: 失败", LanIDs[i]);
else
MyPrintf("0x%04X: "%S"", LanIDs[i], str_desc->bString);
}
}
MyPrintf("iSerialNumber:: 0x%02X", dev_desc->iSerialNumber);
if(dev_desc->iSerialNumber != 0)
{
for (i=0; i< LanIDs_num; i++)
{
memset(str_req, 0, 513);
str_req->ConnectionIndex = port_idx;
str_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | dev_desc->iSerialNumber;
str_req->SetupPacket.wIndex = LanIDs[i];
str_req->SetupPacket.wLength = (USHORT)(513 - sizeof(USB_DESCRIPTOR_REQUEST) + 1);
if(! DeviceIoControl(hub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, str_req, 513, str_req, 513, &data_len, 0))
MyPrintf("0x%04X: 失败", LanIDs[i]);
else
MyPrintf("0x%04X: "%S"", LanIDs[i], str_desc->bString);
}
}
MyPrintf("bNumConfigurations: 0x%02X", dev_desc->bNumConfigurations);
MyPrintf("");
}
void GetEndpoint(int pip_num, USB_PIPE_INFO* pip_list)
{
for(int i = 0; i < pip_num; i++)
{
USB_ENDPOINT_DESCRIPTOR* ep_desc = &(pip_list[i].EndpointDescriptor);
MyPrintf("Endpoint Descriptor:");
if(USB_ENDPOINT_DIRECTION_IN(ep_desc->bEndpointAddress))
MyPrintf("bEndpointAddress: 0x%02X IN", ep_desc->bEndpointAddress);
else
MyPrintf("bEndpointAddress: 0x%02X OUT", ep_desc->bEndpointAddress);
switch(ep_desc->bmAttributes & 0x03)
{
case 0x00:
MyPrintf("Transfer Type: Control");
break;
case 0x01:
MyPrintf("Transfer Type: Isochronous");
break;
case 0x02:
MyPrintf("Transfer Type: Bulk");
break;
case 0x03:
MyPrintf("Transfer Type: Interrupt");
break;
}
MyPrintf("wMaxPacketSize: 0x%04X (%d)", ep_desc->wMaxPacketSize, ep_desc->wMaxPacketSize);
if(ep_desc->bLength == sizeof(USB_ENDPOINT_DESCRIPTOR))
{
MyPrintf("bInterval: 0x%02X", ep_desc->bInterval);
}
else
{
USB_ENDPOINT_DESCRIPTOR2* ep_desc2 = (USB_ENDPOINT_DESCRIPTOR2*)ep_desc;
MyPrintf("wInterval: 0x%04X", ep_desc2->wInterval);
MyPrintf("bSyncAddress: 0x%02X", ep_desc2->bSyncAddress);
}
MyPrintf("");
}
}
------解决方案--------------------
WDK里面的usbview工具需要用WDK的Build环境去编译。
如果你想用VC直接编译的话,VC的Compile和Linker设置一定要对。
建议你直接进入Build环境,直接生成执行程序。