Forum: Ruby WIN32OLE heartburn

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F69abe0f06c7c50b5069f2d759abebb6?d=identicon&s=25 Dave --- (drathm)
on 2006-03-27 18:31
Hi,

After using WIN32OLE, I have been having trouble using WMI to create a
objWMIService object.
1)
locator = WIN32OLE.new(â??WbemScripting.SWbemLocatorâ?)
mgmt = locator.ConnectServer computer, '/root/default:StdRegProv',
login, password
2)
mgmt =
WIN32OLE.connect(â??winmgmts:\\\\#{computer}/root/default:StdRegProvâ?)

I think these two methods should be equivalent objects, however the
first method is missing several internal structures like EnumKey.  The
second method does contain EnumKey, but I can not login with an
impersonation.  If anyone had any ideas I would appreciate them.

dave
7eac4bce18d9bb092b3947e3d5731842?d=identicon&s=25 unknown (Guest)
on 2006-03-27 21:54
(Received via mailing list)
Please see:
http://msdn.microsoft.com/library/default.asp?url=...

require 'win32ole'

HKLM = 0x80000002
computer = "."
reg =
WIN32OLE.connect("winmgmts://#{computer}/root/default:StdRegProv")
in_params = reg.Methods_("EnumKey").inParameters.SpawnInstance_()
in_params["hDefKey"] = HKLM
in_params["sSubKeyName"] =
'Software\Microsoft\Windows\Currentversion\Uninstall'
out_params = reg.ExecMethod_("EnumKey",in_params)
out_params.sNames.each { |key_names| puts key_names }
F69abe0f06c7c50b5069f2d759abebb6?d=identicon&s=25 Dave --- (drathm)
on 2006-03-27 23:55
Thank you for the reply.  Is there another way to reach a remote
registry and use impersonation?  I am not sure how to login under a
different user given your example.
dave
7eac4bce18d9bb092b3947e3d5731842?d=identicon&s=25 unknown (Guest)
on 2006-03-28 16:40
(Received via mailing list)
Try this:

require 'win32ole'

HKLM = 0x80000002
computer = "machine1"
locator = WIN32OLE.new("WbemScripting.SWbemLocator")
svc =
locator.ConnectServer(computer,"root\\default","user","password",nil,nil,nil,nil)
reg = svc.Get("StdRegProv")
in_params = reg.Methods_("EnumKey").inParameters.SpawnInstance_()
in_params["hDefKey"] = HKLM
in_params["sSubKeyName"] =
'Software\Microsoft\Windows\Currentversion\Uninstall'
out_params = reg.ExecMethod_("EnumKey",in_params)
out_params.sNames.each { |key_name| puts key_name }
8fba59b8bf5ffbab08607a55faeccbee?d=identicon&s=25 David G. (david_g63)
on 2011-06-28 19:56
unknown wrote in post #54219:
> Try this:
>
> ...
> ...
> ...


Had some trouble getting the above suggestions to work (I'm currently
using Ruby 1.9.2).  I kept messing around and finally came up with the
following that seems to do great.  It works for the local machine as
well as any remote machine for which the current user context also has
sufficient rights. It's got an example for a key enumeration, value
enumeration, and a value retrieval:



require 'win32ole'

class WIN32OLE
  def list_ole_methods
    method_names = ole_methods.collect {|m| m.name}
    # puts method_names.sort.uniq
    return method_names.sort.uniq
  end
end

HKLM                = 0x80000002
HKEY_CLASSES_ROOT   = 0x80000000
HKEY_CURRENT_USER   = 0x80000001
HKEY_LOCAL_MACHINE  = 0x80000002
HKEY_USERS          = 0x80000003
HKEY_CURRENT_CONFIG = 0x80000005
HKEY_DYN_DATA       = 0x80000006


computer = "127.0.0.1"
reg = WIN32OLE.connect("winmgmts://#{computer}/root/default:StdRegProv")


in_param1 = reg.Methods_("EnumKey").InParameters.SpawnInstance_()

puts "Help: " + in_param1.ole_obj_help.name.to_s
puts "Properties: " + in_param1.Properties_.to_s
puts
puts in_param1.list_ole_methods
puts

in_param1.hDefKey=HKLM
in_param1.sSubKeyName = "SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion"
out_param1 = reg.ExecMethod_("EnumKey",in_param1)
out_param1.sNames.each { |key_names| puts key_names }

puts

in_param2 = reg.Methods_("EnumValues").InParameters.SpawnInstance_()
in_param2.hDefKey=HKLM
in_param2.sSubKeyName = "SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion"
out_param2 = reg.ExecMethod_("EnumValues",in_param2)
out_param2.sNames.each { |value_names| puts value_names }

puts

in_param3 = reg.Methods_("GetStringValue").InParameters.SpawnInstance_()
in_param3.hDefKey=HKLM
in_param3.sSubKeyName = "SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion"
in_param3.sValueName = "PathName"
out_param3 = reg.ExecMethod_("GetStringValue",in_param3)
puts "Value: " + out_param3.sValue.to_s
This topic is locked and can not be replied to.