Application Note - Finding COM Ports on NT
ports via NativeCOM to the NT system. How does your application know which ports have been defined in the system? The answer: the Windows NT Registry.

The Windows NT registry's hardware devicemap contains the COM ports defined in the system. You can see this key using regedit or regedt32. For example:

 
HKEY_LOCAL_MACHINE
    HARDWARE
        DEVICEMAP
            SERIALCOMM
                Serial0      REG_SZ  COM1
                Serial1      REG_SZ  COM2
                Rcs0Serial3  REG_SZ  COM3
                Rcs0Serial4  REG_SZ  COM4
  In this case, there are two serial ports built in to the machine, and at least three NativeCOM serial ports defined (these are identified as "RcsmSerialn"). The only part your application needs to know about is the "value" of the registry entries ("COM3" for instance)
  The following "C" code example illustrates how your application might traverse the Registry to find this information so it can display it for your user.
 
//
// 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]);
    }
}
          Optimized for 1024 X 768 pixels
Copyright © 2004 Systech Corporation. All Rights Reserved
Web partner
 
emantras