MFC 掃描COM連接埠程式
MFC 掃描COM連接埠程式原始碼
主要功能簡介:
核心函數就是Scan。不僅可以掃描出實際的藍芽,印表機等COM連接埠,而且還可以掃描出虛擬機器來的COM連接埠。
不僅可以顯示名稱,還可以顯示生產廠商的具體參數
/*<br />func name: Scan<br />description: Scan and display the COM port information<br />This is the main function of the app. It detect your port, as well as the virtual port.<br />*/</p><p>HRESULT CSmartScanerDlg::Scan(void)<br />{</p><p>HDEVINFO hDevInfo;<br />SP_DEVINFO_DATA DeviceInfoData;<br />DWORD i;</p><p>// Create a HDEVINFO with all present devices.<br />hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVCLASS_PORTS,<br />0, // Enumerator<br />0,<br />DIGCF_PRESENT);</p><p>if (hDevInfo == INVALID_HANDLE_VALUE)<br />{<br />// Insert error handling here.<br />return 1;<br />}</p><p>// Enumerate through all devices in Set.</p><p>DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);<br />for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,<br />&DeviceInfoData);i++)<br />{<br />DWORD DataT;<br />LPTSTR buffer = NULL;<br />DWORD buffersize = 0;</p><p>//<br />// Call function with null to begin with,<br />// then use the returned buffer size<br />// to Alloc the buffer. Keep calling until<br />// success or an unknown failure.<br />//<br />while (!SetupDiGetDeviceRegistryProperty(<br />hDevInfo,<br />&DeviceInfoData,<br />SPDRP_FRIENDLYNAME,<br />&DataT,<br />(PBYTE)buffer,<br />buffersize,<br />&buffersize))<br />{<br />if (GetLastError() ==<br />ERROR_INSUFFICIENT_BUFFER)<br />{<br />// Change the buffer size.<br />if (buffer) LocalFree(buffer);<br />buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);<br />}<br />else<br />{<br />// Insert error handling here.<br />break;<br />}<br />}</p><p>//printf("Result:[%s]/n",buffer);</p><p>/**<br />disp is MFC Edit Control CString variable.<br />here, I just use it display the scan results.<br />the buffer just contains the result.<br />and if you want to get other information ,just change<br />SPDRP_FRIENDLYNAME(please look up in MSDN)<br />*/<br />disp.Append(buffer);<br />disp.Append(_T("/r/n"));<br />conEdit.SetWindowTextW(disp.GetString());</p><p>} </p><p>SetupDiDestroyDeviceInfoList(hDevInfo); </p><p>if (!wcscmp(disp.GetString(), _T("/r/n"))){<br />MessageBox(_T("no device find"), _T("prompt"));<br />}</p><p>return S_OK;<br />}
MFC 掃描COM連接埠程式原始碼