Previous Topic: RegSetValue - Associate the Registry Key with a Text StringNext Topic: String Manipulation Functions


RegSetVariable - Store a Variable in the Registry Key

Valid on Windows

The RegSetVariable function stores a variable in the specified registry key.

This registry manipulation function has the format:

RegSetVariable(hKey as Integer, name as String, value as Integer) as Boolean
RegSetVariable(hKey as Integer, name as String,	value as String) as Boolean
RegSetVariable(hkey as integer, name as string, buffer as void, bufsize as integer) as integer.
RegSetVariable(hkey as integer, name as string, buffer as void, bufsize as integer, type as integer) as integer.
hKey

Identifies a currently open key or a predefined key. The hKey parameter can have one of the following predefined values:

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
name

Identifies the name of the variable that is to be set.

value

Identifies the string or Integer value that is to be set in the variable.

buffer

Buffer containing the value to be set in the variable.

bufsize

Size of the buffer in chars if the buffer is of type string or array of type char; otherwise, the number of bytes are specified.

type

The type of registry entry.

Type of registry entry is returned in Microsoft notation:

REG_NONE

Value 0

REG_SZ

Value 1

REG_EXPAND_SZ

Value 2

REG_BINARY

Value 3

REG_DWORD

Value 4

REG_DWORD_LITTLE_ENDIAN

Value 5

REG_DWORD_BIG_ENDIAN

Value 6

REG_LINK

Value 7

REG_MULTI_SZ

Value 8

On successful completion, the function returns TRUE; otherwise, it returns FALSE.

Example: RegSetVariable function

Before you start the following example, run the RegOpenKey example.

Dim value as string
Dim hkey1 as integer
Dim name, str, dummy as string
Dim i, i1, rtr, int as integer
Dim bBuf[100] as Byte
Dim cBuf[100] as char

ClrScr()

hkey1 = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\CA\4 test only")
if hkey1 = 0 then
	Print("RegOpenKey failed.")
	exit
endif

rtr = RegQueryVariable(hkey1, "var_2", str, int)
Print(Str(rtr) + ": var_2 = " + str)
int = 100
str = ""
rtr = RegQueryVariable(hkey1, "var_2", dummy, int, str)
Print(Str(rtr) + ": var_2(" + Str(int) + ") = " + str)
int = 100
str = ""
rtr = RegQueryVariable(hkey1, "var_2", dummy, int, cBuf)
for i1 = 0 to int - 1
	str = str + cBuf[i1]
next i1
Print(Str(rtr) + ": var_2(" + Str(int) + ") = " + str)
int = 100
str = "| "
rtr = RegQueryVariable(hkey1, "var_2", dummy, int, bBuf)
for i1 = 0 to int - 1
	str = str + Str(bBuf[i1]) + " | "
next i1
Print(Str(rtr) + ": var_2(" + Str(int) + ") = " + str)

RegCloseKey(hkey1)

SetMode64 - Accessing Windows 64-bit Registry and File System

DMScript functions can access the Windows registry and launch programs. When DMScript runs on a 64-bit Windows OS, the OS imposes certain rules as DMScript is a 32-bit application. Registry access for certain keys is automatically redirected to a parallel 32-bit registry area. For example, access to HKLM\SOFTWARE\xxx is redirected to HKLM\SOFTWARE\Wow6432Node\xxx. For a complete list of redirected keys, see: http://msdn.microsoft.com/en-us/library/aa384253(v=vs.85).aspx.

Similarly, accessing the file system using certain environment variables that reference system directories, are automatically redirected to directories that contain 32-bit binaries. For example, the %windir%\System32 directory is reserved for 64-bit applications. If DMScript attempts to access this directory, it is automatically redirected to %windir%\SysWOW64. For more information, see http://msdn.microsoft.com/en-us/library/aa384187(v=vs.85).aspx.

This behavior is important for Intellisig scripts because the scripts must detect 64-bit applications by accessing the correct 64-bit registry keys and file systems. For example, if an Intellisig attempts to read the registry key for the 64-bit version of 7zip in HKLM\SOFTWARE\7-zip, the script fails because it actually searches in HKLM\SOFTWARE\ Wow6432Node\7-zip, which does not exist. Similarly, an attempt to look for the 64-bit version of Notepad in %windir%\System32 finds the 32-bit version.

To handle this behavior, DMScript provides the following function that turns off the automatic redirection on 64-bit Windows OS:

setmode64(mode)
Mode

Specifies whether the redirection must be turned off or on. A value of 1 turns off redirection and a value of 0 turns it on.
Example: setmode64(1)

Note: When in the 64-bit mode, access the 32-bit registry using the physical name such as HKLM\SOFTWARE\Wow6432Node\xxx. However, Microsoft does not recommend this practice because the method of redirection can change in the future versions of Windows. Instead, the script must revert to the 32-bit mode.

Important! Disabling file system redirection for longer periods can prevent dmscript from loading system DLLs, causing it to fail.