Re: How to get available COM-ports

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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

.



Relevant Pages

  • Re: How to block a port on Hp3000 system
    ... Beechglen has a nice article on this and other HP3000 ports. ... Regards, ... Behalf Of Reggie Monroe ... * To join/leave the list, search archives, change list settings, * ...
    (comp.sys.hp.mpe)
  • RE : proxy tunneling
    ... In general if other ports than 80 are blocked in a company it is for ... Best regards, ... It supports proxy tunneling and uses port 80 to pass ...
    (Security-Basics)
  • Re: Open port USB with vb.net in PDA | Abrir el puerto USB con vb.net en PDA
    ... Regards, ... > But you can use drivers that control that ports some other ports or ... >> Sean Gahan ...
    (microsoft.public.windowsxp.embedded)
  • Re: Why not serial port component in .NET?
    ... A serial communication component will be included in VS 2005. ... Regards, ... > CreateFileAPI to access the COM ports to read/write synchronously, ... > and response to events. ...
    (microsoft.public.dotnet.framework)
  • Re: Test Com Ports Xscale Wince 4.2
    ... Regards ... to open a connection to the various ports on your platform. ... > Windows CE Technical Group ... The touch driver and audio driver were taken from the Xscale BSP ...
    (microsoft.public.windowsce.platbuilder)