//
// Example program to retrieve all the COM port names from the NT Registry
//
#include
#include
//
// This example stores the found COM names in this array. Your application
// may have some other place it wants to have COM names stored.
//
char CommList[256][64];
int GetCommList(void)
{
DWORD resVal;
HKEY hSerialCommKey = NULL;
DWORD i, status;
DWORD keyType, nameBufSize, dataBufSize;
UCHAR valueKeyName[64], valueKeyData[64];
//
// First - Get access to the registry key we need
//
resVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"HARDWARE\\DEVICEMAP\\SERIALCOMM",
0, KEY_READ, &hSerialCommKey);
//
// If successful, read all the registry entries under this key
if( resVal == ERROR_SUCCESS ) {
for( i = 0;; i++ ) {
nameBufSize = dataBufSize = 64;
status = RegEnumValue(
hSerialCommKey,
i,
(CHAR *)valueKeyName,
&nameBufSize,
NULL,
&keyType,
(UCHAR *)valueKeyData,
&dataBufSize
);
//
// If we got something, the value of the key is the COM name
// otherwise we've come to the end of the list.
//
if (status == ERROR_SUCCESS)
strcpy(CommList[i], (const char *)valueKeyData);
else
break;
}
//
// Done, close down the registry before returning
//
RegCloseKey(hSerialCommKey);
//
// And return the number we found
//
return i;
}
else
return -1; // Return -1 as an error if we can't access the registry
}
void main(void) {
DWORD count;
DWORD i;
if((count = GetCommList()) >= 0) {
printf("Found %d COM ports\n", count);
for (i = 0; i < count; i++)
printf("%s\n", CommList[i]);
}
} |