WIN32OLE heartburn

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

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

Please see:

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 }

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 }

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