现在的位置: 首页 > 综合 > 正文

[转载]用perl操作注册表的一些基本函数

2013年05月22日 ⁄ 综合 ⁄ 共 7499字 ⁄ 字号 评论关闭

用perl操作注册表的一些基本函数

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://n0thing.blogbus.com/logs/237690.html

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

二 OpenEx():

语法:
$object->OpenEx($SubKey,$Filename,$hKey);
$object A part of the registry.
$Sam Security Access Mask.
$hKey Handle of the opened key.
$Sam:
KEY_ALL_ACCESS:Full access (read, write, delete)
KEY_READ:Read-only access
KEY_WRITE:Write-only access

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

$HKEY_LOCAL_MACHINE->OpenEx($Register,KEY_ALL_ACCESS,$hkey)|| die $!;
$hkey->GetKeys(\@key_list);
print "$Register keys\n";
foreach $key (@key_list)
{
print "$key\n";
}
$hkey->Close();
注:Code to add to /perl5/lib/Win32/Registry.pm

#Cut & Paste this sub in /perl5/lib/win32/registry.pm
#--------------------%<---------%<----------------------------
sub OpenEx
{
my $self = shift;

if( $#_ != 2 ){
die 'usage: OpenEx( $SubKey, $Sam, $ObjRef )';
}

local ($SubKey,$Sam) = @_;
local ($Result,$SubHandle,$Garbage);
undef $Garbage;

$Result = Win32::RegOpenKeyEx($self->{'handle'},$SubKey,$Garbage,$Sam,$SubHandle);
$_[2] = _new( $SubHandle );

if (!$_[2] ){
return 0;
}

($! = Win32::GetLastError()) if(!$Result);

# return a boolean value
return($Result);
}

三 connect()

语法:
$object->Connect( $Node,$ObjRef );
$object A part of the registry.
$Node UNC of a Windows computer, ex : \\\\LUKE.
$ObjRef Handle of the opened key.

示例:
use Win32::Registry;
my ($node) = '\\\\MyComputer';
my ($hNode,$Key,%values);

$HKEY_LOCAL_MACHINE->Connect($node,$hNode)|| die"Cannot connect to $node";
$hNode->Open("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",$hKey)|| die "cannot open regitsry";
$hKey->GetValues(\%values);
$hKey->Close();
$hNode->Close();
foreach (keys(%values))
{
print "Value $_,data = $values{$_}[2]\n";
}
注:Code to add to /perl5/lib/Win32/Registry.pm

#Hack send by Frederick, Michael
#Cut & Paste this sub in /perl5/lib/win32/registry.pm
#--------------------%<---------%<----------------------------
sub Connect
{
my $self = shift;

if( $#_ != 1 )
{
die 'usage: Connect( $Node, $ObjRef )';
}

local ($Node) = @_;
local ($Result,$SubHandle);

$Result = RegConnectRegistry ($Node, $self->{'handle'}, $SubHandle);
$_[1] = _new( $SubHandle );

return 0 if (!$_[1] );

($! = Win32::GetLastError()) if(!$Result);

# return a boolean value
return($Result);
}

四 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";
my $RegType, $RegValue, $RegKey, $value;
my %values;

$HKEY_LOCAL_MACHINE->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";
print " ($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->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->Close();

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

九 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",
    注意和上面函数的区。

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

抱歉!评论已关闭.