Subject:
Re: [ruby-ffi] Problem retrieving struct from pointer
From:
Chuck Remes
Date:
8/5/10 3:23 PM
To:
ruby-ffi@googlegroups.com


On Aug 5, 2010, at 8:12 AM, Joe wrote:

> [snip]
> typedef struct hba_AdapterAttributes {
>    char	Manufacturer[64];
>    char	SerialNumber[64];
>    char	Model[256];
>    char	ModelDescription[256];
>    HBA_WWN	NodeWWN;
>    char	NodeSymbolicName[256];
>    char	HardwareVersion[256];
>    char	DriverVersion[256];
>    char	OptionROMVersion[256];
>    char	FirmwareVersion[256];
>    HBA_UINT32	VendorSpecificID;
>    HBA_UINT32	NumberOfPorts;
>    char	DriverName[256];
> } HBA_ADAPTERATTRIBUTES, *PHBA_ADAPTERATTRIBUTES;
> 
[snip]
> ----------
> 
> In Ruby, I created the struct as so:
> class HBA_AdapterAttributes < FFI::Struct
> 	layout :Manufacturer, :char
> 	:SerialNumber, :char,
> 	:Model, :char,
> 	:ModelDescription, :char,
> 	:NodeWWN, :uint8,
> 	:NodeSymbolicName, :char,
> 	:HardwareVersion, :char,
> 	:DriverVersion, :char,
> 	:OptionROMVersion, :char,
> 	:FirmwareVersion, :char,
> 	:VendorSpecificID, :uint32,
> 	:NumberOfPorts, :uint32,
> 	:DriverName, :char
> end
> 
> Now def get_struct pointer
> def get_struct pointer
> 	HBA::HBA_AdapterAttributes.new pointer
> end
> 
> Calling the get_struct_pointer returns an array of fixnums, instead of
> any string values.
> 
> Any idea what I'm overlooking here?

Your FFI::Struct looks wrong for the char arrays/strings. You need to make sure the struct is allocating space for your string by telling it the length.

class HBA_AdapterAttributes < FFI::Struct
	layout :Manufacturer, [:char, 64],
	:SerialNumber, [:char, 64],
	:Model, [:char, 256],
	:ModelDescription, [:char, 256],
	:NodeWWN, :uint8, # this probably needs to be another struct
	:NodeSymbolicName, [:char, 256],
	:HardwareVersion, [:char, 256],
	:DriverVersion, [:char, 256],
	:OptionROMVersion, [:char, 256],
	:FirmwareVersion, [:char, 256],
	:VendorSpecificID, :uint32,
	:NumberOfPorts, :uint32,
	:DriverName, [:char, 256]
end

Check out the wiki at http://wiki.github.com/ffi/ffi/structs

Lots of good information there...

cr