3

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
tsugua
  • 197
  • 1
  • 13
  • The documentation you link to seems to be the documentation for the raw C library. How do you perform the foreign function calls? – Niklas B. May 18 '12 at 22:52
  • I have a cocoa framework based off the C-API. The functions are all the same (apparently). I generated a bridge support file to access the constants in the header file and it _seems_ to be working. Here's the Cocoa 'guide' (http://www.phidgets.com/documentation/Tutorials/Getting_Started_Cocoa.pdf) – tsugua May 18 '12 at 22:55

1 Answers1

2

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
Josh
  • 1,387
  • 2
  • 13
  • 15
  • Awesome, Thanks for your help Josh. I couldn't find any reference to the pointer class in Ruby docs or the Apple documentation. Just out of curiosity, do you know where the docs on it are? – tsugua May 20 '12 at 04:19
  • It seems we haven't actually documented this yet (normally you'd be able to read the documentation using `macri`). If you'd like to file a bug requesting documentation, you can do so here: https://github.com/MacRuby/MacRuby/issues – Josh May 21 '12 at 09:33
  • Hi Josh, I will file the bug but I did end up finding a pointer class reference on the github wiki: https://github.com/MacRuby/MacRuby/wiki/Pointer-Class – tsugua May 21 '12 at 23:24