Thank you for sending this but I see a few problems on the surface of this
code:
- The routine SendInquiry(UsbHandle) is undefined
- Memory for the pointer buffer is allocated with malloc(sizeof (required))
required is a DWORD. buffer later cast to a
PSP_DEVICE_INTERFACE_DETAIL_DATA structure. The size of
SP_DEVICE_INTERFACE_ETAIL_DATA is not the same as the size of a DWORD.
- after allocating buffer (with the value of required set to 0) the value
of buffer is set to the required buffersize and passed to
SetupDiGetDriverInterfaceDetal as the size of buffer. There is no
allocation for buffer after the size needed is known.
Ed
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Monday, September 28, 2009 2:22 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] USB FLASH device enumeration question
int _tmain(int argc, _TCHAR* argv)
{
int nRetCode = 0;
HANDLE Usbhandle;
HDEVINFO hDevHandle;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
DWORD required = 0;
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
int nBufferSize = 0;
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD MemberIndex = 0;
BOOL Result;
// BAsed on the available Volume in the system it take the
Handle. Check for multpule usb volume.
hDevHandle = SetupDiGetClassDevs(&GUID_DEVINTERFACE_VOLUME, NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if(hDevHandle == INVALID_HANDLE_VALUE)
{
return 1;
}
else
{
BOOL bStart = false;
TCHAR *buffer = NULL;
PSP_DEVICE_INTERFACE_DETAIL_DATA devicedetailData;
buffer = (TCHAR*)malloc(sizeof(required));
do
{
Result = SetupDiEnumDeviceInfo(hDevHandle,
MemberIndex, &devInfoData);
if(Result)
{
Result =
SetupDiEnumDeviceInterfaces(hDevHandle, 0, &GUID_DEVINTERFACE_VOLUME,
MemberIndex, &deviceInterfaceData);
}
if(!Result)
{
SetupDiDestroyDeviceInfoList(hDevHandle);
/*delete buffer;*/ //here it is crashing why???
/*free(buffer);*/
buffer = NULL;
return 1;
}
MemberIndex++;
BOOL detailResult = FALSE;
if(!bStart)
{
// As per MSDN, Get the required buffer size. Call
SetupDiGetDeviceInterfaceDetail with a
// NULL DeviceInterfaceDetailData pointer, a
DeviceInterfaceDetailDataSize of zero,
// and a valid RequiredSize variable. In response to such a
call, this function returns
// the required buffer size at RequiredSize and fails with
GetLastError returning
// ERROR_INSUFFICIENT_BUFFER.
// Allocate an appropriately sized buffer and call the
function again to get the interface details.
SetupDiGetDeviceInterfaceDetail(hDevHandle,
&deviceInterfaceData, NULL, 0, &required, NULL);
devicedetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) buffer;
devicedetailData->cbSize =
sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
nBufferSize = required;
bStart = true;
}
detailResult = SetupDiGetDeviceInterfaceDetail(hDevHandle,
&deviceInterfaceData,
devicedetailData, nBufferSize , &required, NULL);
_tprintf(L"Device Path = %s\n\n", devicedetailData->DevicePath);
if(detailResult)
{
Usbhandle =
CreateFile(devicedetailData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
SendInquiry(Usbhandle);
}
if(!detailResult)
{
continue;
}
}while(Result);
}
return nRetCode;
}
NTDEV is sponsored by OSR
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer