int WinFindCOMPortMappedToBluetoothDevice(
const BLUETOOTH_ADDRESS &btAddr,
cppstring *pCOMPortString)
{
int status = 0;
DWORD reqGuids = 16;
GUID guids[16];
HDEVINFO devs = INVALID_HANDLE_VALUE;
unsigned int ii;
bool bPortFound = false;
DWORD devIndex;
SP_DEVINFO_DATA devInfo;
devInfo.cbSize = sizeof(devInfo);
TCHAR tmpstring[60];
TCHAR hardware_id_string[300];
DWORD propertyType;
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
//Create search string that we are looking for from bluetooth address.
wsprintf(tmpstring, GSTD_S("%02x%02x%02x%02x%02x%02x"),
btAddr.rgBytes[5], btAddr.rgBytes[4], btAddr.rgBytes[3],
btAddr.rgBytes[2], btAddr.rgBytes[1], btAddr.rgBytes[0]);
CString searchString = tmpstring;
searchString.MakeUpper();
CString hwParsingString;
if (SetupDiClassGuidsFromNameEx(GSTD_S("ports"), guids, reqGuids, &reqGuids, NULL, NULL))
{
for (ii = 0; (ii < reqGuids) && (!bPortFound); ii++)
{
devs = SetupDiGetClassDevsEx(&guids[ii],NULL,NULL,DIGCF_PRESENT,NULL,NULL,NULL);
if (devs != INVALID_HANDLE_VALUE)
{
BOOL bSuccess = SetupDiGetDeviceInfoListDetail(devs, &devInfoListDetail);
if (bSuccess)
{
devIndex = 0;
while (SetupDiEnumDeviceInfo(devs,devIndex,&devInfo) && (!bPortFound))
{
status = CM_Get_Device_ID_Ex(devInfo.DevInst, hardware_id_string, 300, 0, devInfoListDetail.RemoteMachineHandle);
if (CR_SUCCESS == status)
{
hwParsingString = hardware_id_string;
if (hwParsingString.Find(searchString) >= 0)
{
bSuccess = SetupDiGetDeviceRegistryProperty(devs, &devInfo,
SPDRP_FRIENDLYNAME, &propertyType,
(PBYTE) hardware_id_string, sizeof(hardware_id_string), NULL);
if (bSuccess)
{
hwParsingString = hardware_id_string;
int firstIndex = hwParsingString.Find(GSTD_S("(COM"));
if (firstIndex >= 0)
{
firstIndex++;
int lastIndex = hwParsingString.Find(GSTD_S(")"), firstIndex);
if (lastIndex > firstIndex)
{
*pCOMPortString = hwParsingString.Mid(firstIndex, lastIndex - firstIndex);
bPortFound = true;
}
}
}
}
}
devIndex++;
}
}
SetupDiDestroyDeviceInfoList(devs);
}
}
}
return (bPortFound ? 0 : -1);
}
Voltar para Visual C++/C/C++/C#
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante