MFC 掃描COM端口程序
主要功能簡介:
核心函數就是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; }
最後更新:2017-04-02 06:51:32