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?

2
  • The documentation you link to seems to be the documentation for the raw C library. How do you perform the foreign function calls? Commented May 18, 2012 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' (phidgets.com/documentation/Tutorials/Getting_Started_Cocoa.pdf) Commented May 18, 2012 at 22:55

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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: github.com/MacRuby/MacRuby/issues
Hi Josh, I will file the bug but I did end up finding a pointer class reference on the github wiki: github.com/MacRuby/MacRuby/wiki/Pointer-Class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.