Subject: [ruby-ffi] Re: Basic question about types |
From: Wayne Meissner |
Date: 10/25/09 10:24 PM |
To: ruby-ffi@googlegroups.com |
In general, no, you can't call va_list functions. I mean, technically it is possible if you allocate memory, pack the arguments in the same way the native code would, then pass that pointer in (I think), but if you have the choice, you may as well call the varargs version. FFI could be made do that behind the scenes - it just hasn't come up before. Would be a good project for someone, so if you want it, add it to the issue tracker (http://github.com/ffi/ffi/issues), and someone may pick it up. 2009/10/22 Gimi <liang.gimi@gmail.com>:
<wmeiss...@gmail.com> wrote:Thank you very much Wayne, thank you for your so-detailed replies! For the 2nd question, I'd already known about :varargs and its usage. My question is how about a function which uses va_list as a parameter, like this: int vprintf ( const char * format, va_list arg ); According to my test, it's impossible to do it in this way: #-- code -- attach_function :vprintf, [:string, :varargs], :int # -- code end -- As va_llist actually is void*, so I guess I should do: #-- code -- attach_function :vprintf, [:string, :pointer], :int #-- code end -- But I don't know how to call it. Although it's not important to do that because there are always(i guess) a :varargs version for this kind of va_list-style-function, I just want to know is it possible to do this in Ruby-FFI. With many thanks! Sincerely, Gimi On Oct 21, 7:21 am, Wayne Meissner<liang.g...@gmail.com>:You specifiy variadic parameters as :varargs, and when you call the function, you need to explicitly specify the type of the parameters. e.g. # this says printf takes a string parameter, followed by a variable number of parameters attach_function :printf, [ :string, :varargs ], :int # You don't need to specify the type of any fixed params, since it is part of # the function signature, but you do need to for each varargs parameter LibC.printf("Hello, %s", :string, "World") 2009/10/21 GimiHi, it's me again : )
I just wanna ask one more question, how to deal with the va_list parameter? Like the vprintf method, how to attach and call it?
With many thanks! Gimi
<liang.g...@gmail.com> wrote:On Oct 20, 10:10 am, GimiHi all,
I'm new to FFI, and I have a basic question about types. Assume there is some C code like this: # -- C code begin -- #if some situation typedef sometype unsignedint #else typedef sometype int #endif
void some_function(sometype); #-- C code end --
So I mean when the types of parameters of a function are different in different situations, how do I write the Ruby code?
Any help will be greatly appreciated.
Regards,