win32api calling with doubles

R

ROTH Jakub

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
 
W

WATANABE Hirofumi

Hi,

ROTH Jakub said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top