Search code examples
cmacrubyscripting-bridge

How do I create a C Pointer in MacRuby?


I am working with a C Framework in MacRuby which has a structure

int CPhidget_getSerialNumber(CPhidgetHandle phid,int * serialNumber)    

You can see the full documentation here (if you are interested).

In MacRuby I have tried the following:

i = CPhidget_getSerialNumber(@encoder, nil)

The above gets me a number but not the serial number.

CPhidget_getSerialNumber(@encoder, i)

This gets me an error stating: expected instance of Pointer, got `0' (Fixnum) (TypeError) (which doesn't surprise me).

So my main question is: Is there an equivalent of a C Pointer in MacRuby?


Solution

  • MacRuby has a Pointer class, which can stand in for a C pointer. So, you could do something like:

    i = Pointer.new(:int)
    CPhidget_getSerialNumber(@encoder, i)
    

    Then, to extract the results from the pointer (i.e. dereference it), you use Ruby's array access syntax:

    i[0] #=> value from CPhidget_getSerialNumber