prel 操作注册表
perl 操作注册表函数
Open():
语法:
$object->Open($RegistryObj,$hKey);
$object A part of the registry.
$RegistryObj The key under the $object you want to explore.
$hKey Handle of the opened key.
$object:
$HKEY_LOCAL_MACHINE
$HKEY_USERS
$HKEY_CURRENT_USER
$HKEY_CLASSES_ROOT
$HKEY_CURRENT_CONFIG
$object A part of the registry.
$RegistryObj The key under the $object you want to explore.
$hKey Handle of the opened key.
$object:
$HKEY_LOCAL_MACHINE
$HKEY_USERS
$HKEY_CURRENT_USER
$HKEY_CLASSES_ROOT
$HKEY_CURRENT_CONFIG
示例:
GetKeys()
语法:
$hkey->GetKeys(\@Key_list);
$hkey Pointer to a key of the registry.
@Key_list Array with all the subkeys.
$hkey Pointer to a key of the registry.
@Key_list Array with all the subkeys.
示例:
GetValues
语法:
$hkey->GetValues(\%values);
$hkey Pointer to a key of the registry.
%values Hash (Name, Type, Value) for each value.
value:
0 REG_NONE
1 REG_SZ
2 REG_EXPAND_SZ
3 REG_BINARY
4 REG_DWORD
REG_DWORD_LITTLE_ENDIAN
5 REG_DWORD_BIG_ENDIAN
6 REG_LINK
7 REG_MULTI_SZ
8 REG_RESOURCE_LIST
9 REG_FULL_RESOURCE_DESCRIPTOR
10 REG_RESSOURCE_REQUIREMENT_MAP
$hkey Pointer to a key of the registry.
%values Hash (Name, Type, Value) for each value.
value:
0 REG_NONE
1 REG_SZ
2 REG_EXPAND_SZ
3 REG_BINARY
4 REG_DWORD
REG_DWORD_LITTLE_ENDIAN
5 REG_DWORD_BIG_ENDIAN
6 REG_LINK
7 REG_MULTI_SZ
8 REG_RESOURCE_LIST
9 REG_FULL_RESOURCE_DESCRIPTOR
10 REG_RESSOURCE_REQUIREMENT_MAP
示例:
use Win32::Registry;
my %RegType = (
0 => 'REG_0',
1 => 'REG_SZ',
2 => 'REG_EXPAND_SZ',
3 => 'REG_BINARY',
4 => 'REG_DWORD',
5 => 'REG_DWORD_BIG_ENDIAN',
6 => 'REG_LINK',
7 => 'REG_MULTI_SZ',
8 => 'REG_RESOURCE_LIST',
9 => 'REG_FULL_RESOURCE_DESCRIPTION',
10 => 'REG_RESSOURCE_REQUIREMENT_MAP');
my $Register = "Software\\MICROSOFT\\Java VM\\RNIModuleFlags"; #java
#ie信任站点
#$Register = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\bjxw.gov.cn\\www";
my $RegType, $RegValue, $RegKey, $value;
my %values;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
#ie信任站点
#$HKEY_CURRENT_USER->Open($Register,$hkey)|| die $!;
$hkey->GetValues(\%values);
foreach $value (keys(%values)){
$RegType = $values{$value}->[1]; #类型
$RegValue = $values{$value}->[2]; #数据
$RegKey = $values{$value}->[0]; #名称
next if ($RegType eq ''); #do not print default value if not assigned
$RegKey = 'Default' if ($RegKey eq ''); #name the default key
print "RegKey $RegKey ";
print "RegType $RegType ($RegType{$RegType}) : ";
SWITCH:
{
if ($RegType == 4)
{printf "Ox%1x \n", unpack("L",$RegValue); last SWITCH; }
if ($RegType == 5)
{printf "Ox%1x", unpack("N",$RegValue); last SWITCH; }
if ($RegType < 8 )
{printf "$RegValue\n"; last SWITCH; }
print "\n";
}
}
$hkey->Close();
my %RegType = (
0 => 'REG_0',
1 => 'REG_SZ',
2 => 'REG_EXPAND_SZ',
3 => 'REG_BINARY',
4 => 'REG_DWORD',
5 => 'REG_DWORD_BIG_ENDIAN',
6 => 'REG_LINK',
7 => 'REG_MULTI_SZ',
8 => 'REG_RESOURCE_LIST',
9 => 'REG_FULL_RESOURCE_DESCRIPTION',
10 => 'REG_RESSOURCE_REQUIREMENT_MAP');
my $Register = "Software\\MICROSOFT\\Java VM\\RNIModuleFlags"; #java
#ie信任站点
#$Register = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\bjxw.gov.cn\\www";
my $RegType, $RegValue, $RegKey, $value;
my %values;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
#ie信任站点
#$HKEY_CURRENT_USER->Open($Register,$hkey)|| die $!;
$hkey->GetValues(\%values);
foreach $value (keys(%values)){
$RegType = $values{$value}->[1]; #类型
$RegValue = $values{$value}->[2]; #数据
$RegKey = $values{$value}->[0]; #名称
next if ($RegType eq ''); #do not print default value if not assigned
$RegKey = 'Default' if ($RegKey eq ''); #name the default key
print "RegKey $RegKey ";
print "RegType $RegType ($RegType{$RegType}) : ";
SWITCH:
{
if ($RegType == 4)
{printf "Ox%1x \n", unpack("L",$RegValue); last SWITCH; }
if ($RegType == 5)
{printf "Ox%1x", unpack("N",$RegValue); last SWITCH; }
if ($RegType < 8 )
{printf "$RegValue\n"; last SWITCH; }
print "\n";
}
}
$hkey->Close();
Create()
语法:
$hkey->Create($key,$subkey);
$hkey Pointer to a key of the registry.
$key Name of a key to open or to create.
$subkey Receives the handle of the opened or created key.
$hkey Pointer to a key of the registry.
$key Name of a key to open or to create.
$subkey Receives the handle of the opened or created key.
示例:
use Win32::Registry;
my $Register = "SOFTWARE";
my $hkey,$SubKey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->Create("LanSing",$SubKey); #在HKEY_LOCAL_MACHINE\\SOFTWARE 下添加LanSing
$hkey->Close();
my $Register = "SOFTWARE";
my $hkey,$SubKey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->Create("LanSing",$SubKey); #在HKEY_LOCAL_MACHINE\\SOFTWARE 下添加LanSing
$hkey->Close();
DeleteKey()
语法:
$hkey->DeleteKey($subkey);
$hkey Pointer to a key of the registry.
$subkey Name of subkey to delete.
$hkey Pointer to a key of the registry.
$subkey Name of subkey to delete.
示例:
use Win32::Registry;
my $Register = "SOFTWARE";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->DeleteKey("LanSing"); 在HKEY_LOCAL_MACHINE\\SOFTWARE 下删除LanSing
$hkey->Close();
my $Register = "SOFTWARE";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->DeleteKey("LanSing"); 在HKEY_LOCAL_MACHINE\\SOFTWARE 下删除LanSing
$hkey->Close();
SetValue()
语法:
$hkey->SetValue($subkey,$type,$value);
$hkey A currently open key.
$subkey Name of subkey to modify.
$type This parameter must be of REG_SZ type.
$value Name of the value.
$hkey A currently open key.
$subkey Name of subkey to modify.
$type This parameter must be of REG_SZ type.
$value Name of the value.
示例:
use Win32::Registry;
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->SetValue("lansing",REG_SZ,"successful");
$hkey->Close();
注:其实是在"SOFTWARE\\LanSing"下建一个名为"lansing"的子键,使其默认的数值数据是"successful".
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->SetValue("lansing",REG_SZ,"successful");
$hkey->Close();
注:其实是在"SOFTWARE\\LanSing"下建一个名为"lansing"的子键,使其默认的数值数据是"successful".
SetValueEx()
语法:
示例:
DeleteValue()
语法:
示例:
use Win32::Registry;
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->DeleteValue("TheValue");
$hkey->Close();
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->DeleteValue("TheValue");
$hkey->Close();
QueryValue()
语法:
$hkey->QueryValue($SubKey,$Value);
$hkey A currently open key.
$SubKey Name of subkey to query.
$Value Value of the unnamed value
$hkey A currently open key.
$SubKey Name of subkey to query.
$Value Value of the unnamed value
示例:
use Win32::Registry;
my $Register = "Software\\LanSing";
my $hkey, $value;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->QueryValue($subkey,$value);
print "the unnamed value is : ";
if ($value eq '')
{
print "undefined\n";
}
else
{
print "$value\n";
}
$hkey->Close();
注:得到的是"Software\\LanSing"默认的键值。
my $Register = "Software\\LanSing";
my $hkey, $value;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->QueryValue($subkey,$value);
print "the unnamed value is : ";
if ($value eq '')
{
print "undefined\n";
}
else
{
print "$value\n";
}
$hkey->Close();
注:得到的是"Software\\LanSing"默认的键值。
QueryKey()
语法:
示例:
use Win32::Registry;
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->QueryKey($subkeys,$values);
print "$subkeys keys, $values values\n";
$hkey->Close();
注:ActivePerl-5.6.1.635-MSWin32-x86.msi版本的QueryKey函数用法是:
usage: $obj->QueryKey($classref, $number_of_subkeys, $number_of_values)
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->QueryKey($subkeys,$values);
print "$subkeys keys, $values values\n";
$hkey->Close();
注:ActivePerl-5.6.1.635-MSWin32-x86.msi版本的QueryKey函数用法是:
usage: $obj->QueryKey($classref, $number_of_subkeys, $number_of_values)
Save()
语法:
$hkey->Save($Filename);
$hkey A currently open key.
$Filename File name ! This file cannot already exist.
If this filename includes an extension, it cannot be used on FAT file systems
by the Load function. The file will have the System, Hidden and Read-Only
attributes setted (before you began searching around ...)!
$hkey A currently open key.
$Filename File name ! This file cannot already exist.
If this filename includes an extension, it cannot be used on FAT file systems
by the Load function. The file will have the System, Hidden and Read-Only
attributes setted (before you began searching around ...)!
示例:
use Win32::Registry;
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
if(IsWin95())
{
$hkey->Save("c:\\just_in_case")
}
else
{
$hkey->Save("c:\\just_in_case.reg");
}
$hkey->Close();
注:IsWin95()这个函数在ActivePerl-5.6.1.635-MSWin32-x86.msi版本中没定义?
my $Register = "SOFTWARE\\LanSing";
my $hkey;
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
if(IsWin95())
{
$hkey->Save("c:\\just_in_case")
}
else
{
$hkey->Save("c:\\just_in_case.reg");
}
$hkey->Close();
注:IsWin95()这个函数在ActivePerl-5.6.1.635-MSWin32-x86.msi版本中没定义?
Load()
语法:
$hkey->Load($SubKey,$Filename);
$hkey A currently open key.
$SubKey Name of the key to be created under hkey.
$Filename Filename
$hkey A currently open key.
$SubKey Name of the key to be created under hkey.
$Filename Filename
示例:
use Win32::Registry;
my $Register = "";
my $subkey = "MyHomeKey";
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->Load($subkey,"c:\\just_in_case.reg");
$hkey->Close();
my $Register = "";
my $subkey = "MyHomeKey";
$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->Load($subkey,"c:\\just_in_case.reg");
$hkey->Close();
链接:http://hi.baidu.com/cyleno/blog/item/12ba25fdceb35c1208244d5f.html
2019年2月16日 16:21
Thank you for sharing this helpful guide, I really need such information.