Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Raku: How do I generate a handle for HKEY_LOCAL_MACHINE?

by nysus (Parson)
on Sep 03, 2022 at 15:49 UTC ( #11146662=perlquestion: print w/replies, xml ) Need Help??

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

I'm looking to improve the Find::Which module so that it can find executables in the registry in HKEY LOCAL MACHINE\SOFTWARE Microsoft) Windows\CurrentVersion\App Paths.

I'm using Raku's NativeCall feature to attempt this but I'm having trouble using the Windows API to make function calls that require an HKEY handle to make queries. My latest failed attempt which just attempts to pass in a string for HKEY and then tries to use cglobal to get a value for it:

#! /usr/bin/env raku use v6; use NativeCall; sub RegQueryInfoKeyA( Str, # hKey #int32, # lpClass #int32, # lpcchClass #int32 is rw, # lpcSubKeys #`{ #int32 is rw, # lpReserved #int32 is rw, # lpcSubKeys } ) returns uint32 is native('Kernel32') { * } my $blah = RegQueryInfoKeyA( 'HKEY_LOCAL_MACHINE', #int32, #0, # # # ); say $blah; my $var := cglobal('Kernel32', 'HKEY_LOCAL_MACHINE', int32); say $var;

I tried taking look at some other Perl modules like Win32API::Registery but did not get anywhere. Any general hints would be greatly appreciated. Thanks.

$PM = "Perl Monk's";
$MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
$nysus = $PM . ' ' . $MC;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Raku: How do I generate a handle for HKEY_LOCAL_MACHINE?
by eyepopslikeamosquito (Bishop) on Sep 05, 2022 at 08:37 UTC

    Not answering your question, but the way Unix and Windows handle the running of external processes is sufficiently different that I usually prefer to have separate code paths (one for Unix, one for Windows) in Perl code that runs external commands.

    I'd be interested to learn the backstory behind your question.

Re: Raku: How do I generate a handle for HKEY_LOCAL_MACHINE?
by nysus (Parson) on Sep 03, 2022 at 18:38 UTC

    I was able to piece together a solution (I think), based on someone's code I found on SO:

    use NativeCall; constant BYTE := uint8; constant WCHAR := uint16; constant DWORD := int32; constant REGSAM := int32; constant WCHARS := CArray[WCHAR]; constant BYTES := CArray[BYTE]; constant HKEY_LOCAL_MACHINE = 0x80000002; constant KEY_QUERY_VALUE = 1; constant ERROR_SUCCESS = 0; # Yeah, I know. The Win-Api uses 0 for + success and other values to indicate errors sub RegOpenKeyExW( DWORD, WCHARS, DWORD, REGSAM, DWORD is rw) is nativ +e("Kernel32.dll") returns DWORD { * }; sub RegQueryValueExW( DWORD, WCHARS, DWORD is rw, DWORD is rw, BYTE is + rw, DWORD is rw) is native("Kernel32.dll") returns DWORD { * }; my $key = 'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths'; my DWORD $hkey; my $length = 1024; sub wstr( Str $str ) returns WCHARS { my $return = CArray[WCHAR].new( $str.encode.list ); $return[$return.elems] = 0; return $return; } my $h-key = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wstr($key), 0, KEY_QUERY +_VALUE, $hkey); say $hkey;

    The $hkey reports back a 3 digit number. However, it changes with each run, so I'm not sure if it's running correctly.

    $PM = "Perl Monk's";
    $MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
    $nysus = $PM . ' ' . $MC;
    Click here if you love Perl Monks

Re: Raku: How do I generate a handle for HKEY_LOCAL_MACHINE?
by Marshall (Canon) on Sep 05, 2022 at 04:31 UTC
    I didn't understand your Raku code and it didn't seem to have much to do with your problem statement. So, I looked back at your problem statement: find executables in the registry in HKEY LOCAL MACHINE\SOFTWARE Microsoft) Windows\CurrentVersion\App Paths.

    Its been something like 25 years since I had to do anything with MS Hives. I think I used Win32::Registry back then, but I think that is now an obsolete module. So, I looked at Win32::TieRegistry.

    This is a complicated subject that is very poorly documented. And the docs assumes that you understand a lot of arcane things about Microsoft Registry Hives. If you want to know paths, these will be REG_SZ (simple null terminated strings) or REG_EXPAND_SZ (strings with unexpanded references to environment variables).

    The code below comes close to listing the names of the .exe's at the key you specified. Note that there are some .dll's in this list. Perl code and printout showing the names of subkeys are shown together. Rest of printout is a "readmore" doc because it is pretty lengthy.

    A few notes: 1) you should check the returned value from a $Registry method, undef means a failure.
    2)This code must run with **admin privileges**.
    3) I'm not sure if you need complete paths, if so, then resolving the next level of hash ref. In addition, I came up with a whole slew of "yeah but's" and "what if's" because I am not really sure what you are trying to do. So at this point, I am stopping. At least I got a list of .exe's for you...

    use strict; use warnings; # SoPW id: $|=1; use Data::Dump qw(pp); my $Registry; use Win32::TieRegistry 0.20 ( TiedRef => \$Registry, Delimiter => "/", ArrayValues => 1, SplitMultis => 1, AllowLoad => 1, qw( REG_SZ REG_EXPAND_SZ REG_DWORD REG_BINARY REG_MULTI_SZ KEY_READ KEY_WRITE KEY_ALL_ACCESS ), ); my $apps= $Registry->{"/HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/ +CurrentVersion/App Paths/"} or die "Can't find the Windows Applications: $^E\n"; # Get list of subkey names: print "########################### Sub Key Names ###############\n\n"; my @subKeyNames = $apps->SubKeyNames; print "$_\n" for @subKeyNames; print "########################### Pretty Print Dump ###############\n +\n"; pp \$apps; print "########################### foreach Printing Loop ###########\n +\n"; foreach( keys %$apps ) { print "$_: ", $apps->{$_} // "undef", "\n"; } __END__ C:.......\PerlProjects\Monks>perl win32regManipulation.pl ########################### Sub Key Names ############### 7zFM.exe Acrobat.exe AcrobatInfo.exe Au3Info.exe Au3Info_x64.exe Aut2Exe.exe Aut2Exe_x64.exe AutoIt3.exe AutoIt3_x64.exe chrome.exe cmmgr32.exe devenv.exe dfshim.dll Escfg.exe escndv.exe excel.exe firefox.exe fsquirt.exe IEDIAG.EXE IEDIAGCMD.EXE IEXPLORE.EXE install.exe javaws.exe LGMobileDriver_WHQL_Ver_4.2.0.exe licensemanagershellext.exe Lync.exe MCUI32.exe mip.exe mplayer2.exe MSACCESS.EXE msedge.exe msoadfsb.exe msoasb.exe MsoHtmEd.exe msoxmled.exe MSPUB.EXE NAVW32.EXE OneNote.exe OUTLOOK.EXE pbrush.exe PowerDirector14 PowerDVD14 powerpnt.exe PowerShell.exe pwsh.exe SciTE.exe sdxhelper.exe setup.exe Skype.exe SKYPESERVER.EXE Snagit SnagIt32.exe SnippingTool.exe table30.exe TabTip.exe TextPad.Exe vlc.exe vstoee.dll wab.exe wabmig.exe waterfox.exe winget.exe Winword.exe Wireshark.exe wmplayer.exe WORDPAD.EXE WRITE.EXE

        On my windows machine, I had to open a command window with admin privileges to get the code to work. Just reporting that fact.
        I can run the GUI regedit from my account.
        I guess mileage varies.
Re: Raku: How do I generate a handle for HKEY_LOCAL_MACHINE? (reg query hklm )
by Anonymous Monk on Sep 04, 2022 at 07:22 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2022-09-29 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?