🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #997

Re: Enum com ports ignore modems etc

#997
From: Colin B Maharaj
Date: Thu, 29 Nov 2007 23:39
124 lines
4411 bytes
Nope, you may need to do some further digging
to enumerate modems in the registry, then probably
subtract this from all available ports.

I have some quick and dirty code to enumerate
existing COM ports, weather open or not.
However, it is not perfect, and you will need to
clean it up yourself. :)

Good luck....

//-----------------------------------------------------------------------------

bool COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8;

bool COMExists(char *ComStr)
{
    HKEY hKey;
    DWORD keyType   = 0;
    DWORD retValue  = 0;
    DWORD keyLength = 0;
    char *t = new char [MAX_PATH];
    char *CV = new char [256];
    strcpy(CV, "Hardware\\DeviceMap\\SerialComm"); // safe
    retValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, CV, NULL, KEY_READ, &hKey);
    if (retValue != ERROR_SUCCESS)
    {
      delete []CV;
      delete []t;
      return false;
    }
    RegQueryValueEx(hKey, ComStr, NULL, &keyType, NULL, &keyLength);
    retValue = RegQueryValueEx(hKey, ComStr, NULL, &keyType, (UCHAR *)t, 
&keyLength);
    RegCloseKey(hKey);

    if (retValue != ERROR_SUCCESS)
    {
       delete []CV;
       delete []t;
       return false;
    }
    if (strcmp(t, "COM1")==0) COM1 = true;
    if (strcmp(t, "COM2")==0) COM2 = true;
    if (strcmp(t, "COM3")==0) COM3 = true;
    if (strcmp(t, "COM4")==0) COM4 = true;
    if (strcmp(t, "COM5")==0) COM5 = true;
    if (strcmp(t, "COM6")==0) COM6 = true;
    if (strcmp(t, "COM7")==0) COM7 = true;
    if (strcmp(t, "COM8")==0) COM8 = true;
    delete []CV;
    delete []t;
    return true;
}

//-----------------------------------------------------------------------------

AnsiString EnumSerialPorts()
{
    AnsiString COMcsv = "";
   OSVERSIONINFO osv;
   osv.dwOSVersionInfoSize = sizeof(osv);
   GetVersionEx(&osv);
   if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
      COMExists("\\Device\\Serial0");
      COMExists("\\Device\\Serial1");
      COMExists("\\Device\\Serial2");
      COMExists("\\Device\\Serial3");
      COMExists("\\Device\\Serial4");
      COMExists("\\Device\\Serial5");
      COMExists("\\Device\\Serial6");
      COMExists("\\Device\\Serial7");
    }
    else
    {
      COMExists("COM1");
      COMExists("COM2");
      COMExists("COM3");
      COMExists("COM4");
      COMExists("COM5");
      COMExists("COM6");
      COMExists("COM7");
      COMExists("COM8");
    }
    if (COM1 == true) COMcsv = "COM1";
    if (COM2 == true) COMcsv += "\,COM2";
    if (COM3 == true) COMcsv += "\,COM3";
    if (COM4 == true) COMcsv += "\,COM4";
    if (COM5 == true) COMcsv += "\,COM5";
    if (COM6 == true) COMcsv += "\,COM6";
    if (COM7 == true) COMcsv += "\,COM7";
    if (COM8 == true) COMcsv += "\,COM8";

    return  COMcsv;
}

//-----------------------------------------------------------------------------





northern_RATT wrote:
> Hi Folks;
> 
> I need to populate a menu w/ 
> available COM ports. I know how to 
> do this with CreatFile(dwAccess=0).
> Fine & Dandy, But if this port is 
> attached to a modem, IR port, 
> Bluetooth device then I want to 
> skip it. I'm only interested in 
> RS232, 9 pin D connector, COM 
> ports. So my question is: If I get 
> a valid handle back form CreateFile 
> is there a good way to determine 
> what is on the other end?
> 
> Thanks
> Drew 
> 
> 
Technical Details
Message-ID: <474f4db2$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <474e3f47$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e757ae01059b0e21
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Thu, 29 Nov 2007 19:39:32 -0400
From: Colin B Maharaj <noreply@myhost.com>
User-Agent: Thunderbird 2.0.0.9 (Windows/20071031)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Enum com ports ignore modems etc
References: <474e3f47$1@newsgroups.borland.com>
In-Reply-To: <474e3f47$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 071129-0, 11/29/2007), Outbound message
X-Antivirus-Status: Clean
NNTP-Posting-Host: 190.58.101.127
Message-ID: <474f4db2$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1196379570 190.58.101.127 (29 Nov 2007 15:39:30 -0700)
Lines: 124
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail