Subject:
[ruby-ffi] Re: Passing Windows VARIANT by value leads to segfault
From:
Stephan Schwab
Date:
1/18/11 4:29 PM
To:
ruby-ffi

While this post had been waiting to be published I learned a bit more
about the Windows VARIANT. It is a very complex datatype that consists
of multiple structs and unions. The segmentation fault happens because
the contents of memory are wrong once the C function wants to do
something with it.

Does anyone have a VARIANT modeled using FFI::Struct and FFI::Union?
I'm sure it can be done but seems to be a lot of work and if one
element is missing --- booom.

On Jan 18, 1:23 pm, Stephan Schwab <s...@caimito.net> wrote:
> Hi!
>
> Given this code:
>
> get_accState      = FFI::Function.new(:uint32, [:pointer,
> Variant.by_value, :pointer], i_accessible_vtbl[:get_accState])
> variant_out       = Variant.new
>
> variant_in = Variant.new
> variant_init(variant_in)
> variant_in[:vt] = Constants::VT_I4   # 0x3
> variant_in[:lVal] = 0  # CHILDID_SELF
>
> acc_state_result  = get_accState.call(i_accessible, variant_in,
> variant_out)
> fail "Can't query for button state. HRESULT = 0x" +
> acc_state_result.to_s(16) unless acc_state_result == Constants::S_OK
> # S_OK = 0
>
> I get a segmentation fault when calling get_accState.call()
>
> The method get_accState() allows me to skip the second parameter and
> pass in NULL instead. If I do that (after changing the parameter type
> to a pointer and using 'nil') the function returns S_OK and does not
> crash.
>
> What might be the reason for the segmentation fault given this code?
>
> Stephan