[轉載]用perl操作註冊表的一些基本函數

來源:互聯網
上載者:User
用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();

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.