Article View: borland.public.cppbuilder.nativeapi
Article #121Re: How to detect COM installed
From: Peter Stefanos
Date: Tue, 10 Jun 2008 03:24
Date: Tue, 10 Jun 2008 03:24
35 lines
2212 bytes
2212 bytes
Colin B Maharaj wrote:
>
> 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.
>
Ahhh you mean COM Port.
You can test the existance of a COM port using the CreateFile() function.
Here's a simple function to test if a COM port exists. (It's a little crude)
bool ComPortExists(int ComPortNumber)
{
HANDLE Handle;
char Buffer[100];
bool Ret = false;
sprintf(Buffer, "\\\\.\\COM%d", ComPortNumber);
Handle = CreateFile(Buffer, 0, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
// Notes: If Handle == INVALID_HANDLE_VALUE *AND*
// if errno == 5 the port does exist, but cannot be opened ...
// if errno == 2 doesn't exist, otherwise error.
if ((Handle != INVALID_HANDLE_VALUE) || (GetLastError() == 5)) {
CloseHandle(Handle); // Not sure if this is necessary. Probably not.
Ret = true;
}
return Ret;
}
Technical Details
Message-ID:
<484df3fc$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!newsfeed0.kamp.net!newsfeed.kamp.net!feeder.erje.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4844feb4$1@newsgroups.borland.com> <484de9b4$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: Tue, 10 Jun 2008 13:24:59 +1000 From: Peter Stefanos <peterATpennywiseDOTcomDOTau> 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> <484de9b4$1@newsgroups.borland.com> In-Reply-To: <484de9b4$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 202.45.113.90 Message-ID: <484df3fc$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1213068284 202.45.113.90 (9 Jun 2008 20:24:44 -0700) Lines: 35 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!newsfeed0.kamp.net!newsfeed.kamp.net!feeder.erje.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail