Hi. This is a repeat of my earlier post (but with less code that is better formatted and more explanation).
I’m having difficulty getting to the point where I can open a handle to WinUSB
from an application. I have succesfully installed WinUSB under XP (x86). I
have code that will find the number of devices I have plugged in to the USB port
(devices that I am developing software for), but I can’t seem to get past this
point. I am following the code found on Microsoft’s website pretty closely:
http://msdn2.microsoft.com/en-us/library/aa476413.aspx
See below for my code. Let me know if you have any ideas. Specifically, I’m
trying to print out the device path. This is the critical element I need to Create a
file and retrieve a WinUSB handle…
Specifically, I think I’m having problems with SetupDiGetDeviceInterfaceDetail. Once I figure how many of my devices are on the USB port, I then enumerate through all of my devices, calling the above function. It fails the first time through (to get the size of the device path). Then, I call it again to actually get the device path. Is this correct?
–Brian Hindman
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <strsafe.h>
#include <setupapi.h>
#include “OSROnline.h”
//----------------------------------------------------------------------
// Main for finding DS2490s (USB devices)
//
int main(int argc, char **argv)
{
// Declare and initialize variables
GUID InterfaceGuid; // The GUID matches the one in the inf file
HDEVINFO hdevClassInfo; // Define a handle to a device information set.
SP_DEVICE_INTERFACE_DATA MyDeviceData; // Structure for interface data
SP_DEVINFO_DATA DevInfo;
PSP_INTERFACE_DEVICE_DETAIL_DATA pBuffer; // Structure for device details
DWORD nMemberIndex = 0; // Holds number of device interfaces
BOOL nStatus = FALSE; // Holds function call results
struct DeviceData // Declare DeviceList variable as
{
TCHAR HardwareId;
TCHAR Path;
TCHAR FriendlyName;
DWORD DeviceInstance;
} DeviceList;
DWORD i = 0;
DWORD nSize;
size_t nLen = 0;
InterfaceGuid = GUID_DEVINTERFACE_DS2490; // Taken from OSROnline.h
hdevClassInfo = SetupDiGetClassDevs (&InterfaceGuid, NULL, NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hdevClassInfo == INVALID_HANDLE_VALUE)
{
// ERROR
printf(“Error retrieving a device information set for given GUID\n”);
}
// Determine how many elements are in the device information set.
MyDeviceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
nMemberIndex = 0;
nStatus = TRUE;
while (nStatus == TRUE)
{
nMemberIndex++;
nStatus = SetupDiEnumDeviceInterfaces (hdevClassInfo, NULL, (LPGUID)
&InterfaceGuid, nMemberIndex, &MyDeviceData);
}
printf(“Number of DS2490 device interfaces: %i\n”, nMemberIndex);
//
// This is as far as I get
//
DeviceList = (struct DeviceData ) malloc ((nMemberIndex + 1) * sizeof(struct DeviceData));
if (DeviceList == NULL)
{
// ERROR
printf(“Error no devices.\n”);
return 0;
}
printf(“DeviceList successfully malloced\n\n”);
nStatus = FALSE;
// Enumerate devices that are associated with the interface.
for (i = 0; i < nMemberIndex; i++)
{
nStatus = SetupDiEnumDeviceInterfaces(hdevClassInfo, NULL,
(LPGUID)&InterfaceGuid,
i, &MyDeviceData);
if (nStatus != TRUE) break;
nStatus = FALSE;
// Retrieve the size of the device data.
nStatus = SetupDiGetDeviceInterfaceDetail(hdevClassInfo, &MyDeviceData,
NULL, 0, &nSize, NULL);
if (nStatus != FALSE) break; // This function is expected to fail
printf(“Size of device data is: %i\n”, nSize);
// Allocate memory for the device detail buffer.
pBuffer = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc (nSize);
if (pBuffer == NULL)
{
// ERROR
printf(“Error allocating memory for the device detail buffer.\n”);
return 0;
}
// Initialize variables.
MyDeviceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
nStatus = SetupDiGetDeviceInterfaceDetail(hdevClassInfo, &MyDeviceData,
pBuffer, nSize, NULL,&DevInfo);
if (nStatus = FALSE)
{
// ERROR
printf(“Error retrieving device interface detail.\n”);
return 0;
}
//***********************************
// Save the device interface path:
// This path can be used to open
// the interface with CreateFile.
// ****************************************
nLen = strlen(pBuffer->DevicePath) + 1;
DeviceList[i].Path = (TCHAR *) malloc (nLen * sizeof(TCHAR));
StringCchCopy(DeviceList[i].Path, nLen, pBuffer->DevicePath);
DeviceList[i].Path[nLen-1] = 0;
// Save the device instance.
DeviceList[i].DeviceInstance = DevInfo.DevInst;
printf(“DeviceList path is: %s\n”,DeviceList[i].Path);
//_tprintf(_T(“DeviceList path is: %s\n”),DeviceList[i].Path);
}
free(DeviceList); // we must clean up…
return 0;
}</setupapi.h></strsafe.h></tchar.h></stdlib.h></stdio.h></windows.h>