国外网站上解决SetupDiEnumDeviceInterfaces返回false的方法 (转)

注:我也遇到了这个问题,其实很简单,就是GUID与设备驱动的GUID不一致导致的。
 
CodeGuru Forums >  Visual C++ & C++ Programming >  Driver Development > SetupDiEnumDeviceInterfa ces()

PDA

Click to See Complete Forum and Search --> : SetupDiEnumDeviceInterfaces()


scottweddle
March 9th, 2005, 04:38 PM
Hello,

I have a problem with SetupDiEnumDeviceInterfa ces(). It returns FALSE and GetLastError() returns ERROR_NO_MORE_ITEMS. It never returns TRUE meaning it thinks there's no devices attached to the USB port. I have a USB device connected. Device manager says it's set up correctly. I'm passing the ClassGuid for the USB device class.

I'm wondering if anyone knows why it never returns TRUE?

Thanks.

Here's the code:
#include "stdafx.h"
#include "windows.h"
#include "Setupapi.h"
#include "stdio.h"

struct __declspec(uuid("36FC9E60-C465-11CF-8056-444553540000")) uuidUsbDevClass
{
};



int main(int argc, char* argv[])
{
char szTraceBuf[256];
GUID guidUsbDevClass = __uuidof(uuidUsbDevClass);
// Get device interface info set handle for all devices attached to system
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&guidUsbDevClass,
NULL,
NULL,
DIGCF_DEVICEINTERFACE | DIGCF_PRESENT
);

if (hDevInfo == INVALID_HANDLE_VALUE)
{
sprintf(szTraceBuf, "SetupDiClassDevs() failed. GetLastError() " \
"returns: 0x%x\n", GetLastError());
OutputDebugString(szTraceBuf);
printf("SetupDiClassDevs() failed. GetLastError() " \
"returns: 0x%x\n", GetLastError());
return 1;
}

sprintf(szTraceBuf, "Device info set handle for all devices attached to " \
"system: 0x%x\n", hDevInfo);
OutputDebugString(szTraceBuf);
printf("Device info set handle for all devices attached to " \
"system: 0x%x\n", hDevInfo);

// Retrieve a context structure for a device interface of a device
// information set.
DWORD dwIndex = 0;
SP_DEVICE_INTERFACE_DATA devInterfaceData;
ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
BOOL bRet = FALSE;
while(TRUE)
{
bRet = SetupDiEnumDeviceInterfa ces(
hDevInfo,
NULL,
&guidUsbDevClass,
dwIndex,
&devInterfaceData
);
if (!bRet)
{
sprintf(szTraceBuf, "SetupDiEnumDeviceInterfa ces failed " \
"GetLastError() returns: 0x%x\n", GetLastError());
OutputDebugString(szTraceBuf);
printf("SetupDiEnumDeviceInterfa ces failed " \
"GetLastError() returns: 0x%x\n", GetLastError());

if (GetLastError() == ERROR_NO_MORE_ITEMS)
{
break;
}
}

dwIndex++;
}

sprintf(szTraceBuf, "Number of device interface sets representing all " \
"devices attached to system: 0x%x\n", dwIndex);
OutputDebugString(szTraceBuf);
printf("Number of device interface sets representing all " \
"devices attached to system: 0x%x\n", dwIndex);

SetupDiDestroyDeviceInfo List(hDevInfo);

return 0;
}

scottweddle
March 10th, 2005, 11:29 AM
Here's more to add:
I'm doing this enumeration at the application level (user mode) not driver level (kernel mode).

In order to be able to enumerate the devices does ther driver have to call IoRegisterDeviceInterfac e()?

Funny thing is when I call SetDiGetClassDevs() with whatever combonation for the 1st and 4th argument's I always get back the same handle. I'm passing the ClassGuid for the USB devices that come under the registry key:
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\USB
My theroy is that if you pass only DIGCF_DEVICEINTERFACE as the 4th arg along with the USB ClassGuid then SetupDiEnumDeviceInterfa ces() should return a count = to the number of univeral serial bus controllers listed in the Computer Management dialog under the device manager tree icon.
If you pass the combonation of flags DIGCF_PRESENT | DIGCF_DEVICEINTERFACE and I have my device attached to the host SetupDiEnumDeviceInterfa ces() should return 1.

That's all for now.

Thanks,
Scott

scottweddle
March 11th, 2005, 12:43 PM
Problem solved. I was using the wrong GUID. The correct GUID is:
A5DCBF10-6530-11D2-901F-00C04FB951ED.
Here the code that works. Its basically the same except for how the GUID is declared and passed to functions SetupDiGetClassDevs() and SetupDiEnumDeviceInterfa ces().

#include "stdafx.h"
#include "windows.h"
#include "Setupapi.h"
#include "stdio.h"

