Consultor Eletrônico



Kbase 21622: How to read a Windows registry key calling a Win32 API
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   4/4/2011
Status: Verified

GOAL:

How to call the Win32 API function RegOpenKeyEx?

GOAL:

How to call the Win32 API function RegQueryValueEx?

GOAL:

How to call the Win32 API function RegCloseKey?

GOAL:

How to read a registry key

GOAL:

Alternative to the GET-KEY-VALUE statement

FACT(s) (Environment):

Progress 9.x
OpenEdge 10.x
Windows

FIX:

The code below returns the value of the DLC variable located in:

HKEY_CURRENT_USER\Software\PSC\PROGRESS\9.1C\Startup

DEFINE VARIABLE SubKey AS CHARACTER NO-UNDO.
DEFINE VARIABLE qKey AS CHARACTER NO-UNDO.
DEFINE VARIABLE hKey AS INTEGER NO-UNDO.

DEFINE VARIABLE bufSize AS INTEGER NO-UNDO.
DEFINE VARIABLE res AS INTEGER NO-UNDO.

DEFINE VARIABLE hKeyResult AS INTEGER NO-UNDO.
DEFINE VARIABLE lpData AS MEMPTR NO-UNDO.
DEFINE VARIABLE dataType AS INTEGER NO-UNDO.
DEFINE VARIABLE SamDesired AS INTEGER NO-UNDO.

&GLOBAL-DEFINE HKEY_CURRENT_USER -2147483647.
&GLOBAL-DEFINE HKEY_LOCAL_MACHINE -2147483646.
&GLOBAL-DEFINE HKEY_CURRENT_CONFIG -2147483643.
&GLOBAL-DEFINE HKEY_CLASSES_ROOT -2147483648.

&GLOBAL-DEFINE KEY_QUERY_VALUE 1 .

/* Initialization */
hKey = {&HKEY_CURRENT_USER} .
SamDesired = {&KEY_QUERY_VALUE} .

SubKey = "Software\PSC\PROGRESS\9.1C\Startup\".
qKey = "DLC".

bufSize = 255.
SET-SIZE(lpData) = bufSize .

/* Opens the specified key */
RUN RegOpenKeyExA (
INPUT hKey, /* handle of open key */
INPUT SubKey, /* name of subkey to open */
INPUT 0, /* reserved */
INPUT SamDesired, /* security access mask */
OUTPUT hKeyResult, /* handle of open key */
OUTPUT res).

/* Retrieves data for a specified value name
associated with an open registry key */
RUN RegQueryValueExA (
INPUT hKeyResult, /* handle of key to query */
INPUT qKey, /* name of value to query */
INPUT 0, /* reserved */
OUTPUT datatype, /* address of buffer for value type */
GET-POINTER-VALUE(lpData), /* address of data buffer */
OUTPUT bufSize, /* data buffer size */
OUTPUT res).

/* Releases the handle of the specified key */
RUN RegCloseKey (
INPUT hKeyResult, /* handle of key to close */
OUTPUT res).

MESSAGE qKey ": " GET-STRING(lpData, 1) VIEW-AS ALERT-BOX.

/* ********* Procedure definitions ********* */

PROCEDURE RegOpenKeyExA EXTERNAL "advapi32" :
DEFINE INPUT PARAMETER hkey AS LONG.
DEFINE INPUT PARAMETER lpSubKey AS CHARACTER.
DEFINE INPUT PARAMETER ulOptions AS LONG.
DEFINE INPUT PARAMETER samDesired AS LONG.
DEFINE OUTPUT PARAMETER phkResult AS LONG.
DEFINE RETURN PARAMETER RetResult AS LONG.
END PROCEDURE.

PROCEDURE RegQueryValueExA EXTERNAL "advapi32" :
DEFINE INPUT PARAMETER hkey AS LONG.
DEFINE INPUT PARAMETER lpValueName AS CHARACTER.
DEFINE INPUT PARAMETER lpdwReserved AS LONG.
DEFINE OUTPUT PARAMETER lpdwType AS LONG.
DEFINE INPUT PARAMETER lpbData AS LONG.
DEFINE OUTPUT PARAMETER lpcbData AS LONG.
. DEFINE RETURN PARAMETER lpresult AS LONG.
END PROCEDURE.

PROCEDURE RegCloseKey EXTERNAL "advapi32" :
DEFINE INPUT PARAMETER hkey AS LONG.
DEFINE RETURN PARAMETER lpresult AS LONG.
END PROCEDURE..