🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to detect COM installed

#120
From: Colin B Maharaj
Date: Tue, 10 Jun 2008 02:40
106 lines
4086 bytes
Firstly, sorry the code is poorly designed, but works!
I cut and paste from some code I have. Call EnumSerialPorts() and
return it into an AnsiString. The return value is a csv list of the
COM ports that exists. NOTE: This code does not say if it is in use.

If you call code to open a comport and it cannot be opened then
either it is in use or it does not exists.

//-------------------------------------------------------
   bool COM1, COM2, COM3, COM4;
   bool 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 = "";
    if (Win32Platform == VER_PLATFORM_WIN32_NT)
    {
      COMExists("\\Device\\Serial0"); // NT/2k/xp
      COMExists("\\Device\\Serial1");
      COMExists("\\Device\\Serial2");
      COMExists("\\Device\\Serial3");
      COMExists("\\Device\\Serial4");
      COMExists("\\Device\\Serial5");
      COMExists("\\Device\\Serial6");
      COMExists("\\Device\\Serial7");
    }
    else
    {
      COMExists("COM1"); // 9x
      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;
}

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



James5950 wrote:
> Dear All,
> 
> How to detect COM install on user system?
> 
> Thansk any comments and help.
> Best regards,
> James
> 
Technical Details
Message-ID: <484de9b4$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4844feb4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e3f9e3d7cc319589
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Mon, 09 Jun 2008 22:40:35 -0400
From: Colin B Maharaj <noreply@myhost.com>
User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: How to detect COM installed
References: <4844feb4$1@newsgroups.borland.com>
In-Reply-To: <4844feb4$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 080609-2, 06/09/2008), Outbound message
X-Antivirus-Status: Clean
NNTP-Posting-Host: 190.58.101.173
Message-ID: <484de9b4$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1213065652 190.58.101.173 (9 Jun 2008 19:40:52 -0700)
Lines: 106
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail