主要功能简介:
核心函数就是Scan。不仅可以扫描出实际的蓝牙,打印机等COM端口,而且还可以扫描出虚拟机来的COM端口。
不仅可以显示名称,还可以显示生产厂商的具体参数
/* func name: Scan description: Scan and display the COM port information This is the main function of the app. It detect your port, as well as the virtual port. */ HRESULT CSmartScanerDlg::Scan(void) { HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i; // Create a HDEVINFO with all present devices. hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVCLASS_PORTS, 0, // Enumerator 0, DIGCF_PRESENT); 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 // to Alloc the buffer. Keep calling until // success or an unknown failure. // while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buffer, buffersize, &buffersize)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) LocalFree(buffer); buffer = (LPTSTR)LocalAlloc(LPTR,buffersize); } else { // Insert error handling here. break; } } //printf("Result:[%s]/n",buffer); /** disp is MFC Edit Control CString variable. here, I just use it display the scan results. the buffer just contains the result. and if you want to get other information ,just change SPDRP_FRIENDLYNAME(please look up in MSDN) */ disp.Append(buffer); disp.Append(_T("/r/n")); conEdit.SetWindowTextW(disp.GetString()); } SetupDiDestroyDeviceInfoList(hDevInfo); if (!wcscmp(disp.GetString(), _T("/r/n"))){ MessageBox(_T("no device find"), _T("prompt")); } return S_OK; }