2015-10-05 17 views
0

我試圖通過使用CreateFile()從藍牙低功耗設備獲取一個HANDLE。 因此我需要提取設備的設備路徑。 當調用SetupDiEnumDeviceInterfaces時,我得到一個ERROR_INVALID_PARAMETER錯誤。看起來第二個參數(DeviceInfoData)有問題。 任何想法可能是什麼問題?SetupDiEnumDeviceInterfaces在查詢藍牙設備時返回ERROR_INVALID_PARAMETER

編輯:簡化代碼

HDEVINFO hDevInfo; 
DWORD i; 

// Create a HDEVINFO with all present devices. 
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, 0, 0, DIGCF_PRESENT); 

if (hDevInfo == INVALID_HANDLE_VALUE) 
{ 
    // Insert error handling here. 
    return;//1; 
} 
PSP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA; 
DeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA); 

for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) 
{ 
    DeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA); 

    char detailDataBuf[0x100]; 
    PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)detailDataBuf; 
    ULONG length; 
    ULONG requiredLength = 0; 
    bool bResult = FALSE; 

    for(DWORD j = 0; j < 10; j++) 
    { 
     SP_DEVICE_INTERFACE_DATA interfaceData; 
     interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 
     bResult = SetupDiEnumDeviceInterfaces(hDevInfo, DeviceInfoData, &GUID_DEVCLASS_BLUETOOTH, j, &interfaceData); 
     if (!bResult) { 
      int lastError = GetLastError(); // always returns ERROR 259 
      continue; 
     } 
     // Get the size of the buffer required to receive the device info 
     SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, 0, &requiredLength, NULL); 
     if(requiredLength >= sizeof(detailDataBuf)) 
      break; 

     // Get the name of the device 
     detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); 
     length = requiredLength; 
     bResult = SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, detailData, length, &requiredLength, NULL) != 0; 
     if(!bResult) 
      break; 
    } 
} 

EDITED2: 傳遞NULL爲DeviceInfoData:這個簡單的情況下,始終返回false

HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 

bool bResult = FALSE; 

for(DWORD j = 0; j < 10; j++) 
{ 
    SP_DEVICE_INTERFACE_DATA interfaceData; 
    interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 
    bResult = SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &GUID_DEVCLASS_BLUETOOTH, j, &interfaceData); 
    if (!bResult) { 
     int lastError = GetLastError(); // ERROR 259 
     continue; 
    } 
} 

回答

0

文檔說:

DeviceInfoData [輸入,可選]

指向SP_DEVINFO_DATA結構的指針,該結構指定DeviceInfoSet中的設備信息元素。

換句話說,它必須指向一個deviceInfo的元素,它傳遞的指針不是。如果您不想將結果過濾到設備信息集中特定設備的接口,請通過NULL

(注意,這是一個輸入參數,由「在」的標籤,輸出經由第五參數傳遞,DeviceInterfaceData。如所示)

+0

謝謝,我編輯的代碼以指向HDEVINFO但我仍然得到相同的錯誤。不應該SetupDiEnumDeviceInfo填充DeviceInfoData? – user2246120

+0

新代碼有相同的錯誤,即'DeviceInfoData'不指向設備信息集的一個元素。關於你的評論,不,該函數不會填寫'DeviceInfoData'。這是一個輸入參數,而不是輸出參數。 –

+0

但它是'SetupDiEnumDeviceInfo'的輸出參數,用作'SetupDiEnumDeviceInterfaces'的輸入,對嗎?我也嘗試過爲deviceInfo傳遞NULL,但是這也不起作用。任何其他想法這裏發生了什麼? – user2246120