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

Using Win32::OLE...
use Win32::OLE ('in'); ... @host = ($server,"Root/default",$user,$password); $localWMI = Win32::OLE->new('WbemScripting.SWbemLocator'); $WMI = $localWMI->ConnectServer(@host); $registry = $WMI->Get("StdRegProv") or die "Failed to get registry."; $strPath='SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName'; $strValue = "ComputerName"; print $strPath; $return=$registry->GetStringValue( 2147483650, $strPath, $strValue, $result ); print "\n$strValue:($return,$result)\n"; my $err = Win32::OLE->LastError(); if ($err == 0) {print "Successful!\n";} else {print $err;}
You think it would be in $result, don't you?
I always get...
ComputerName:(0,)
Successful!

So there is no OLE error... and according to other scripts I've seen, everybody expects the query to be put in that variable... If I change the path statements/reg key name, I get funky error codes (161, -2147217403, etc.), so I know it's READING the value right.... but where does it PUT the value???

edited: Thu Mar 27 14:20:32 2003 by jeffa - appended "(problem with Win32::OLE)" to title

Replies are listed 'Best First'.
Re: Where's the result??? (problem with Win32::OLE)
by jand (Friar) on Mar 26, 2003 at 20:49 UTC
    You could try passing $result by reference.
    use Win32::OLE::Variant; # ... my $result = Variant(VT_BYREF|VT_BSTR); my $return = $registry->GetStringValue(2147483650, $strPath, $strV +alue, $result);
    $result will still be a Win32::OLE::Variant object, but it should stringify correctly to the returned value.
      jand, my hero. You're right, of course. I actually had to use:
      my $result = Variant(VT_BYREF|VT_BSTR,0);
      But there it is! Thank you!
      Anyone want to take this on? I'm not that fluent with OLE::Variants, but in order to capture registry keys, you need to pass an array. For instance, to get all the subkeys under "HKEY_Users"...
      $base = ''; my @keys = Variant(VT_ARRAY|VT_UI1, ''); $result = $registry->EnumKey(2147483651, $base, @keys); print "$base\nResult: $result\n"; foreach $key (@keys) { print "Key:$key\n"; }
      I _thought_ this would propogate with the keys. No such luck. I also tried initializing the blank array to a SID (since that is the length it is going to match) but again, no luck.
        Try this:
        my $keys = Variant(VT_BYREF|VT_VARIANT); my $result = $registry->EnumKey(2147483651, $base, $keys); my @dim = $keys->Dim; for ($dim[0][0] .. $dim[0][1]) { my $key = $keys->Get($_); # ... }
        I'm in a hurry right now, but one thing I can spot immediately: @keys should be $keys, as VARIANTs are always scalars. In this case you have a reference to a SAFEARRAY, not a Perl level array.

        Oh, and another thing: Win32::OLE may be treating one dimensional arrays of VT_UI1 specially, so I'm not sure if this might complicate things some more.