Article View: borland.public.cppbuilder.nativeapi
Article #2338Re: how to enumerate serial ports under vista?
From: Jim Dodd
Date: Wed, 14 Mar 2007 18:40
Date: Wed, 14 Mar 2007 18:40
83 lines
3724 bytes
3724 bytes
A Nonymous wrote:
> I have an app that needs to know what serial ports are available. I
> currently just open the registry key HKEY_LOCAL_MACHINE\hardware\devicemap
> \serialcomm and get the port names. This has worked for years, but under
> Vista it does not. I looked with regedit and the names are there but the
> open key fails. I suspect it is some "enhanced" security feature...
>
> As I said I have been using it for a long time because it worked, but
> perhaps there is a better "recommended" way of doing this?
>
> Does anybody know of another way?
I have stayed away from finding serial ports by examining the Registry
because I found instances where the Registry said a port existed but
it did not (perhaps it once existed but was removed?) Anyway, to get
back to your question, the code below will check for installed ports
and also checks that, if they are installed, are they available for
use (not in use by another application). I copied this from a larger
program so beware of small errors.
int FindPorts(TCheckBox *AvailPorts)
{
AvailPorts->Items->Clear();
for (int i = 1; i <= 99; i++)
{
//Form the Raw device name
String sPort = String("\\\\.\\COM")+String(i);
bool bInstalled = false; // physically installed?
bool bAvailable = false; // installed and available?
//Try to open the port
HANDLE hPort = ::CreateFile(sPort.c_str(), GENERIC_READ |
GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hPort == INVALID_HANDLE_VALUE)
{
DWORD dwError = GetLastError();
//Check to see if the error was because some other
//app had the port open or a general failure
if (dwError == ERROR_ACCESS_DENIED ||
dwError == ERROR_GEN_FAILURE)
{
bInstalled = true;
}
}
else
{
//The port was opened successfully
bInstalled = true;
//The port is not in use by another application
bAvailable = true;
//Don't forget to close the port, you can open it
//again later when needed
CloseHandle(hPort);
}
//Add the port number to the string list
if (bInstalled) // if the port is physically installed...
{
String portName = String("COM")+String(i);
AvailPorts->Items->Add(portName);
//if the installed port is not already in use...
if( bAvailable )
{
AvailPorts->Checked[AvailPorts->Items->Count-1] = true;
}
//else the installed port is in use by another application
else
{
AvailPorts->Checked[AvailPorts->Items->Count-1] = false;
}
}
}
}
Regards,
Jim Dodd
Onset Computer Corp.
Technical Details
Message-ID:
<45f8418a$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.germany.com!news.belwue.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<Xns98F2AD9E12FA4anonymoushotmailcom@207.105.83.66>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ac871bf8921dea56 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Wed, 14 Mar 2007 14:40:21 -0400 From: Jim Dodd <bcbuilderboy@yahoo.com> User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: how to enumerate serial ports under vista? References: <Xns98F2AD9E12FA4anonymoushotmailcom@207.105.83.66> In-Reply-To: <Xns98F2AD9E12FA4anonymoushotmailcom@207.105.83.66> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.75.243.228 Message-ID: <45f8418a$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1173897610 24.75.243.228 (14 Mar 2007 11:40:10 -0800) Lines: 83 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.germany.com!news.belwue.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail