XP下查找USB设备
我在win2000下用GUID查找我们公司的USB板卡,在2000下一切工作正常,可以排除usb鼠标,U盘,可是移植到XP底下,我定义的guidHID_1如下:
static const GUID guidHID_1 =
{0x27d732d0, 0x979d, 0x4330,{ 0xb9, 0xd8, 0x7a,0x2f,0x23,0x6b,0x71,0x8c}};
未经任何操作就变成了guidHID_1={IID_IMAPISession};
结果除了我们公司的板卡,我还查找到了U盘,个人感觉XP自动改变了我先前定义的guidHID_1.请高手指教我为什么?怎样能改回来?guidHID_1在SetupDiGetClassDevs的时候就已经是IID_IMAPISession了,下面附上源代码:
//文件 define_usb
#include "winioctl.h"
#include "INITGUID.h"
static const GUID guidHID_1 =
{0x27d732d0, 0x979d, 0x4330,{ 0xb9, 0xd8, 0x7a, 0x2f, 0x23, 0x6b, 0x71, 0x8c}};
//主文件
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <mbstring.h>
#include <conio.h>
#include <stdlib.h>
#include <assert.h>
#include <MAPIGUID.H>
#include <setupapi.h>
#include <basetyps.h>
#include "usbioctl.h"
#include "define_usb.h"
extern "C" int PASCAL open_reader(char* time ,int i)
{
int usb1;
iguid=0;
m_Status.m_c.mCommand.m_c.mchar1 = 0x0;
m_Status.m_c.mCommand.m_c.mchar2=AGM_OPENREADER;
current=i;
if(m_hMCom[current])
{
m_Status.m_c .mErrorCode = AGM_COMMOPENED_ERROR;
return m_Status.mint ;
}
HDEVINFO hDevInfoSet;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
BOOL bResult;
// 取得一个该GUID相关的设备信息集句柄
hDevInfoSet = ::SetupDiGetClassDevs((LPGUID) (guidHID_1,//GUID_CLASS_USB_DEVICE, // class GUID
NULL, // 无关键字
NULL, // 不指定父窗口句柄
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
// 失败...
if (hDevInfoSet == INVALID_HANDLE_VALUE)
{
m_Status.m_c .mErrorCode=AG_NOUSB_ERROR;
return m_Status.mint ;
}
// 申请设备接口数据空间
nCount = 0;
usb1 = 7 ;
bResult = TRUE;
SP_DEVICE_INTERFACE_DATA ifdata;
// 设备序号=0,1,2... 逐一测试设备接口,到失败为止
while (bResult)
{
ifdata.cbSize = sizeof(ifdata);
// 枚举符合该GUID的设备接口
bResult = ::SetupDiEnumDeviceInterfaces(
hDevInfoSet, // 设备信息集句柄
NULL, // 不需额外的设备描述
(LPGUID)&guidHID_1,//GUID_CLASS_USB_DEVICE, // GUID
(ULONG)nCount, // 设备信息集里的设备序号
&ifdata); // 设备接口信息
if (bResult)
{
ULONG predictedLength = 0;
ULONG requiredLength = 0;
// 取得该设备接口的细节(设备路径)
bResult = SetupDiGetInterfaceDeviceDetail(
hDevInfoSet, // 设备信息集句柄
&ifdata, // 设备接口信息
NULL, // 设备接口细节(设备路径)
0, // 输出缓冲区大小
&requiredLength, // 不需计算输出缓冲区大小(直接用设定值)
NULL); // 不需额外的设备描述
// 取得该设备接口的细节(设备路径)
predictedLength=requiredLength;
pDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, predictedLength);
pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
bResult = SetupDiGetInterfaceDeviceDetail(
hDevInfoSet, // 设备信息集句柄
&ifdata, // 设备接口信息
pDetail, // 设备接口细节(设备路径)
predictedLength, // 输出缓冲区大小
&requiredLength, // 不需计算输出缓冲区大小(直接用设定值)
NULL); // 不需额外的设备描述
if (bResult)
{
// 复制设备路径到输出缓冲区
//::strcpy(pszDevicePath[nCount], pDetail->DevicePath);
m_hMCom[usb1]=CreateFile(pDetail->DevicePath,//&guidHID_1,//
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
//m_hMcom[nCount-1]=m_hMcom;
// 调整计数值
usb1++;
nCount++;
if (usb1==15){
m_Status.m_c .mErrorCode=AG_TOOUSB_ERROR;
return m_Status.mint ;
}
}
}
}
}