From: "José Augusto" Date: 2003-04-07T19:28:00+09:00 Subject: Re: Win32 DLL and Ruby 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