I'm writing an application that reads data from a Windows CE device's serial
port. So far so good (I'm using the API's standard functions to do this)...
What I can't figure out (and I've looked all over the place - MSDN, the rest
of the Internet, etc.) is how I can ask the device what serail ports are
available (so that, for example, I can display all the available ports in a
dropdown, and allow the user to select the serial port that they want to
use).
If anyone can point me in the direction of the information that I need to
look at, I'd be most appreciative.
Thanks in advance...
Dave Bruzzone
This isn't some kind of blessed code, but it works on our device:
Paul T.
int CountSerialPorts()
{
HKEY basekey;
int lastfound = 0;
// Open and enumerate the key [HKEY_LOCAL_MACHINE\Drivers\Active]
if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T( "\\Drivers\\Active" ),
0, 0, &basekey ) == ERROR_SUCCESS )
{
HKEY key;
DWORD index = 0;
TCHAR keyname[ 256 ];
// Read HKEY_LOCAL_MACHINE\Drivers\Active, looking for COMx: ports.
// We could also just look through the BuiltIn section, but this should,
// in theory at least, allow PC Card-based serial ports to be detected.
while ( 1 )
{
// This *has* to get set each time, since it is also returned
// when RegEnumKeyEx() is called.
DWORD keynamesize = sizeof( keyname ) / sizeof( keyname[ 0 ] );
if ( RegEnumKeyEx( basekey, index, keyname, &keynamesize, NULL,
NULL, NULL, NULL ) != ERROR_SUCCESS )
{
break;
}
// We have the name of the key. Open it and see if it is a serial
// port.
if ( RegOpenKeyEx( basekey, keyname, 0, 0, &key ) == ERROR_SUCCESS )
{
DWORD nametype;
TCHAR name[ 256 ];
DWORD namesize = sizeof( name ) / sizeof( name[ 0 ] );
// Check for serial port.
// There should be a Name subkey whose value will be COMx for
// a serial port.
if ( RegQueryValueEx( key, _T( "Name" ), NULL, &nametype,
(unsigned char *)&name[ 0 ], &namesize ) == ERROR_SUCCESS )
{
// Check the name for COM.
int val;
if ( _stscanf( name, _T( "COM%d:" ), &val ) == 1 )
{
// If the value is bigger than the current value, update
// it.
if ( val > lastfound )
lastfound = val;
}
}
// Close key.
RegCloseKey( key );
}
// Go to next installed driver.
index++;
}
// Close base key.
RegCloseKey( basekey );
}
return lastfound;
}
--
Steve Maillet
Entelechy Software Consulting
smaillet 'AT' EntelechyConsulting 'DOT' com
http://www.EntelechyConsulting.com
"Davide Bruzzone" <dbru...@home.com> wrote in message
news:#V8WbFNrAHA.1840@tkmsftngp03...
Thanks very much... It may not be blessed, but if it works...
Cheers...
Dave
"Paul G. Tobey" <nos...@nospam.com> wrote in message
news:#jteBWNrAHA.1408@tkmsftngp05...
> Dave,
>
> This isn't some kind of blessed code, but it works on our device:
>
> Paul T.
>
> int CountSerialPorts()
> {
<Snip>
This seems like the easiest way to do what I need to do, but if I can't
easily use the function in a Windows CE application, I'll have to do what I
need to do via the registry.
Cheers...
Dave
"Steve Maillet" <nos...@nospam.com> wrote in message
news:uGR6veNrAHA.2112@tkmsftngp05...