[Reprinted] some basic functions for operating the registry using Perl

Source: Internet
Author: User
Use Perl to operate some basic functions of the Registry

Copyright Disclaimer: During reprinting, please use hyperlinks to indicate the original source and author information of the article and this statement
Http://n0thing.blogbus.com/logs/237690.html

One open ():

Syntax:
$ Object-> open ($ registryobj, $ hkey );
$ Object a part of the Registry.
$ Registryobj the key under the $ object you want to specify E.
$ Hkey handle of the opened key.
$ Object:
$ HKEY_LOCAL_MACHINE
$ HKEY_USERS
$ HKEY_CURRENT_USER
$ Hkey_classes_root
$ Hkey_current_config

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

2. openex ():

Syntax:
$ 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

Example:
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 ();
Note: 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 );
}

Tri-Connect ()

Syntax:
$ Object-> connect ($ node, $ objref );
$ Object a part of the Registry.
$ Node UNC of a Windows computer, EX: \\\\ Luke.
$ Objref handle of the opened key.

Example:
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 ";
}
Note: 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 ()

Syntax:
$ Hkey-> getkeys (\ @ key_list );
$ Hkey pointer to a key of the Registry.
@ Key_list array with all the subkeys.

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

5. getvalues

Syntax:
$ 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

Example:
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 ()

Syntax:
$ Hkey-> Create ($ key, $ subkey );
$ Hkey pointer to a key of the Registry.
$ Key name of a key to open or to create.
$ Subkey keys es the handle of the opened or created key.

Example:
Use Win32: Registry;
My $ register = "software ";
My $ hkey, $ subkey;

$ HKEY_LOCAL_MACHINE-> open ($ register, $ hkey) | die $ !;
$ Hkey-> Create ("Lansing", $ subkey );
$ Hkey-> close ();

Ekey ()

Syntax:
$ Hkey-> deletekey ($ subkey );
$ Hkey pointer to a key of the Registry.
$ Subkey name of subkey to delete.
Example:
Use Win32: Registry;
My $ register = "software ";
My $ hkey;

$ HKEY_LOCAL_MACHINE-> open ($ register, $ hkey) | die $ !;
$ Hkey-> deletekey ("Lansing ");
$ Hkey-> close ();

Evalue ()

Syntax:
$ Hkey-> deletevalue ($ name );
$ Hkey a currently open key.
$ Name of value to delete.

Example:
Use Win32: Registry;
My $ register = "Software \ Lansing ";
My $ hkey;

$ HKEY_LOCAL_MACHINE-> open ($ register, $ hkey) | die $ !;
$ Hkey-> deletevalue ("thevalue ");
$ Hkey-> close ();

Nine setvalue ()

Syntax:
$ 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.
Example:
Use Win32: Registry;
My $ register = "Software \ Lansing ";
My $ hkey;

$ HKEY_LOCAL_MACHINE-> open ($ register, $ hkey) | die $ !;
$ Hkey-> setvalue ("Lansing", REG_SZ, "successful ");
$ Hkey-> close ();
Note: A sub-key named "Lansing" is created under "Software \ Lansing" so that the default value is "successful ".

Ten setvalueex ()

Syntax:
$ Hkey-> setvalueex ($ valuenam, $ reserved, $ type, $ data );
$ Hkey a currently open key.
$ Valuename name of the value to set.
$ Reserved must be null (UNDEF ).
$ Type of the value.
$ Data the value or data.
Example:
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 ();
Note: add an item under "Softeare \ Lansing" with the name "thename", type "REG_SZ", and key value "successful ",
Note and the area of the above function.

Eleven queryvalue ()

Syntax:
$ Hkey-> queryvalue ($ subkey, $ value );
$ Hkey a currently open key.
$ Subkey name of subkey to query.
$ Value of the unnamed Value

Example:
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 ();
Note: The default key value is "Software \ Lansing.

12 querykey ()

Syntax:
$ 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.

Example:
Use Win32: Registry;
My $ register = "Software \ Lansing ";
My $ hkey;

$ HKEY_LOCAL_MACHINE-> open ($ register, $ hkey) | die $ !;
$ Hkey-> querykey ($ subkeys, $ values );
Print "$ subkeys keys, $ values \ n ";
$ Hkey-> close ();
Note: The querykey function usage of the ActivePerl-5.6.1.635-MSWin32-x86.msi version is:
Usage: $ obj-> querykey ($ classref, $ number_of_subkeys, $ number_of_values)

Thirteen save ()

Syntax:
$ Hkey-> Save ($ filename );
$ Hkey a currently open key.
$ Filename file name! This file cannot already exist.
If this filename already des 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 ...)!

Example:
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 ();
Note: Is this function iswin95 () not defined in the ActivePerl-5.6.1.635-MSWin32-x86.msi version?

Fourteen load ()

Syntax:
$ Hkey-> load ($ subkey, $ filename );
$ Hkey a currently open key.
$ Subkey name of the key to be created under hkey.
$ Filename

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.