Acessando o registro do Windows


Podemos acessar o registro do Windows com o PHP utilizando a extensão win32std ou então utilizando o WScript.Shell

win32std:


$keyConst = HKEY_LOCAL_MACHINE;

// backslash is used as an escape so it must be escaped itself
$key = "Software\Teste";

// abre o registro
$reg = @reg_open_key($keyConst, $key);
if (!$reg) {
	throw new Exception("Cannot access registry.");
}

// retorna um array de subchaves
$subkeys = reg_enum_key($reg);
foreach ($subkeys as $index => $subkey)
	echo "The subkey at " . $index . " is " . $subkey . "n";

// retorna uma subchave especifica
$index = 2;
$subkey = reg_enum_key($reg, $index);
echo "The subkey at " . $index . " is " . $subkey . "n";

// retorna um array de valores
$values = reg_enum_value($reg);
foreach ($values as $index => $value) {
	echo "The value at " . $index . " is " . $value . " and stores ";
	echo reg_get_value($reg, $value) . "n";
}

// retorna um valor especifico
$index = 1;
$value = reg_enum_value($reg, $index);
echo "The value at " . $index . " is " . $value . " and stores ";
echo reg_get_value($reg, $value) . "n";

// escrevendo no registro
$values = array(
	array("dbDriver", REG_SZ,    "mssql"),
	array("dbHost",   REG_SZ,    "localhost"),
	array("dbPort",   REG_DWORD, 1433),
	array("dbSchema", REG_SZ,    "MyAppDB"),
	array("dbUser",   REG_SZ,    "DBUser"),
	array("dbPass",   REG_SZ,    "s3cr3t")
);

foreach ($values as $value)
	reg_set_value($reg, $value[0], $value[1], $value[2]);

// fecha o registro
reg_close_key($reg);
WScript.Shell:


	// lendo o registro
	$Wshshell= new COM('WScript.Shell');
	$data= $Wshshell->regRead('HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip\path');
	echo "Data is ::".$data;
	
	// escrevendo no registro
	$WshShell = new COM("WScript.Shell");
	$registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
	try{
		$result = $WshShell->RegWrite($registry, $value, $type);
		echo "Entry is Successfully written at:".$registry; 
	}
	catch(Exception $e){
		echo "Some Exception in Registry writing".$e;
	}
	
	// deletando o registro
	$WshShell = new COM("Wscript.shell");
    $registry = "HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder . "\\" . $key;
    try{
		$result = $WshShell->RegDelete($registry);
		echo $key." is Successfully deleted from HKEY_LOCAL_MACHINE\\SOFTWARE\\" . $folder ; 
    }
    catch(Exception $e){
        echo "Some Exception with the code::".$e;
    }
Constantes:

HKEY_CURRENT_CONFIG (HKCC)
HKEY_LOCAL_MACHINE (HKLM)
HKEY_USERS (HKU)
HKEY_CLASSES_ROOT (HKCR)
HKEY_CURRENT_USER (HKCU)

Tipos de dados:

REG_DWORD – inteiro
REG_SZ – string de tamanho fixo
REG_EXPAND_SZ – string de tamanho variável
REG_MULTI_SZ – lista de itens separados por espaço ou vírgula
REG_BINARY – binário
REG_NONE – nenhum data type associado




Comentários

Copyright © 2018 Anderson Bravo
www.andersonbravo.com.br
anderson@andersonbravo.com.br