PHP Classes

PHP Windows Registry Access: Manage the values of keys in Windows registry

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 210 This week: 1All time: 8,367 This week: 560Up
Version License PHP version Categories
windows-registry 1.0BSD License5.5PHP 5, Windows, Configuration
Description 

Author

This class can manage the values of keys in Windows registry.

It can create, read, update and delete key values in Windows registry using the WMI StdRegProv provider.

The class supports values of types DWORD, QWORD, SZ, EXPAND_SZ, BINARY and MULTI_SZ, VARIANT and enumerate keys and values.

VARIANT objects returned by the WMI API are correctly handled and converted to built-in PHP structures.

Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 58
All time rank: 13810 in France France
Week rank: 10 Up1 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x

Example

<?php
/***
 * This sample script :
 * 1) Enumerates all the keys under HKCU\Software and prints them
 * 2) Creates the HKCU\TestRegistry\Values key
 * 3) Creates/updates the following keys (using WMI API calls) :
 * a) A binary value :
 * 1. using a string
 * 2. using a byte array
 * 3. using a hex string
 * b) A dword value
 * c) An expanded string value
 * d) A multi-string value :
 * 1. With only one string
 * 2. With multiple strings
 * e) A qword value
 * f) A string value
 * 4) Creates/updates the following keys (using WShell Registry calls) :
 * a) A string value
 * b) An expanded string value
 * c) A DWORD value
 * 5) Enumerates all the values in the HKCU\TestRegistry\Values key
 * 6) Enumerates all values, using RegistryValue objects
 * 7) Retrieves created key values
 * 8) Deletes a key
 * 9) Tries to delete the created subkey (HKCU\TestRegistry)
 *
 * The check_error() function defined in this script is called after each registry method invocation
 * and prints an error message if the function call failed.
 *
 ***/

include ( 'Registry.class.php' ) ;

if (
php_sapi_name ( ) == 'cli' )
    {
   
$eol = PHP_EOL ;
   
$tab = "\t" ;
     }
else
    {
   
$eol = "<br/>" ;
   
$tab = str_repeat ( "&nbsp;", 8 ) ;
     }


// check_error -
// Checks that the last called registry method succeded.
function check_error ( )
   {
    global
$registry, $tab, $eol ;

   
$status = $registry -> GetLastError ( ) ;

    if (
$status )
       {
        echo (
"Last registry call failed, status = 0x" . sprintf ( "%08X", $status ) . $eol ) ;
        exit ;
        }
    }

// 0) Create the registry object
$registry = new Registry ( ) ;

// 1) Enumerate all the keys under HKCU\Software and print them
$keys = $registry -> EnumKeys ( Registry::HKCU, 'Software' ) ;
check_error ( ) ;

echo (
"Registry keys for HKCU\\Software :\n" ) ;

foreach (
$keys as $key )
    echo (
"$tab$key$eol" ) ;

// 2) Create the HKCU\TestRegistry\Values key
$test_key = 'TestRegistry/Values' ; // Slashes are replaced by backslashes
$registry -> CreateKey ( Registry::HKCU, $test_key ) ;
check_error ( ) ;

// 3.a.1) Create the HKCU\TestRegistry\Values\BinaryValueFromString key
$registry -> SetBinaryValue ( Registry::HKCU, $test_key, 'BinaryValueFromString', 'ABCDEF' ) ;
check_error ( ) ;

// 3.a.2) Create the HKCU\TestRegistry\Values\BinaryValueFromrArray key (set to "ABC")
$registry -> SetBinaryValue ( Registry::HKCU, $test_key, 'BinaryValueFromArray', [ 65, 66, 67 ] ) ;
check_error ( ) ;

// 3.a.3) Create the HKCU\TestRegistry\Values\BinaryValueFromrHexString key (set to "0123456789ABCDEF")
$registry -> SetBinaryValueFromHexString ( Registry::HKCU, $test_key, 'BinaryValueFromHexString', "0123456789ABCDEF" ) ;
check_error ( ) ;

