Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by A Nonymous
Tue, 13 Mar 2007 23:04
how to enumerate serial ports under vista?
Author: A Nonymous
Date: Tue, 13 Mar 2007 23:04
Date: Tue, 13 Mar 2007 23:04
10 lines
1209 bytes
1209 bytes
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?
Re: how to enumerate serial ports under vista?
Author: 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.
Re: how to enumerate serial ports under vista?
Author: A Nonymous
Date: Thu, 15 Mar 2007 15:21
Date: Thu, 15 Mar 2007 15:21
39 lines
2483 bytes
2483 bytes
Jim, Thanks for the reply. I used to use code like this, and guess I will have to go back to it... Reading the registry was just so much easier and quicker. Anyhow, the Vista problems are getting worse. When I first did our Vista compatibility testing, I checked everywhere we used the registry, among other things... The serial port thing worked fine. After releasing a Vista compatibility update, serverl users were complaiing about not seeing any serial ports, but for some it worked fine. I retested and suddenly it wasn't working for me either. The problem reports had come from our tech support, who did their own test and kept tellig users it worked fine. When I told them it was broken the seemed surprised. We were both using the same Vista PC for testing. Under my account it stopped working, but on their account it was still working fine. The user is supposed to be using an admin equivelant account for our software and both of our accounts apperaed to be the same... After looking around I happened to open the device manager under the TECHSUPPORT account/profile. All I did was look to see what ports showed up, absolutely no changes were made. I ran our app again and now the serial ports didn't show up! I created another test account on the Vista PC and installed our software. The serial ports showed up. I opened the device manager, ran our app again and no serial ports... Why should opening the device manager have anything to do with this? I hate black magic! I think Vista is going to be a PROBLEM! Sorry for the rant. Thanks again for the code snippet.
Re: how to enumerate serial ports under vista?
Author: Mark Jacobs
Date: Mon, 19 Mar 2007 12:01
Date: Mon, 19 Mar 2007 12:01
18 lines
1979 bytes
1979 bytes
"A Nonymous" <anonymous@hotmail.com> wrote in message news:Xns98F45F2B4A9CDanonymoushotmailcom@207.105.83.66... > After looking around I happened to open the device manager under the > TECHSUPPORT account/profile. All I did was look to see what ports > showed up, absolutely no changes were made. I ran our app again and now > the serial ports didn't show up! > > I created another test account on the Vista PC and installed our > software. The serial ports showed up. I opened the device manager, ran > our app again and no serial ports... Why should opening the device > manager have anything to do with this? Perhaps it is M$'s attempt at modelling a quantum universe where perceiving something changes it. -- Mark Jacobs DK Computing http://www.dkcomputing.co.uk
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads