Problems calling a DLL from Ruby

Hello everyone,

I'm trying to call a third-party DLL from a Ruby script. I have no
control over this DLL. The examples I have for calling this DLL's
functions are all in Visual Basic. I have registered the DLL using
regsvr32.

I've successfully used the DLL in a small VBA project, but I can't make
it work in Ruby. I think the problem may be in part because I have to
instantiate an object to call the DLL's functions. Here's what I've done
in VB :

···

---------
Function Main()

    Dim AnOPCServer As OPCServer
    Dim AllOPCServers As Variant

    Set AnOPCServer = New OPCServer
    AllOPCServers = AnOPCServer.GetOPCServers

End Function
---------

Here's one try in Ruby using Win32Ole :

Code :

---------
require './TestDNP.rb'
require 'win32ole'

test = WIN32OLE.new('C:\Windows\System32\gbda_aut.dll')
servers = test.GetOPCServers
---------

Error I get :

---------
C:\Ruby192\bin\ruby.exe -e
$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)
C:/RubyWorkspaces/testEnv/test_DNP_Comm.rb
C:/RubyWorkspaces/testEnv/test_DNP_Comm.rb:5:in `initialize': unknown
OLE server: `C:\Windows\System32\gbda_aut.dll' (WIN32OLERuntimeError)
    HRESULT error code:0x800401f3
      Chaine de classe incorrecte
  from C:/RubyWorkspaces/testEnv/test_DNP_Comm.rb:5:in `new'
  from C:/RubyWorkspaces/testEnv/test_DNP_Comm.rb:5:in `<top
(required)>'
  from -e:1:in `load'
  from -e:1:in `<main>'

Process finished with exit code 1
---------

Here's another try using Win32API. The problem here is that I don't know
how to instantiate an object using Win32API. I don't know what to pass
as the function's arguments and return value, since there are none.

Code :

---------
require "Win32API"

OPCserver = Win32API.new('C:\Windows\System32\gbda_aut.dll','OPCServer',
['0'], '0')
---------

Error I get :

---------
C:\Ruby192\bin\ruby.exe -e
$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)
C:/RubyWorkspaces/testEnv/test_DNP_Comm.rb
C:/Ruby192/lib/ruby/1.9.1/Win32API.rb:14:in `[]': unknown symbol
"OPCServer" (LoadError)
  from C:/Ruby192/lib/ruby/1.9.1/Win32API.rb:14:in `initialize'
  from C:/RubyWorkspaces/testEnv/test_DNP_Comm.rb:11:in `new'
  from C:/RubyWorkspaces/testEnv/test_DNP_Comm.rb:11:in `<top
(required)>'
  from -e:1:in `load'
  from -e:1:in `<main>'

Process finished with exit code 1
---------

Any help to successfully create an object from this DLL and call its
functions would be greatly appreciated.

Isabelle LaRoche
Junior Engineer
Gentec
Quebec, Canada

--
Posted via http://www.ruby-forum.com/.

Did you register the DLL as an administrator?

Also, did you escape the backslashes in the path name?
'C:\\Windows\\System32\\gbda_aut.dll'
Sometimes windows will accept '/', but not reliably enough for me.

···

--
Posted via http://www.ruby-forum.com/.

Joel Pearson wrote in post #1079891:

Did you register the DLL as an administrator?

Also, did you escape the backslashes in the path name?
'C:\\Windows\\System32\\gbda_aut.dll'
Sometimes windows will accept '/', but not reliably enough for me.

Thanks for answering so quickly.

I resgistered as an administrator, yes. I haven't tried escaping the
'/'. I should've thought about that!

However, I think I may have fixed the problem myself, with some help
from a collegue. We checked the registry to see if the DLL was indeed
registered. It was registered under the name "Graybox.OPC.DAWrapper", so
I figured I'd try using this in Win32OLE's new function, as so :

test = WIN32OLE.new('Graybox.OPC.DAWrapper')

It seems to work so far since I don't have the error anymore and I've
successfully called one function.

···

--
Posted via http://www.ruby-forum.com/\.