// 3.b) Create the HKCU\TestRegistry\Values\DWORDValue key (set to 0x01020304)
$registry -> SetDWORDValue ( Registry::HKCU, $test_key, 'DWORDValue', 0x01020304 ) ;
check_error ( ) ;

// 3.c) Create the HKCU\TestRegistry\Values\ExpandedStringValue key, referencing the %WINDIR% environment variable
$registry -> SetExpandedStringValue ( Registry::HKCU, $test_key, 'ExpandedStringValue', '%WINDIR%\Something' ) ;
check_error ( ) ;

// 3.d.1) Create the HKCU\TestRegistry\Values\MultiStringValueSingle key
$registry -> SetMultiStringValue ( Registry::HKCU, $test_key, 'MultiStringValueSingle', 'A sample string' ) ;
check_error ( ) ;

// 3.d.2) Create the HKCU\TestRegistry\Values\MultiStringValueMultiple key
$registry -> SetMultiStringValue ( Registry::HKCU, $test_key, 'MultiStringValueMultiple',
        [
'Sample 1', 'Sample 2', 'Sample 3' ] ) ;
check_error ( ) ;

// 3.e) Create the HKCU\TestRegistry\Values\QWORDValue key (one greater than PHP_INT_MAX, and the other equal to 1)
$registry -> SetQWORDValue ( Registry::HKCU, $test_key, 'QWORDBigValue', PHP_INT_MAX * 10 ) ;
check_error ( ) ;

$registry -> SetQWORDValue ( Registry::HKCU, $test_key, 'QWORDValue', 1 ) ;
check_error ( ) ;

// 3.f) Create the HKCU\TestRegistry\Values\StringValue key
$registry -> SetStringValue ( Registry::HKCU, $test_key, 'StringValue', 'this is a sample string value' ) ;
check_error ( ) ;

// 4.a) Create the HKCU\TestRegistry\Values\WShellStringValue key
$registry -> SetValue ( Registry::HKCU, $test_key, 'WShellStringValue', 'this is a sample WSHELL string value', Registry::REG_SZ ) ;
check_error ( ) ;

// 4.b) Create the HKCU\TestRegistry\Values\WShellExpandedStringValue key
$registry -> SetValue ( Registry::HKCU, $test_key, 'WShellExpandedStringValue', 'this is a sample WSHELL expanded string value, WINDIR = %WINDIR%',
               
Registry::REG_EXPAND_SZ ) ;
check_error ( ) ;

// 4.c) Create the HKCU\TestRegistry\Values\WShellDWORDValue key
$registry -> SetValue ( Registry::HKCU, $test_key, 'WShellDWORDValue', 0x01020304,
               
Registry::REG_DWORD ) ;
check_error ( ) ;

// 5) Enumerate all the values in the HKCU\TestRegistry\Values key with their type
$keys = $registry -> EnumValues ( Registry::HKCU, $test_key, false ) ;
check_error ( ) ;

echo (
"Created values :$eol" ) ;

foreach (
$keys as $key => $type )
    echo (
"$tab$key ($type)$eol" ) ;

// 6) Enumerate all the values in the HKCU\TestRegistry\Values key, using registry value objects
$keys = $registry -> EnumValuesEx ( Registry::HKCU, $test_key ) ;
check_error ( ) ;

echo (
"Created RegistryValue objects :$eol" ) ;

foreach (
$keys as $value )
    echo (
"$tab" . str_replace ( "$eol", "$eol$tab", print_r ( $value, true ) ) ) ;

// 7) Retrieve created key values - You must known the type of each key before doing that, or else use the GetValue() method,
// which works only on REG_SZ, REG_EXPAND_SZ, REG_DWORD and REG_BINARY types.
echo ( "Created values :$eol" ) ;

