prel 操作注册表

just posted @ 2009年1月08日 20:54 in 脚本 with tags Perl 注册表 , 1615 阅读

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
 


示例:

use Win32::Registry;
my $Register = "SOFTWARE\\Microsoft";
my ($hkey,@key_list,$key);

$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->GetKeys(\@key_list);
print "$Register keys\n";
foreach $key (@key_list)
{
print "$key\n";
}
$hkey->Close();

 GetKeys()

语法:

$hkey->GetKeys(\@Key_list);
$hkey Pointer to a key of the registry.
@Key_list Array with all the subkeys.
 


示例:

use Win32::Registry;
my $Register = "SOFRWARE\\Microsoft";
my ($hkey,@key_list,$key);

$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
$hkey->GetKeys(\@key_list);
print "$Register keys\n";
foreach $key (@key_list)
{
print "$key\n";
}
$hkey->Close();
 


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
 


示例:

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();

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.
 


示例:

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();
 

DeleteKey()

语法:

$hkey->DeleteKey($subkey);
$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();

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.
 

示例:

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".
 


SetValueEx()

语法:

$hkey->SetValueEx($ValueNam,$Reserved,$Type,$Data);
$hkey A currently open key.
$ValueName Name of the value to set.
$Reserved Must be NULL (undef).
$Type Type of the value.
$Data The value or data.
 

示例:

use Win32::Registry;
my $Register = "SOFTEARE\\LanSing";
my $hkey;

$HKEY_LOCAL_MACHINE->Open($Register,$hkey)|| die $!;
undef $garbage;
$hkey->SetValueEx("TheName",$garbage,REG_SZ,"successful");
$hkey>Close();
注:在"SOFTEARE\\LanSing"这个键下增加一项,名称是"TheName",类型是"REG_SZ",键值是"successful"
    注意和上面函数的区。
 

DeleteValue()

语法:

$hkey->DeleteValue($Name);
$hkey A currently open key.
$Name Name of value to delete.
 


示例:

use Win32::Registry;
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 

示例:

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"默认的键值。


QueryKey()

语法:

$hkey->QueryKey($SubKey,$Value);
$hkey A currently open key.
$SubKey Number of subkeys contained by the specified key. Can be NULL.
$Value Number of values associated with the key. Can be NULL.
 


示例:

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)
 

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 ...)!

 

示例:

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版本中没定义?
 


Load()

语法:

$hkey->Load($SubKey,$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();
 

链接:http://hi.baidu.com/cyleno/blog/item/12ba25fdceb35c1208244d5f.html

  • 无匹配
happy wheels 说:
2019年2月16日 16:21

Thank you for sharing this helpful guide, I really need such information.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter