Visual Studio错误,正在获取设备GUID和路径名

Visual Studio errors and getting a device GUID and path name?

本文关键字:GUID 路径名 获取 Studio 错误 Visual      更新时间:2023-10-16

晚上大家好,我想知道是否有人能快速回答我两个问题。

我制作了一个与arm设备通信的应用程序,它运行良好,但当我移动PC等时,我需要重新配置设备路径。这是一个像下面这样的长的。

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

我做了一些阅读,发现它有两个特性SetupDiGetClassDevsSetupDiGetDeviceInstanceId我需要。我的问题是,我是否在寻找正确的位置,即这两个函数是否会返回如上所述的路径。这个路径的技术名称是什么?

我在下面的微软网站上找到了一个我认为很好的例子(总是从例子中学到更好的东西),但这引发了错误C2440: '=' : cannot convert from 'HLOCAL' to 'LPTSTR'哪一个是我的新指针错误?

这是代码

   #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;
   }

希望这很容易,谢谢。

您需要键入LocalAlloc():的返回值

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

有关更多信息,请参阅MSDN上的LocalAlloc()文档。