echo (
"$tab$test_key/BinaryValueFromArray : " . $registry -> GetBinaryValue ( Registry::HKCU, $test_key, 'BinaryValueFromArray' ) . $eol ) ;
echo (
"$tab$test_key/BinaryValueFromHexString : " . $registry -> GetBinaryValue ( Registry::HKCU, $test_key, 'BinaryValueFromHexString' ) . $eol ) ;
echo (
"$tab$test_key/BinaryValueFromString : " . $registry -> GetBinaryValue ( Registry::HKCU, $test_key, 'BinaryValueFromString' ) . $eol ) ;
echo (
"$tab$test_key/DWORDValue : 0x" . sprintf ( "%08X", $registry -> GetDWORDValue ( Registry::HKCU, $test_key, 'DWORDValue' ) ) . $eol ) ;
echo (
"$tab$test_key/ExpandedStringValue : " . $registry -> GetExpandedStringValue ( Registry::HKCU, $test_key, 'ExpandedStringValue' ) . $eol ) ;
echo (
"$tab$test_key/BinaryValueFromHexString : " . $registry -> GetBinaryValue ( Registry::HKCU, $test_key, 'BinaryValueFromHexString' ) . $eol ) ;
echo (
"$tab$test_key/MultiStringValueMultiple : " . implode ( ', ', $registry -> GetMultiStringValue ( Registry::HKCU, $test_key, 'MultiStringValueMultiple' ) ) . $eol ) ;
echo (
"$tab$test_key/MultiStringValueSingle : " . implode ( ', ', $registry -> GetMultiStringValue ( Registry::HKCU, $test_key, 'MultiStringValueSingle' ) ) . $eol ) ;
echo (
"$tab$test_key/MultiStringValueMultiple : " . implode ( ', ', $registry -> GetMultiStringValue ( Registry::HKCU, $test_key, 'MultiStringValueMultiple' ) ) . $eol ) ;
echo (
"$tab$test_key/QWORDBigValue : " . $registry -> GetQWORDValue ( Registry::HKCU, $test_key, 'QWORDBigValue' ) . $eol ) ;
echo (
"$tab$test_key/QWORDValue : " . $registry -> GetQWORDValue ( Registry::HKCU, $test_key, 'QWORDValue' ) . $eol ) ;
echo (
"$tab$test_key/StringValue : " . $registry -> GetQWORDValue ( Registry::HKCU, $test_key, 'StringValue' ) . $eol ) ;
echo (
"$tab$test_key/WShellDWORDValue : 0x" . sprintf ( "%08X", $registry -> GetValue ( Registry::HKCU, $test_key, 'WShellDWORDValue' ) ) . $eol ) ;

// Note that the GetValue() method for WShell does not seem to process variable expansion
echo ( "$tab$test_key/WShellExpandedStringValue: " . $registry -> GetValue ( Registry::HKCU, $test_key, 'WShellExpandedStringValue' ) . $eol ) ;
echo (
"$tab$test_key/WShellStringValue : " . $registry -> GetValue ( Registry::HKCU, $test_key, 'WShellStringValue' ) . $eol ) ;

// 8) Delete a subkey (HKCU\TestRegistry\Values\WShellExpandedStringValue key)
$registry -> DeleteValue ( Registry::HKCU, $test_key, 'WShellExpandedStringValue' ) ;
check_error ( ) ;

// 9) Delete the test subkey - leads to an error, since a key can only be deleted if it has no subkeys - so you'll have to delete TestRegistry manually under HKCU
$registry -> DeleteKey ( Registry::HKCU, 'TestRegistry' ) ;
check_error ( ) ;


Details

?This class package is an extraction from a wider personal project called Thrak, a sort of PHP framework for Web development and command-line scripting (https://github.com/christian-vigh/php-thrak). I have made my best to extract this class and cut off all of its dependencies from other Thrak classes without removing functionalities, so that it becomes a standalone package that you can freely integrate into your projects. There is no namespace, so that you are free to add your own one if you wish, and my own error handling mechanism has been replaced with standard exceptions. All other dependencies have been replaced with core PHP functions or, when needed, by methods I developed elsewhere and imported into this package. Should you have any question or find a bug, please feel free to contact me at : christian.vigh@orange.fr

  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file NOTICE Doc. Package notes
Accessible without login HTML file README.html Doc. Package documentation
Plain text file Registry.class.php Class Windows Registry Access Class

 Version Control Unique User Downloads Download Rankings  
 0%
Total:210
This week:1
All time:8,367
This week:560Up