🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to detect COM installed

#122
From: Colin B Maharaj
Date: Tue, 10 Jun 2008 03:38
46 lines
2583 bytes
Holy..... can't believe I did
that. Saw COM and thought it was
COM port...

That dreaded COM.



Peter Stefanos wrote:
> 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: <484df737$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4844feb4$1@newsgroups.borland.com> <484de9b4$1@newsgroups.borland.com> <484df3fc$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 23:38:13 -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> <484de9b4$1@newsgroups.borland.com> <484df3fc$1@newsgroups.borland.com>
In-Reply-To: <484df3fc$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: <484df737$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1213069111 190.58.101.173 (9 Jun 2008 20:38:31 -0700)
Lines: 46
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
X-Original-Bytes: 2389