Question

Evening everyone I wondered if some could answer me 2 quick questions.

I made an app to communicate with arm device which works fine, however when I move PC etc, I need to reconfigure the device path. Its a long one like below.

Path: \\?\usb#vid_045e&pid_0040#6&ff454f2&0&3#{a5dcbf10-6530-11d2-901f-00c04fb951ed}

I done some reading up and discovered it's two features SetupDiGetClassDevs and SetupDiGetDeviceInstanceId I need. My question is am I looking in the right place i.e will these 2 functions return the path like above. Also what is the techinical name for this path?

I found what I think is a nice example (always learn better from example) on Microsoft's website cut and paste below but this throws up error C2440: '=' : cannot convert from 'HLOCAL' to 'LPTSTR' which is a new one on me a pointer error?

This is the code

   #include <stdio.h>
   #include <windows.h>
   #include <setupapi.h>
   #include <devguid.h>
   #include <regstr.h>

   int main( int argc, char *argv[ ], char *envp[ ] )
   {
       HDEVINFO hDevInfo;
       SP_DEVINFO_DATA DeviceInfoData;
       DWORD i;

       // Create a HDEVINFO with all present devices.
       hDevInfo = SetupDiGetClassDevs(NULL,
           0, // Enumerator
           0,
           DIGCF_PRESENT | DIGCF_ALLCLASSES );

       if (hDevInfo == INVALID_HANDLE_VALUE)
       {
           // Insert error handling here.
           return 1;
       }

       // Enumerate through all devices in Set.

       DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
       for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
           &DeviceInfoData);i++)
       {
           DWORD DataT;
           LPTSTR buffer = NULL;
           DWORD buffersize = 0;

           //
           // Call function with null to begin with, 
           // then use the returned buffer size (doubled)
           // to Alloc the buffer. Keep calling until
           // success or an unknown failure.
           //
           //  Double the returned buffersize to correct
           //  for underlying legacy CM functions that 
           //  return an incorrect buffersize value on 
           //  DBCS/MBCS systems.
           // 
           while (!SetupDiGetDeviceRegistryProperty(
               hDevInfo,
               &DeviceInfoData,
               SPDRP_DEVICEDESC,
               &DataT,
               (PBYTE)buffer,
               buffersize,
               &buffersize))
           {
               if (GetLastError() == 
                   ERROR_INSUFFICIENT_BUFFER)
               {
                   // Change the buffer size.
                   if (buffer) LocalFree(buffer);
                   // Double the size to avoid problems on 
                   // W2k MBCS systems per KB 888609. 
                   buffer = LocalAlloc(LPTR,buffersize * 2); // ERROR LINE
               }
               else
               {
                   // Insert error handling here.
                   break;
               }
           }

           printf("Result:[%s]\n",buffer);

           if (buffer) LocalFree(buffer);
       }


       if ( GetLastError()!=NO_ERROR &&
            GetLastError()!=ERROR_NO_MORE_ITEMS )
       {
           // Insert error handling here.
           return 1;
       }

       //  Cleanup
       SetupDiDestroyDeviceInfoList(hDevInfo);

       return 0;
   }

Hope its an easy one thank you.

Was it helpful?

Solution

You need to typecast the return value from LocalAlloc():

buffer = (LPSTR) LocalAlloc(LPTR,buffersize * 2);

For more info, see the LocalAlloc() documentation on MSDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top