From: Eric Hodel Date: 2005-03-30T04:14:51+09:00 Subject: Re: DL usage / DL documentation Whoops, that last one made it out the door due to an accidental mouse click. On 29 Mar 2005, at 05:49, Stephan K�mper wrote: > Eric Hodel wrote: >> On 28 Mar 2005, at 09:59, Stephan K�mper wrote: >>> Hi all, >>> >>> I currently try to call a function in a DLL, which has a prototype >>> like this: >>> >>> int test(int an_int, long * a_ptr_to_long, char * a_string); >>> >>> How do I get a 'long *' into the method from Ruby using DL? >> DL just does it: > > Thanks for answering. I'm sure it does. But apparently I'm a bit > stupid these days - I still don't get it. > >> require "dl" >> require "dl/import" >> require "dl/struct" >> module LIBC >> extend DL::Importable >> dlload "libc.dylib" >> typealias "const time_t *clock", "long ref" > > What does the 'clock' and 'ref' part mean it this typealias? > Does that mean a "const time_t *" is just another (type) name for > "long"? The header for localtime is "struct tm *localtime(const time_t *);" I shouldn't need the clock in there, but even still DL doesn't know what a time_t is (it's a long on most 32 bit platforms), so typealias teaches DL what a time_t * really is (the same as "long *", which is a "long ref", see dl/types.rb). >> extern "struct tm * localtime(const time_t *clock)" >> def self.c_localtime(clock) >> tm = LIBC.localtime(clock) >> return LIBC::StructTm.new(tm) >> end >> end >> p LIBC.c_localtime(Time.now.to_i).tm_min > > What would I have to do if the called DLL function changes the thing > pointed to? > > I'll just return to 'my' function mentioned above: > > int test(int an_int, long * a_ptr_to_long, char * a_string); > > As far as I understood, something like this might be a start: > > require "dl" > require "dl/import" > require "dl/struct" > > module Foo > extend DL::Importable > dlload "mylinb.dll" > extern "int test(int, long *, char)" extern "int test(int, long *, char *)" > end > > # Now create some variables, to pass into 'Foo.test' > > index = 42 > name = "F. Prefect" > > # ... missing code here to create ptr_to_long > > ret = Foo.test( index, ptr_to_long, name ) > > How (I think) that's my question: How can I creatthat ptr_to_long > thing - and how would I get back the value (to which it points after > the function call)? If you don't need the value back: ret = Foo.test(42, 3, "F. Prefect") DL automatically turns the number you pass in into a pointer for you. I'm not sure how you'd be able to get at the value if the function changes the value of the pointer, though. That section seems to be missing from all the documentation I've looked at. -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04