Core Function RegRead
From Sputnik Wiki
(Difference between revisions)
m (1 revision) |
|||
(4 intermediate revisions by one user not shown) | |||
Line 28: | Line 28: | ||
Failure: Returns the default data. | Failure: Returns the default data. | ||
+ | |||
+ | Major Failure: Returns null. | ||
=== Remarks === | === Remarks === | ||
Line 38: | Line 40: | ||
my $var = RegRead(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", @"ProgramFilesDir"); | my $var = RegRead(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", @"ProgramFilesDir"); | ||
MsgBox("Program files are in: $var"); | MsgBox("Program files are in: $var"); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Long names | ||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | RegWrite(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestSTRING", "REG_SZ", "Hello"); | ||
+ | RegWrite(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestDWORD", "REG_DWORD", 777); | ||
+ | RegWrite(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestQWORD", "REG_QWORD", 111); | ||
+ | say RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestSTRING"); // Prints: Hello | ||
+ | say RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestDWORD"); // Prints: 777 | ||
+ | say RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestQWORD"); // Prints: 111 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Short names | ||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | RegWrite(@"HKCU\SOFTWARE\Test", @"TestSTRING", "REG_SZ", "Hello"); | ||
+ | RegWrite(@"HKCU\SOFTWARE\Test", @"TestDWORD", "REG_DWORD", 777); | ||
+ | RegWrite(@"HKCU\SOFTWARE\Test", @"TestQWORD", "REG_QWORD", 111); | ||
+ | say RegRead(@"HKCU\SOFTWARE\Test", @"TestSTRING"); // Prints: Hello | ||
+ | say RegRead(@"HKCU\SOFTWARE\Test", @"TestDWORD"); // Prints: 777 | ||
+ | say RegRead(@"HKCU\SOFTWARE\Test", @"TestQWORD"); // Prints: 111 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | No type set so it will try guess the type | ||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | RegWrite(@"HKCU\SOFTWARE\Test", @"Test", "", "Hello"); | ||
+ | say RegRead(@"HKCU\SOFTWARE\Test", @"Test"); // Prints: Hello | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Reading and writing binary | ||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Create binary from a string | ||
+ | $binary = Pack("A*", "Hello World!"); | ||
+ | // Write it to reg | ||
+ | RegWrite(@"HKCU\SOFTWARE\Test", @"TestBINARY", "REG_BINARY", $binary); | ||
+ | // Read it from reg | ||
+ | my $var = RegRead(@"HKCU\SOFTWARE\Test", @"TestBINARY"); | ||
+ | // Print information | ||
+ | println("The binary size is: " . BinaryLen($var) ); | ||
+ | Foreach ($var as $i) | ||
+ | { | ||
+ | println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); | ||
+ | } | ||
+ | $String = Unpack("A*", $var, 3); | ||
+ | println("Full string: " . $String); // Back to string | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Reading and writing arrays | ||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Make array | ||
+ | $myarray = array("One", "Two", "Three", "Four", 777, 100.42); | ||
+ | // Write array to reg | ||
+ | RegWrite(@"HKCU\SOFTWARE\Test", @"TestARRAY", "REG_MULTI_SZ", $myarray); | ||
+ | |||
+ | // Read array from reg | ||
+ | my $var = RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestARRAY"); | ||
+ | // Print the array | ||
+ | printr($var); | ||
+ | /* | ||
+ | Prints: | ||
+ | ARRAY | ||
+ | { | ||
+ | [0] => One | ||
+ | [1] => Two | ||
+ | [2] => Three | ||
+ | [3] => Four | ||
+ | [4] => 777 | ||
+ | [5] => 100.42 | ||
+ | } | ||
+ | */ | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Reading and writing arrays as a string | ||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | / /When writing a REG_MULTI_SZ key you must | ||
+ | // separate each value with @LF ("\n" can be added to string to this effect). | ||
+ | // The value must NOT end with @LF (a newline) | ||
+ | // unless you want an empty substring | ||
+ | |||
+ | // Make array | ||
+ | $mystring = "One\nTwo\nThree\nFour\n777\n100.42"; | ||
+ | // Write array to reg | ||
+ | RegWrite(@"HKCU\SOFTWARE\Test", @"TestARRAY", "REG_MULTI_SZ", $mystring); | ||
+ | |||
+ | // Read array from reg | ||
+ | my $var = RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestARRAY"); | ||
+ | // Print the array | ||
+ | printr($var); | ||
+ | /* | ||
+ | Prints: | ||
+ | ARRAY | ||
+ | { | ||
+ | [0] => One | ||
+ | [1] => Two | ||
+ | [2] => Three | ||
+ | [3] => Four | ||
+ | [4] => 777 | ||
+ | [5] => 100.42 | ||
+ | } | ||
+ | */ | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:37, 14 June 2015
RegRead( <key>, <value>, <default> )
Contents |
Description
Reads a value from the registry
Parameters
key
The registry key to read.
value
The value to read.
default
Optional; The value to return if the read failed.
(Default: "" empty string)
Return Value
Success: Returns the correct data.
Failure: Returns the default data.
Major Failure: Returns null.
Remarks
None.
Example
my $var = RegRead(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", @"ProgramFilesDir"); MsgBox("Program files are in: $var");
Long names
RegWrite(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestSTRING", "REG_SZ", "Hello"); RegWrite(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestDWORD", "REG_DWORD", 777); RegWrite(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestQWORD", "REG_QWORD", 111); say RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestSTRING"); // Prints: Hello say RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestDWORD"); // Prints: 777 say RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestQWORD"); // Prints: 111
Short names
RegWrite(@"HKCU\SOFTWARE\Test", @"TestSTRING", "REG_SZ", "Hello"); RegWrite(@"HKCU\SOFTWARE\Test", @"TestDWORD", "REG_DWORD", 777); RegWrite(@"HKCU\SOFTWARE\Test", @"TestQWORD", "REG_QWORD", 111); say RegRead(@"HKCU\SOFTWARE\Test", @"TestSTRING"); // Prints: Hello say RegRead(@"HKCU\SOFTWARE\Test", @"TestDWORD"); // Prints: 777 say RegRead(@"HKCU\SOFTWARE\Test", @"TestQWORD"); // Prints: 111
No type set so it will try guess the type
RegWrite(@"HKCU\SOFTWARE\Test", @"Test", "", "Hello"); say RegRead(@"HKCU\SOFTWARE\Test", @"Test"); // Prints: Hello
Reading and writing binary
// Create binary from a string $binary = Pack("A*", "Hello World!"); // Write it to reg RegWrite(@"HKCU\SOFTWARE\Test", @"TestBINARY", "REG_BINARY", $binary); // Read it from reg my $var = RegRead(@"HKCU\SOFTWARE\Test", @"TestBINARY"); // Print information println("The binary size is: " . BinaryLen($var) ); Foreach ($var as $i) { println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); } $String = Unpack("A*", $var, 3); println("Full string: " . $String); // Back to string
Reading and writing arrays
// Make array $myarray = array("One", "Two", "Three", "Four", 777, 100.42); // Write array to reg RegWrite(@"HKCU\SOFTWARE\Test", @"TestARRAY", "REG_MULTI_SZ", $myarray); // Read array from reg my $var = RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestARRAY"); // Print the array printr($var); /* Prints: ARRAY { [0] => One [1] => Two [2] => Three [3] => Four [4] => 777 [5] => 100.42 } */
Reading and writing arrays as a string
/ /When writing a REG_MULTI_SZ key you must // separate each value with @LF ("\n" can be added to string to this effect). // The value must NOT end with @LF (a newline) // unless you want an empty substring // Make array $mystring = "One\nTwo\nThree\nFour\n777\n100.42"; // Write array to reg RegWrite(@"HKCU\SOFTWARE\Test", @"TestARRAY", "REG_MULTI_SZ", $mystring); // Read array from reg my $var = RegRead(@"HKEY_CURRENT_USER\SOFTWARE\Test", @"TestARRAY"); // Print the array printr($var); /* Prints: ARRAY { [0] => One [1] => Two [2] => Three [3] => Four [4] => 777 [5] => 100.42 } */