Obtaining handle to WinUSB

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…

–Brian Hindman

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <strsafe.h>
#include <setupapi.h>
#include “DS2490Discovery.h”

//----------------------------------------------------------------------
// Main for FindDS2490s
//
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
SP_DEVINFO_DATA DevInfoDataFromRegistry; // Structure for registry data details
DWORD dwRegType; // Place for Registry type
DWORD dwRegSize; // Size of registry key
TCHAR pBufferFriendlyName;
TCHAR pBufferHardwareID;
DWORD nMemberIndex = 0; // Holds number of device interfaces
BOOL nStatus = FALSE; // Holds function call results
struct DeviceData // Declare DeviceList variable as struct
{
TCHAR HardwareId;
TCHAR Path;
TCHAR FriendlyName;
DWORD DeviceInstance;
} DeviceList;
DWORD i = 0;
DWORD nSize;
size_t nLen = 0;
DWORD lastError = 0;

InterfaceGuid = GUID_DEVINTERFACE_DS2490; // Taken from DS2490Discovery.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;
printf(“After first break in for loop\n”);

// 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
lastError = GetLastError();
if (lastError == ERROR_INSUFFICIENT_BUFFER)
{
printf(“GetLastError returned ERROR_INSUFFICIENT_BUFFER\n”);
}
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);

// 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>