How does one use pack / unpack when dealing with pointers

I am using Ruby to talk to a device using ioctl calls. This works great, but
I have a case where I need to pass a pointer of the data area in a packet
and then reference the data, which is structure that varies according the
type of event that is returned.

I got everything working for the cases that do not involve pointers. I am
not sure how to do it.

I am doing something like…

class Control

item1: input (int)

item2: output (int)

GetEZPacket = Struct.new(“Get Easy Packet”, :item1, :item2)

eventQueue: input (int)

eventNumber: output (int)

eventData: input/output (pointer)

GetEventPacket = Struct.new(“Get Event Packet”, :eventQueue, :eventNumber,
:eventData)

stuff1: output (int)

stuff2: output (int)

EventDataFor1 = Struct.new(“Event Data For 1”, :stuff1, :stuff2)

stuff3: output (int)

stuff4: output (int)

stuff5: output (int)

EventDataFor2 = Struct.new(“Event Data For 2”, :stuff3, :stuff4, :stuff5)

This works for cases without pointers

def getEZStuff( device, item )
ezPacket = GetEZPacket.new( item, 0 )
arg = ezPacket.to_a.pack(“ii”)
device.ioctl( IOCTL_EZ, arg )
result = arg.unpack(“ii”)
result[1]
end

This is where I need help

def getEvent( device, queue )
# How do I set the pointer argument to point to a data region big
# enough to hold either different return structures?
area = " " * 256
gep = GetEventPacket.new( queue, 0, area )
arg = gep.to_a.pack(“iiP256”)
device.ioctl( IOCTL_EVENT, arg )
result = arg,unpack(“iiP256”)

if result[1] == 1 then
  # Is this how I unpack the pointer and associate it with my Struct?
  ra = result[2].unpack("i,i")
  edf1 = EventDataFor1.new( ra[0], ra[1] )
  
  if edf1.stuff1 == BLAH then
  ... once I get to here everything is a breeze

end
end

Any advice appreciated.
Thanks,
Dale

  def getEvent( device, queue )
    # How do I set the pointer argument to point to a data region big
    # enough to hold either different return structures?
    area = " " * 256
    gep = GetEventPacket.new( queue, 0, area )
    arg = gep.to_a.pack("iiP256")

       arg = gep.to_a.pack("iiP")

    device.ioctl( IOCTL_EVENT, arg )
    result = arg,unpack("iiP256")
    
    if result[1] == 1 then
      # Is this how I unpack the pointer and associate it with my Struct?

     yes,

a stupid example

pigeon% cat tt.c
#include <ruby.h>

struct vv {
    int i, j;
};

struct tt {
    int j;
    struct vv *vv0;
};

static VALUE
tt_tt(obj, a)
    VALUE obj, a;
{
    struct tt *tt0 = (struct tt*)RSTRING(a)->ptr;
    tt0->j = 24;
    tt0->vv0->i = 32;
    tt0->vv0->j = 72;
    return Qnil;
}

void Init_tt()
{
    rb_define_method(rb_cObject, "tt", tt_tt, 1);
}
pigeon%

pigeon% cat b.rb
#!/usr/bin/ruby
require 'tt'
a = [12, "abcdefghijk"].pack("iP")
tt(a)
res = a.unpack("iP11")
p res[0]
p res[1].unpack("ii")
pigeon%

pigeon% b.rb
24
[32, 72]
pigeon%

Guy Decoux