# Win32 DLL and Ruby

**URL:** <https://rubytalk.org/t/win32-dll-and-ruby/5518>\
**Category:** ruby-talk\
**Created:** [5 April 2003 13:43 UTC](https://rubytalk.org/t/win32-dll-and-ruby/5518 "2003-04-05T13:43:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srijit](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Srijit](https://rubytalk.org/u/Srijit)\
**Post date:** [5 April 2003 13:43 UTC](https://rubytalk.org/t/win32-dll-and-ruby/5518/1 "2003-04-05T13:43:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shashank\_Date2](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@Shashank\_Date2](https://rubytalk.org/u/Shashank_Date2)\
**Post date:** [5 April 2003 16:24 UTC](https://rubytalk.org/t/win32-dll-and-ruby/5518/2 "2003-04-05T16:24:08Z")

</div>

[srijit@yahoo.com](mailto: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 ☹

> 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

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [5 April 2003 16:26 UTC](https://rubytalk.org/t/win32-dll-and-ruby/5518/3 "2003-04-05T16:26:54Z")

</div>

> 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](http://www.ruby-doc.org) and see if there is anything  
there that can help.

James

---

<div class="post-metadata">

**Author:** ![Jose\_Augusto](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Jose\_Augusto](https://rubytalk.org/u/Jose_Augusto)\
**Post date:** [7 April 2003 10:28 UTC](https://rubytalk.org/t/win32-dll-and-ruby/5518/4 "2003-04-07T10:28:00Z")

</div>

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/](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](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](mailto: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

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [7 April 2003 11:49 UTC](https://rubytalk.org/t/win32-dll-and-ruby/5518/5 "2003-04-07T11:49:06Z")

</div>

Hi,

> **···**
>
> At Sat, 5 Apr 2003 22:43:40 +0900, [srijit@yahoo.com](mailto: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
