Subject:
[ruby-ffi] Re: Link to non-windows dlls
From:
kvberge
Date:
10/23/10 10:18 AM
To:
ruby-ffi

I can provide the c declarations to the functions I am attempting to
wrap, but I cannot provide the dll because it is a dll for the company
I work for and I am not allowed to supply the dll.

-----------------------------------------------------------------------------------------------------------
here is the code for the ruby module that is using FFI to wrap the
dll:
#usblibrary.rb

require "ffi"
module UsbLibrary
  extend FFI::Library
  ffi_lib "xrusblib.dll"
  ffi_convention :stdcall

  attach_function :OpenPort, [ :int ], :int
  attach_function :ClosePort, [ :int ], :void
  attach_function :Send, [ :int , :pointer], :bool
  attach_function :Receive, [ :int, :int ], :pointer
end
#endfile

here is the code that attemps to use it:

#test_usb.rb

require "./usblibrary"

a = UsbLibrary.OpenPort(0x3020)
puts UsbLibrary.Send(a,"some command")
puts UsbLibrary.Receive(a,1000)
b = UsbLibrary.ClosePort(a)

#endfile

Here is the c header file for the c functions I am attempting to wrap:

#include <string>

using namespace std;

int    __stdcall OpenPort(int ProductID);
void   __stdcall ClosePort(int iPort);
void   __stdcall FlushReceiveBuffer(int iPort);
string __stdcall ControlReceive(int iPort, int Timeout);
int    __stdcall ControlSend(int iPort, string strData);
BOOL   __stdcall IsDataAvailable(int iPort);
string __stdcall Receive(int iPort, int Timeout);
string __stdcall ReceiveBcpPacket(int iPort, int Timeout);
BOOL   __stdcall Send(int iPort, string strData);
----------------------------------------------------------------------------------------------------------------------

Here is some background;  I am attempting to connect to a device that
our my company makes through this custom usb library.  Basically once
we connect we have commands that we send to and responses we receive
from the instrument.  This is all made in house.  The dll is
successfully being used to run custom PC software and as I said before
I use it successfully on ruby 1.8.6 through a swig wrapper on a
Windows XP Box.

The OpenPort function will return an Id of the the port opened, this
index will then be used by the Send, Receive, and ClosePort functions
to do the requested action on the appropriate port.

On Oct 23, 10:46 am, Luis Lavena <luislav...@gmail.com> wrote:
> On Fri, Oct 22, 2010 at 8:33 PM, kvberge <kvandenbe...@gmail.com> wrote:
> > Is there a trick to using FFI and the non-windows api dll's?  I am
> > attempting to link to a custom usb dll that is known to work
> > (currently using wrapper created from swig and using ruby 1.8.6).  I
> > am now attempting to run ruby 1.9.2 and FFI seemed like a simpler
> > solution than worrying about using swig.  I should also note that I am
> > on windows 7 and ran the ruby one click installer for windows.  When
> > running the script I get this error:
>
> > Any advice?
> > If you need more information let me know.
>
> Please show the code, we need to look at the code to be able to see
> what are you talking about. Perhaps even reproduce it.
>
> Some non-microsoft DLLs uses cdecl as calling convention instead of
> stdcall, which is the default on Windows.
>
> Please provide every single piece of code and information for us to be
> able to reproduce the problem. Linsk to download of the DLL, the code
> that generates the issue, any documentation.
>
> That will really help us help you.
> --
> Luis Lavena
> AREA 17
> -
> Perfection in design is achieved not when there is nothing more to add,
> but rather when there is nothing more to take away.
> Antoine de Saint-Exupéry