Home > Language Reference > System Functions > WriteReg

WriteReg

The WriteReg function creates or modifies a registry entry.

Usage

Use all registry functions with caution. If you are unfamiliar with the Windows Registry we recommend that you do not use these functions. Removing or modifying a registry entry can cause your system to become unstable.

Syntax

ret = WriteReg(<registry_key>, <type>, <value>)

Parameters

<registry_key>. string, registry key (folder) to read. .

<type>, integer, constant. The possible values are:

<type>  Type of the returned value
 1  string
 2  string expand
 3  binary
 4  integer or dword 
 7  string multiple

If <type> is 1 or 2, a string is returned; if it is 7, a string is also returned but the binary zeroes separating the multiple strings are changed to \n\ and the ending double binary 0 is not returned. If <type> is 3 (binary), a string is returned containing a hexadecimal conversion of the binary value. A <type> of 4 returns an integer.

<value>, variable only. Data type for this variable must be consistent with <type>.

Return value

ret, optional numeric return value. If the function has successfully writes the key, the return value is 0. If the function fails, the return value is a nonzero value (see System Functions Error Codes).

Remarks

If the <registry_key> or <value> do not exist, they are created. If the value already exists, it is replaced by the new <value>.

Under Windows Vista/Windows 7/Windows 8, if you don't have enough rights, writing in Registry is allowed only under HKEY_CURRENT_USER. If you execute a WriteReg with non admin rights, writing under HKEY_LOCAL_MACHINE, then Windows writes a virtualized key which is in fact under HKEY_CURRENT_USER\Software\Classes\VirtualStore

See also

ReadReg
DeleteRegKey
DeleteRegValue

Examples

WriteReg("HKEY_LOCAL_MACHINE\SOFTWARE\WinTask\WinTask\1.0\AppDir",1,name$)

'Script writing some entries in Registry
#IgnoreErrors=1
path$="HKEY_LOCAL_MACHINE\Personal\Id\"

firstname$="Robert"
lastname$="DUPONT"
age=40
idnumber=300000000
binary_key$="01000ff00c0"

res=WriteReg(path$+"FirstName",1,firstname$)
res=WriteReg(path$+"Lastname",1,lastname$)
res=WriteReg(path$+"Age",4,age)
res=WriteReg(path$+"Id",4,idnumber)
res= WriteReg(path$+"Key",3,binary_key$)

msgbox("Return code : "+str$(res),64,"Write to Registry")