Re: How to get available COM-ports
- From: "hughgray@xxxxxxxxxxxxx" <hughgray@xxxxxxxxxxxxx>
- Date: 18 Jul 2005 00:49:15 -0700
Kjetil Salomonsen wrote:
> I have used QueryDosDevice(). I wonder if there is a better method?
>
> Here is an example:
>
>
>
> void CSerialCommunication::GetAvailableComPorts()
> {
> OSVERSIONINFO osinfo;
> osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
> BOOL bGetVer = GetVersionEx(&osinfo);
> if(bGetVer && (osinfo.dwPlatformId == VER_PLATFORM_WIN32_NT))
> {
> TCHAR devices[65535];
> DWORD nChars = QueryDosDevice(NULL, devices, 65535);
> if(nChars)
> {
> int i = 0;
> for(;;)
> {
> TCHAR* pszCurrentDevice = &devices[i];
> size_t nLen = _tcslen(pszCurrentDevice);
> if(nLen > 3 && _tcsnicmp(pszCurrentDevice, _T("COM"), 3) == 0)
> {
> if( (pszCurrentDevice[3] >47) && (pszCurrentDevice[3] < 58) )
> {
> CString str;
> int nPort = _ttoi(&pszCurrentDevice[3]);
> str.Format("COM%d", nPort);
> AddDevice(str);
> }
> }
> while(devices[i] != _T('\0'))
> i++;
> i++;
> if(devices[i] == _T('\0'))
> break;
> }
> }
> }
> else
> {
> TRACE(_T("Failed to call QueryDosDevice, GetLastError: %d\n"),
> GetLastError());
> }
> }
>
>
> regards,
> Kjetil
Are you having any problems with the above code? The method I've used
in the past has been using the SetupDi... functions. The code is
slightly more bloated than yours because of that, but it seems to work
well.
One thing to consider adding, either as a final check or perhaps as a
seperate validation function - try a CreateFile(...) on each com port
to make sure it can be safely used. This depends though on whether you
just want to show all possible com ports or all com ports that are in a
usable state.
Regards,
Hugh Gray
.
- Follow-Ups:
- Re: How to get available COM-ports
- From: Kjetil Salomonsen
- Re: How to get available COM-ports
- References:
- How to get available COM-ports
- From: Kjetil Salomonsen
- How to get available COM-ports
- Prev by Date: CImageList DestroyImageList Problem
- Next by Date: Re: A queue structure for CArray
- Previous by thread: How to get available COM-ports
- Next by thread: Re: How to get available COM-ports
- Index(es):
Relevant Pages
|