Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am developing an application for interfacing with the USB device. When i called SetupDiEnumDeviceInterfaces() for enumerating the device interface, I get error as "No more data is available"(got error code as 259 using GetLastError()).

Before Calling this function i called SetupDiGetClassDevs() and HidD_GetHidGuid(), it works fine.

Any suggestion for this error?

Regards
Mohan
Posted
Updated 30-Dec-10 19:08pm
v2
Comments
Dave Kreskowiak 31-Dec-10 1:29am    
Without seeing your code that is trying to enumerate the interfaces, it's impossible to tell you where you went wrong.
Mohanraj Sam 31-Dec-10 1:35am    
Hi,
Thanks for ur reply. I paste the code here, plz go through it and suggest me if I went wrong.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h>
#include <io.h>
#include <conio.h>
#include <windows.h>
#include <winioctl.h>
#include <setupapi.h>
#include "hidsdi.h"

int main()
{

HANDLE deviceHandle = INVALID_HANDLE_VALUE;
DWORD index = 1;
HIDD_ATTRIBUTES deviceAttributes;
BOOL matched = FALSE;

DWORD vendorID, productID, versionNumber;

while (!matched && (deviceHandle = connectToUSBDevice (index)) != INVALID_HANDLE_VALUE)
{
if (!HidD_GetAttributes (deviceHandle, &deviceAttributes))
return INVALID_HANDLE_VALUE;

if ((vendorID == 0 || deviceAttributes.VendorID == vendorID) &&
(productID == 0 || deviceAttributes.ProductID == productID) &&
(versionNumber == 0 || deviceAttributes.VersionNumber == versionNumber))
return deviceHandle; /* matched */

CloseHandle (deviceHandle); /* not a match - close and try again */
index++;
}

return INVALID_HANDLE_VALUE;
}


int connectToUSBDevice(DWORD in_dwDeviceIndex)
{
BOOL bDiskFreeSts, bGetPartinfo;
DWORD errStatus;
DWORD BytesReturned;
DWORD result;

HANDLE hDeviceHandle = INVALID_HANDLE_VALUE;
PARTITION_INFORMATION_EX PartitionInfomation; //partition structure
DRIVE_LAYOUT_INFORMATION_EX DriveInformation; //drive layout structure

HDEVINFO hDev;
GUID guid;
SP_DEVICE_INTERFACE_DATA interfaceDev;
PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
ULONG wSize;

//Get the HID GUID value - used as mask to get list of devices
HidD_GetHidGuid(&guid);

//Get a list of devices matching the criteria (hid interface, present)
hDev = SetupDiGetClassDevs(&guid,
NULL, // Define no enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices.

interfaceDev.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

//Go through the list and get the interface data
result = SetupDiEnumDeviceInterfaces (hDev,NULL, &guid, in_dwDeviceIndex, &interfaceDev);

/* Failed to get a device - possibly the index is larger than the number of devices */
if (result == FALSE)
{
errStatus = GetLastError();
printf("\nError in Opening Device....Error Status = %d",errStatus);

SetupDiDestroyDeviceInfoList(hDev);
return INVALID_HANDLE_VALUE;
}

//Get the details with null values to get the required size of the buffer
SetupDiGetDeviceInterfaceDetail(hDev, &interfaceDev, NULL, 0, &wSize,0);

//Allocate the buffer
deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(wSize);
deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

//Fill the buffer with the device details
if (!SetupDiGetDeviceInterfaceDetail(hDev,&interfaceDev, deviceDetail,\
wSize, &wSize, NULL))
{
SetupDiDestroyDeviceInfoList(hDev);
free (deviceDetail);
return INVALID_HANDLE_VALUE;
}

/*Open file on the device*/
hDeviceHandle = CreateFile(deviceDetail->DevicePath /*dev1*/,
GENERIC_READ/* | GENERIC_WRITE*/,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0,
NULL); // No template file

if(hDeviceHandle == INVALID_HANDLE_VALUE) // cannot open the drive
{
errStatus = GetLastError();
printf("\nError in Opening Device....Error Status = %d",errStatus);
}
else
{
/*Retrieves extended information for each entry in the partition tables for a disk */
bGetPartinfo= DeviceIoControl(hDeviceHandle, // handle to a partition
IOCTL_DISK_GET_DRIVE_LAYOUT_EX, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
&DriveInformation, // output buffer
sizeof(DriveInformation), // size of output buffer
cariolihome 6-Jan-11 17:24pm    
You have to call SetupDiGetClassDevs without DIGCF_DEVICEINTERFACE flag
Then you have to call next functions:
SetupDiEnumDeviceInfo to get SP_DEVINFO_DATA struct
SetupDiGetDeviceRegistryProperty with SPDRP_PHYSICAL_DEVICE_OBJECT_NAME parameter to get device name

If you call SetupDiGetClassDevs with DIGCF_DEVICEINTERFACE flag, then ClassGUID must be GUID_DEVINTERFACE_*,
not GUID_DEVCLASS_*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900