Suppose there is a registry key
HKEY_LOCAL_MACHINE\Software\Nerdware\Test
and suppose it contains a DWORD value, named "count".
This source example will read the value of "count", identifies the datatype, and increment the value of "count" by one.
{windows.i} /* March 28, 1998 or later */ DEFINE VARIABLE key-hdl AS INTEGER NO-UNDO. DEFINE VARIABLE lpBuffer 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 icount AS INTEGER NO-UNDO. RUN RegOpenKeyA IN hpApi ( {&HKEY_LOCAL_MACHINE}, "Software\Nerdware\Test", OUTPUT key-hdl, OUTPUT reslt). IF reslt NE {&ERROR_SUCCESS} THEN DO: MESSAGE "key not found in registry" VIEW-AS ALERT-BOX. RETURN. END. /* read value of "count" into lpBuffer */ /* make buffer large, because we don't know for sure if it is really a DWORD. The maximum size is supposed to be MAX_PATH + 1 */ ASSIGN lth = {&MAX_PATH} + 1 SET-SIZE(lpBuffer) = lth. RUN RegQueryValueExA IN hpApi (key-hdl, "count", 0, /* reserved, must be 0 */ OUTPUT datatype, GET-POINTER-VALUE(lpBuffer), INPUT-OUTPUT lth, OUTPUT reslt). IF reslt NE {&ERROR_SUCCESS} THEN MESSAGE "value not found in registry" VIEW-AS ALERT-BOX. ELSE DO: CASE datatype : WHEN 1 THEN MESSAGE "datatype=STRING" SKIP "value=" GET-STRING(lpBuffer,1) VIEW-AS ALERT-BOX. WHEN 4 THEN MESSAGE "datatype=DWORD" SKIP "value=" GET-LONG(lpBuffer,1) VIEW-AS ALERT-BOX. OTHERWISE MESSAGE "unexpected datatype:" SKIP "datatype=" datatype SKIP VIEW-AS ALERT-BOX. END CASE. /* if it is a DWORD, then increment the value */ IF datatype=4 THEN DO: icount = GET-LONG(lpBuffer,1). SET-SIZE(lpBuffer) = 4. /* =sizeof(DWORD) */ PUT-LONG(lpBuffer,1) = icount + 1. RUN RegSetValueExA IN hpApi (key-hdl, "count", INPUT 0, /* reserved, must be 0 */ INPUT 4, /* = REG_DWORD */ INPUT GET-POINTER-VALUE(lpBuffer), INPUT 4, /* = get-size(lpBuffer) */ OUTPUT reslt). IF reslt NE {&ERROR_SUCCESS} THEN MESSAGE "can not write value in registry" VIEW-AS ALERT-BOX. END. END. SET-SIZE(lpBuffer)=0. RUN RegCloseKey IN hpApi (key-hdl,OUTPUT reslt).
function RegSetValueEx will create the value "count" if it did not exist yet. So even if RegQueryValue did not find the value, you can still set one. Of course you will have to change the logic of this source example to allow that.
There are more datatypes than just REG_SZ (=1) and REG_DWORD (=4). Check your API documentation for a complete list.