static GUID GUID_DEVINTERFACE_USB_DEVICE =
{ 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };



int main(int argc, char* argv[])
{
char szTraceBuf[256];
// Get device interface info set handle for all devices attached to system
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&GUID_DEVINTERFACE_USB_DEVICE,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
);

if (hDevInfo == INVALID_HANDLE_VALUE)
{
sprintf(szTraceBuf, "SetupDiClassDevs() failed. GetLastError() " \
"returns: 0x%x\n", GetLastError());
OutputDebugString(szTraceBuf);
printf("SetupDiClassDevs() failed. GetLastError() " \
"returns: 0x%x\n", GetLastError());
return 1;
}

sprintf(szTraceBuf, "Device info set handle for all devices attached to " \
"system: 0x%x\n", hDevInfo);
OutputDebugString(szTraceBuf);
printf("Device info set handle for all devices attached to " \
"system: 0x%x\n", hDevInfo);

// Retrieve a context structure for a device interface of a device
// information set.
DWORD dwIndex = 0;
SP_DEVICE_INTERFACE_DATA devInterfaceData;
ZeroMemory(&devInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
BOOL bRet = FALSE;
while(TRUE)
{
bRet = SetupDiEnumDeviceInterfa ces(
hDevInfo,
NULL,
&GUID_DEVINTERFACE_USB_DEVICE,
dwIndex,
&devInterfaceData
);
if (!bRet)
{
sprintf(szTraceBuf, "SetupDiEnumDeviceInterfa ces failed " \
"GetLastError() returns: 0x%x\n", GetLastError());
OutputDebugString(szTraceBuf);
printf("SetupDiEnumDeviceInterfa ces failed " \
"GetLastError() returns: 0x%x\n", GetLastError());

if (GetLastError() == ERROR_NO_MORE_ITEMS)
{
break;
}
}

dwIndex++;
}

sprintf(szTraceBuf, "Number of device interface sets representing all " \
"devices attached to system: 0x%x\n", dwIndex);
OutputDebugString(szTraceBuf);
printf("Number of device interface sets representing all " \
"devices attached to system: 0x%x\n", dwIndex);

SetupDiDestroyDeviceInfo List(hDevInfo);

return 0;
}

Neor
March 13th, 2005, 09:18 AM
Congratulation!

howcani
June 29th, 2005, 09:34 PM
I really need to know where you found the device interface GUID as distinct from the device setup GUID.....

optionalreaction
September 3rd, 2005, 12:51 PM
I really need to know where you found the device interface GUID as distinct from the device setup GUID.....


Yes, so do I.

Has anyone got the (current) 'interface GUID' header file that they could post here?

Keith

guitarmy
September 10th, 2005, 12:04 PM
Look in the winioctl.h header in the sdk include directory. make sure to include initguid.h as well if you plan to use the predefined guids.
eric

optionalreaction
September 11th, 2005, 01:58 PM
Look in the winioctl.h header in the sdk include directory. make sure to include initguid.h as well if you plan to use the predefined guids.
eric

Thanks guitarmy... unfortunately winioctl.h contains GUIDs for IO only (disk, comport etc).

I think the only way to get them is with the MS DDK, and that's not available for free download.

Keith

cehupper
April 13th, 2006, 02:26 PM




DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB, 0xf18a0e88, 0xc30c, 0x11d0, 0x88, 0x15, 0x00, \
0xa0, 0xc9, 0x06, 0xbe, 0xd8);


DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, \
0xC0, 0x4F, 0xB9, 0x51, 0xED);


DEFINE_GUID(GUID_DEVINTERFACE_USB_HOST_CONTROLLER, 0x3abf6f2d, 0x71c4, 0x462a, 0x8a, 0x92, 0x1e, \
0x68, 0x61, 0xe6, 0xaf, 0x27);


DEFINE_GUID(GUID_USB_WMI_STD_DATA, 0x4E623B20L, 0xCB14, 0x11D1, 0xB3, 0x31, 0x00,\
0xA0, 0xC9, 0x59, 0xBB, 0xD2);


DEFINE_GUID(GUID_USB_WMI_STD_NOTIFICATION, 0x4E623B20L, 0xCB14, 0x11D1, 0xB3, 0x31, 0x00,\
0xA0, 0xC9, 0x59, 0xBB, 0xD2);




#define GUID_CLASS_USBHUB GUID_DEVINTERFACE_USB_HUB
#define GUID_CLASS_USB_DEVICE GUID_DEVINTERFACE_USB_DEVICE
#define GUID_CLASS_USB_HOST_CONTROLLER GUID_DEVINTERFACE_USB_HOST_CONTROLLER

#define FILE_DEVICE_USB FILE_DEVICE_UNKNOWN

codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.
被过滤广告国外网站上解决SetupDiEnumDeviceInterfaces返回false的方法 <wbr>(转)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值