Win32 DLL and Ruby

I am learning Ruby. Though Ruby is a neat and feature rich language it
suffers from lack of good and updated documentation and tutorials in
english.

For example, I am having a difficult time with WIN32API. I have the
following functions in a dll named testdl.dll

#ifndef TESTDLL
#define TESTDLL extern “C” __declspec(dllimport)
#endif

TESTDLL float _stdcall AddNum(float a, int b);
TESTDLL int _stdcall MulNum(int a, int b);

I have succesfully used WIN32API to access MulNum. But I am unable to
access AddNum. I do not know how to access float data type through
WIN32API. I have tried the following code which does not work.

require 'Win32API’
ReturnVal = Win32API.new(“testdll”, “AddNum”, [‘n’, ‘n’], ‘f’)
t1 = ReturnVal.Call(20.2,10)
print t1, "\n"
print t1.type(), “\n”

After executing the code :
t1 = 0
t1.type = Fixnum
The results are incorrect.

Can I use WIN32API to access any dll?

With best regards,
Srijit

srijit@yahoo.com wrote in message

Though Ruby is a neat and feature rich language it suffers from
lack of good and updated documentation and tutorials in english

Very true with respect to WIN32API :frowning:

For example, I am having a difficult time with WIN32API. I have the
following functions in a dll named testdl.dll

If you can send me in a private email the copy of your DLL, perhaps
I may be able to help you. I will start by playing around with different
values of ‘N’,‘I’,‘L’ etc.

– shanko

I am learning Ruby. Though Ruby is a neat and feature rich language it
suffers from lack of good and updated documentation and tutorials in
english.

Yes and no. There are some very good Ruby books and a growing
number of online and print articles. But there are any number of
sub-topics within Ruby (such as Win2k programming) that can always use
more in-depth description.

Take a look at http://www.ruby-doc.org and see if there is anything
there that can help.

James

Hi,

I’ve been doing some C extension code in Ruby recently only on Linux.
I have no experience with C coding under Windows or in using Win32API.

But I have a few suggestions that may help you. Take them with care,
though…

According to Chapter 16 of the Pickaxe book
from Thomas and Hunt (it is online at http://www.rubycentral.com/book/).
and to an Win32API example in page 512 of the book
(appears in http://www.rubycentral.com/book/lib_windows.html), you should
use
‘i’ (or ‘I’) to represent an integer and ‘n’ or ‘l’ (or ‘N’ or ‘L’)
to represent ‘numbers’,
which include floating values. So
you should use to call your function

ReturnVal = Win32API.new(“testdll”, “AddNum”, [‘n’, ‘i’], ‘n’)
or
ReturnVal = Win32API.new(“testdll”, “AddNum”, [‘L’, ‘I’], ‘L’)
or, perhaps
ReturnVal = Win32API.new(“testdll”, “AddNum”, [‘L’, ‘N’], ‘L’)

From the docs appears that ‘L’ and ‘N’ are interchangeable.
From your code it appears that ‘n’ and ‘i’ can be used to refer to an int
argument.
However, ‘f’ is not documented in the book
(and doesn’t appear in Win32API.c sources! I checked it…)
so it doesn’t represent a float.

Also, perhaps you shoud get space to the returned value
and you must unpack it (see the docs I mentioned above).
Here is my suggestion (NOT TESTED!)

require ‘Win32API’

Get space for one pointer ??? (perhaps not needed)

temp = " " * 4
ReturnVal = Win32API.new(“testdll”, “AddNum”, [‘L’, ‘I’], ‘L’) # or
‘N’ instead of ‘L’
temp = ReturnVal.Call(20.2,10)

unpack your returned value float

t1 = temp.unpack(“f”)
print t1, “\n”
print t1.type(), “\n”

I don’t know how if all this stuff is needed (perhaps just
getting rid of ‘f’ is enough…). see table 22.8 of the book for
references to the unpack possible values.

Hope this helps

J. Augusto

srijit@yahoo.com wrote:

···

I am learning Ruby. Though Ruby is a neat and feature rich language it
suffers from lack of good and updated documentation and tutorials in
english.

For example, I am having a difficult time with WIN32API. I have the
following functions in a dll named testdl.dll

#ifndef TESTDLL
#define TESTDLL extern “C” __declspec(dllimport)
#endif

TESTDLL float _stdcall AddNum(float a, int b);
TESTDLL int _stdcall MulNum(int a, int b);

I have succesfully used WIN32API to access MulNum. But I am unable to
access AddNum. I do not know how to access float data type through
WIN32API. I have tried the following code which does not work.

require ‘Win32API’
ReturnVal = Win32API.new(“testdll”, “AddNum”, [‘n’, ‘n’], ‘f’)
t1 = ReturnVal.Call(20.2,10)
print t1, “\n”
print t1.type(), “\n”

After executing the code :
t1 = 0
t1.type = Fixnum
The results are incorrect.

Can I use WIN32API to access any dll?

With best regards,
Srijit

Hi,

···

At Sat, 5 Apr 2003 22:43:40 +0900, srijit@yahoo.com wrote:

TESTDLL float _stdcall AddNum(float a, int b);
TESTDLL int _stdcall MulNum(int a, int b);

Win32API doesn’t support float nor double. You need DL.


Nobu Nakada