2012-07-25 57 views
0

晚上每個人,我想知道如果有人能回答我2個簡單的問題。Visual Studio錯誤並獲取設備GUID和路徑名稱?

我做了一個應用程序與手臂設備進行通信工作正常,但是當我移動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; 
    } 

希望其容易的謝謝你。

+0

歡迎來到StackOverflow。如果您指出哪條線路導致了錯誤,而不是讓我們猜測或試圖找出問題,那麼它可能會非常有幫助,您不覺得嗎?你可以編輯你的問題並添加某種評論,或者至少在你的問題文本中提到,以表明這一點?謝謝。 – 2012-07-25 01:51:53

+0

嗨肯對不起,我削減了行號62行我已經標記在上面。完全錯誤的值是類型「HLOCAL」不能分配給類型爲「LPTSTR」的實體 錯誤C2440:'=':無法從'HLOCAL'轉換爲'LPTSTR' 謝謝 – user1241548 2012-07-25 02:00:06

回答

0

您需要從LocalAlloc()類型轉換的返回值:

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

欲瞭解更多信息,請參見MSDN上LocalAlloc()文檔。

+0

感謝ken that和#pragma評論(lib,「SetupAPI」)工作。現在來學習:-)。 – user1241548 2012-07-25 02:05:02