WIN32OLE.connect("winmgmts://") returns nil with JRuby

372 Views Asked by At

I am trying to check if a process exists in JRuby on a Windows 7 machine. I'm trying:

require 'jwin32ole'
wmi = WIN32OLE.connect("winmgmts://")
processes = wmi.ExecQuery("select * from win32_process")

However, WIN32OLE.connect("winmgmts://") is returning nil.

When I started up Excel, I was able to successfully call WIN32OLE.connect("Excel.Application"), so WIN32OLE.connect can work.

Any suggestions on how to get this working, or another way to check if a process exists?

1

There are 1 best solutions below

0
On

I found what looks like to be the problem.

In jwin32ole, this is the code for connect:

def self.connect app
  dispatcher=Java::com.jacob.activeX.ActiveXComponent.connectToActiveInstance(app)
  if dispatcher.class.to_s != "NilClass"
    AppDispatcher.new dispatcher
  else
    return nil

And looking at the API documentation for jacob.activeXComponent.connectToActiveInstance:

This will fail for any prog id with a ":" in it

Which, of course, will result in a failure while trying to connect to "winmgmts:". Why it doesn't accept colons in not explained.

Another connection method in jacob.activeXComponent would have to be directly used that doesn't have a problem with colons in the prog id.