How does one declare and external function (in a DLL) for use with Ruby
which requires doubles as parameters and likewise returns double as a
result? Is there any good detailed doc/info on the web on this?
I have a C fct declared as
double jadd2( double a, double b )
and export it from a DLL it gets compiled into. I want to call it from Ruby.
Import it into Ruby as:
jadd2 = Win32API.new('jadd.dll', 'jadd2', 'dd', 'd')
and call as
p jadd2.Call( 1.3, 2.4 )
the error is “in `Call’: Wrong number of parameters: expected 0, got 2.
(RuntimeError)”
However, a similar fct declared as short jadd(short a, short b) and imported
into Ruby with ‘II’, ‘I’ as the Win32API.new import and export parameters
works just fine!
What am I missing?
Thanks,
–Jakub Roth
Hi,
ROTH Jakub jroth@csob.cz writes:
How does one declare and external function (in a DLL) for use with Ruby
which requires doubles as parameters and likewise returns double as a
result? Is there any good detailed doc/info on the web on this?
Win32API does not support double.
I have a C fct declared as
double jadd2( double a, double b )
and export it from a DLL it gets compiled into. I want to call it from Ruby.
Import it into Ruby as:
jadd2 = Win32API.new(‘jadd.dll’, ‘jadd2’, ‘dd’, ‘d’)
and call as
p jadd2.Call( 1.3, 2.4 )
the error is “in `Call’: Wrong number of parameters: expected 0, got 2.
(RuntimeError)”
Use Ruby/DL:
% cat foo.c
double foo(double a, double b)
{
return a + b;
}
% gcc -mno-cygwin -shared foo.c -o foo.dll
% cat footest.rb
require ‘dl/import’
module Foo
extend DL::Importable
dlload “foo”
extern “double foo(double, double)”
end
p Foo.foo(1.1, 2.2)
% ruby footest.rb
3.3
···
–
eban