Enumerating registry values

This example enumerates all values in a given registry key.
In this case, it will return a list of all installed printers (with their names and ports), so it actually does the same as the EnumPrinters function.
As explained in the registry introduction you should prefer to use the EnumPrinters function instead of this RegEnumA stuff, because Microsoft may decide to store the printers info elsewhere in the future, while the EnumPrinters API will be maintained.

{windows.i}  /* March 28, 1998 or later */
 
DEFINE VARIABLE hKey        AS INTEGER NO-UNDO.
DEFINE VARIABLE hPrinterkey AS INTEGER NO-UNDO.
DEFINE VARIABLE subkey      AS CHARACTER    NO-UNDO.
DEFINE VARIABLE port        AS MEMPTR  NO-UNDO.
DEFINE VARIABLE lth         AS INTEGER NO-UNDO.
DEFINE VARIABLE reslt       AS INTEGER NO-UNDO.
DEFINE VARIABLE datatype    AS INTEGER NO-UNDO.
DEFINE VARIABLE ITEM        AS INTEGER NO-UNDO.
 
RUN RegOpenKeyA IN hpApi( {&HKEY_LOCAL_MACHINE},
                          "System\CurrentControlSet\control\Print\Printers",
                          OUTPUT hKey,
                          OUTPUT reslt).
 
ASSIGN ITEM  = 0
       reslt = 0.
 
DO WHILE reslt NE {&ERROR_NO_MORE_ITEMS} :
 
   ASSIGN lth     = {&MAX_PATH} + 1
          subkey  = FILL("x", lth).
 
   RUN RegEnumKeyA IN hpApi (hKey, 
                             ITEM, 
                             OUTPUT subkey, 
                             INPUT LENGTH(subkey), 
                             OUTPUT reslt).
 
   IF reslt NE {&ERROR_NO_MORE_ITEMS} THEN DO:
 
      /* get the printer port (or description..) */
      RUN RegOpenKeyA IN hpApi ( hKey,
                                 subkey,
                                 OUTPUT hPrinterkey,
                                 OUTPUT reslt).
      lth  = {&MAX_PATH} + 1.
      SET-SIZE(port) = lth.
      RUN RegQueryValueExA IN hpApi (hPrinterkey,
                                     "port",
                                     0,  /* reserved, must be 0 */
                                     OUTPUT datatype,
                                     GET-POINTER-VALUE(port),
                                     INPUT-OUTPUT lth,
                                     OUTPUT reslt).
      RUN RegCloseKey IN hpApi (hPrinterkey,OUTPUT reslt).
 
      MESSAGE "printer name=" subkey SKIP 
              "port="         GET-STRING(port,1)
              VIEW-AS ALERT-BOX.
 
   END.
 
   ITEM = ITEM + 1.
END. /* do while not ERROR_NO_MORE_ITEMS */
 
SET-SIZE(port)=0.       
RUN RegCloseKey IN hpApi (hKey,OUTPUT reslt).

Notes

Obviously a (sub)key may contain values with different datatypes, like strings and numbers. You should test the 'datatype' parameter before you interpret a value. For an example of this, see RegQueryValueEx