设为首页 收藏本站
查看: 586|回复: 0

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

[复制链接]

尚未签到

发表于 2015-12-27 09:18:03 | 显示全部楼层 |阅读模式
用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();

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156818-1-1.html 上篇帖子: Perl/C#连接Oracle/SQL Server和简单操作 下篇帖子: Perl 学习手札之四:variables and value
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表