Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

[OT] Using Module TieRegistry? for reading and pasting registrykey into another section

by mh88 (Novice)
on Feb 21, 2018 at 17:38 UTC ( [id://1209682]=perlquestion: print w/replies, xml ) Need Help??

mh88 has asked for the wisdom of the Perl Monks concerning the following question:

Hi valuable Monks,


the problem:
Read a given (Windows) Registry key and store the "value" into another registry Key.
(Office 12 Upgrade to Office 16 and migrate the Templates Path)

-> Any way to accomplish this is welcome, even if it is not Perl as long as your are not offended ;-). I tried it already with a .bat file too, but that was broken cause of the "hexcode"


what i want to do:
Read this:

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\General] "SharedTemplates"=hex(2):47,00,3a,00,5c,00,4f,00,46,00,46,00,49,00,43, +00,45,00,\ 32,00,30,00,30,00,37,00,5c,00,57,00,4f,00,52,00,44,00,37,00,5c,00,56 +,00,4f,\ 00,52,00,4c,00,41,00,47,00,45,00,4e,00,00,00

and directly "paste" it into the section listed below or save it into variable?

[HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\General] "SharedTemplates"=hex(2):47,00,3a,00,5c,00,4f,00,46,00,46,00,49,00,43, +00,45,00,\ 32,00,30,00,30,00,37,00,5c,00,57,00,4f,00,52,00,44,00,37,00,5c,00,56 +,00,4f,\ 00,52,00,4c,00,41,00,47,00,45,00,4e,00,5c,00,00,00

This translates to the Path G:\Office2007\... but this is different path for other users,
so i must read the key and store the same REG_EXPAND_SZ value into the ...\16.0 path.

I read the Modules examples but its not possible for me to accomplish this, and i dont understand this completely.
I only get some "subkeys" listed but i cant read or store it in the needed section.

I am at my other machine and saved the code on local drive,
so i cant provide an example of my (very bad) code right now.

Perhaps you did something similar already and maybe could provide me with your code which i may could adjust to my needs?

I tried it with StrawberryPortable which seems to include the Tie::Registry functionality.
I could only use Modules which are integrated into a portable Perl Version.


Thanks for your help

mh88
  • Comment on [OT] Using Module TieRegistry? for reading and pasting registrykey into another section
  • Select or Download Code

Replies are listed 'Best First'.
Re: [OT] Using Module TieRegistry? for reading and pasting registrykey into another section
by ikegami (Patriarch) on Feb 21, 2018 at 19:55 UTC

    I only get some "subkeys" listed but i cant read or store it in the needed section.

    There are two registries (more or less), one for 32-bit apps, and one for 64-bit apps. This explains a bit.

      btw, replace

      use Win32::TieRegistry( Delimiter => '#', ArrayValues => 0); my $pound = $Registry->Delimiter("/");
      with
      use Win32::TieRegistry Delimiter => '/', ArrayValues => 0;
      I tried the following, but honestly i dont understand much/or nearly nothing of it.
      This is an example which i tried to modify
      Just how to read a simple value like:
      "NoTrack"=dword:00000001
      under: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\General
      use Win32::TieRegistry( Delimiter=>"#", ArrayValues=>0 ); $pound= $Registry->Delimiter("/"); $targetKey= $Registry->{"HKEY_CURRENT_USER/Software/Microsoft/Office +/12.0/Common/"} or die "Can't read the key: $^E\n"; $data= $key->{"/General"} or die "Can't read the key: value: $^E\n"; foreach $entry ( keys(%$targetKey) ) { ... } foreach $subKey ( $targetKey->SubKeyNames ) { ... } $targetKey->AllowSave( 1 ); $targetKey->RegSaveKey( "C:/TEMP/testReg", [] );
      Error message:
      Cant read the key: value: handle is invalid

      How do i read a single value from a hash/array? or whatever this is stored when read into the Perl Code in?
      Any help welcome
      Ty
      mh88

        I don't see a .../12.0/Common/General in my registry: I see .../16.0/Common/General, and .../14.0/Common/General, but .../12.0/Common (which does exist in mine) doesn't have a General subkey.

        I think you're getting confused about subkeys vs. values. You have placed the 'general' subkey in a variable called $data. In the Win32::TieRegistry examples,$data holds a value, not a subkey.

        The error you are getting seems to indicate that HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\General does not exist. Since it doesn't exist in my registry, that doesn't surprise me.

        I have built up an SSCCE which uses the 16.0 version, and looks at a value in there (since I don't have the 12.0 'General' subkey)

        #!perl use warnings; use strict; use Data::Dumper; use Win32::TieRegistry( Delimiter => '#', ArrayValues => 0); my $pound = $Registry->Delimiter("/"); my $targetKey = $Registry->{"HKEY_CURRENT_USER/Software/Microsoft/Offi +ce/16.0/Common"} or die "Can't read the 'Common' key: $^E\n"; print STDERR "targetKey = '$targetKey'\n"; # this is the one you + called "data" before. It's not data, it's another key my $generalKey = $targetKey->{"General"} # thhis is another subkey or die "Can't read the 'General' subkey: $^E\n"; print STDERR "generalKey = '$generalKey'\n"; # this is the one y +ou called "data" before. It's not data, it's another key print STDERR Dumper $generalKey; # here, you can access actual values from the key, with or without a p +refixed slash my $data = $generalKey->{'/Authorized'} // '<undef>'; printf STDERR qq("%s" => >>%s<<\n), '/Authorized', $data; my $noslash = $generalKey->{'Authorized'} // '<undef>'; printf STDERR qq("%s" => >>%s<<\n), 'Authorized', $noslash; my $dne = $generalKey->{'DoesNotExist'} // '<undef>'; printf STDERR qq("%s" => >>%s<<\n), 'DoesNotExist', $dne; # update target key to 12.0: $targetKey = $Registry->{"HKEY_CURRENT_USER/Software/Microsoft/Office/ +12.0/Common"} or die "Can't read the '12.0/Common' key: $^E\n"; print STDERR "targetKey = '$targetKey'\n"; # this exists in mine $generalKey = $targetKey->{"General"} or die "Can't read the '12.0/Common/General' subkey: $^E\n"; print STDERR "generalKey = '$generalKey'\n"; # this doesn't exis +t in mine, so it won't get here __END__ generalKey = 'Win32::TieRegistry=HASH(0x26620c8)' $VAR1 = bless( { '/Xlstart' => 'XLSTART', ... '/Authorized' => '0x7FFFFFFF', ... }, 'Win32::TieRegistry' ); "/Authorized" => >>0x7FFFFFFF<< "Authorized" => >>0x7FFFFFFF<< "DoesNotExist" => >><undef><< targetKey = 'Win32::TieRegistry=HASH(0x51e80a8)' Can't read the '12.0/Common/General' subkey: The system cannot find th +e file specified

        I get a different error message than you got... but since you omitted code, who knows what else is different. Try my example, and see what you get. If it works the same, then you can try to tweak the code to make it do what you want.

      Hello
      Thanks for your reply. I think i am not affected by this since the changes are only under HKCU?
      and not HKLM?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1209682]
Approved by Marshall
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found