Problem with SetupDiGetDeviceInterfaceDetail function

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello, I am trying to use the SetupDiGetDeviceInterfaceDetailA() function in C++. It works fine for doing the trick of getting the exact required size. However, when I run the function a second time to get the information, my dev_interface.cbSize gets changed
and my dsize value also changes. Then after it is done, the program calls a stack overflow on my dev_detail variable. I do not know wat is going wrong, because I also have the same code in Python and those values do not change. I am using Visual Studio 2010
and I link my project to the Setupapi.lib
<div id="x_:bm Here is my C++ code I am using:
<pre class="prettyprint #include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <SetupAPI.h>

int main(int argc, char **argv)
{
// Build vector list of device types to search for
GUID guid_vec [2] = {GUID_DEVINTERFACE_COMPORT, GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR};

for (unsigned int i=0; i<2; ++i){
GUID guid = guid_vec;
// Get the class of the device
HDEVINFO dev_set = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if (dev_set == INVALID_HANDLE_VALUE){ // Check if INVALID_HANDLE_VALUE
DWORD last_error = GetLastError();
printf("SetupDiGetClassDevsA Error: %lun", last_error);
system("PAUSE");
return FALSE;
}
BOOL more_items = TRUE;
DWORD idx = 0;
SP_DEVINFO_DATA dev_info;

while (more_items){
dev_info.cbSize = sizeof(SP_DEVINFO_DATA);
more_items = SetupDiEnumDeviceInfo(dev_set, idx, &dev_info);
if (more_items){
// Get the devices hardware id
BYTE hardware_id[256];
hardware_id[0] = ;
DWORD hid_size = sizeof(hardware_id);
if (SetupDiGetDeviceRegistryProperty(dev_set, &dev_info, SPDRP_HARDWAREID, NULL, hardware_id, hid_size, NULL) == FALSE){
DWORD last_error = GetLastError();
if (last_error != ERROR_INSUFFICIENT_BUFFER){ // Ignore ERROR_INSUFFICIENT_BUFFER
printf("SetupDiGetDeviceRegistryPropertyA HARDWAREID Error: %lun", last_error);
system("PAUSE");
return FALSE;
}
}

// Get the devices enumerator name
BYTE enum_name[256];
enum_name[0] = ;
DWORD enum_size = sizeof(enum_name);
if (SetupDiGetDeviceRegistryProperty(dev_set, &dev_info, SPDRP_ENUMERATOR_NAME, NULL, enum_name, enum_size, NULL)){
SP_DEVICE_INTERFACE_DATA dev_interface;
dev_interface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

if (SetupDiCreateDeviceInterface(dev_set, &dev_info, &guid, NULL, 0, &dev_interface) == FALSE){
DWORD last_error = GetLastError();
printf("SetupDiEnumDeviceInterfaces Error: %lun", last_error);
system("PAUSE");
return FALSE;
}

// Get the size of the detail information for the device
DWORD dsize;
if (SetupDiGetDeviceInterfaceDetail(dev_set, &dev_interface, NULL, 0, &dsize, NULL) == FALSE){
DWORD last_error = GetLastError();
if (last_error != ERROR_INSUFFICIENT_BUFFER){ // Ignore ERROR_INSUFFICIENT_BUFFER
printf("SetupDiGetDeviceInterfaceDetailA 1 Error: %lun", last_error);
system("PAUSE");
return FALSE;
}
}

// Allocate for the size of the information of the device
SP_DEVICE_INTERFACE_DETAIL_DATA dev_detail;
dev_detail.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

if (SetupDiGetDeviceInterfaceDetail(dev_set, &dev_interface, &dev_detail, dsize, NULL, NULL) == FALSE){
DWORD last_error = GetLastError();
printf("SetupDiGetDeviceInterfaceDetailA 2 Error: %lun", last_error);
system("PAUSE");
return FALSE;
}
}
else{
DWORD last_error = GetLastError();
if (last_error != ERROR_INSUFFICIENT_BUFFER){ // Ignore ERROR_INSUFFICIENT_BUFFER
printf("SetupDiGetDeviceRegistryPropertyA ENUMERATOR Error: %lun", last_error);
system("PAUSE");
return FALSE;
}
}

}
++idx;
}
SetupDiDestroyDeviceInfoList(dev_set);
}
system("PAUSE");
return 0;
}[/code]
Any clarification would be grateful.
Thank you,
chrisgeorge10

View the full article
 
Back
Top