Calling a c function from ruby?

R

Randy Lawrence

I'm having trouble calling a c function from ruby and haven't been able
to find a solution using google.

Specifically, I'd like to call functions that receive these three datatypes:

char*
char*[]
byte[]

Swig 1.3 has been great in helping me call a c++ function that uses a
std::string parameter but the 3 c datatypes mentioned above are eluding me.

Thanks.
 
R

Randy Lawrence

Christian said:
Well in C you only get VALUE.

To get a char*, you use rb_str2cstr() to get the string and its length:

myfunc(VALUE v)
{
char* pc = rb_str2cstr(v,NULL);
}

To get a char*[], you need to walk the array (passed in VALUE) and get the
strings using the above function. I guess using rb_ary_each(), you can walk
the array.

To get a BYTE*, you also use rb_str2cstr() - remember, a string may contain
any character, including '\0':

myfunc(VALUE v)
{
long len;
BYTE* data = rb_str2cstr(v,&len);
}


(There are newer APIs like StringValue() and StringValuePtr(), you might
check those also...)

Christian

I'm having trouble calling a c function from ruby and haven't been able
to find a solution using google.

Specifically, I'd like to call functions that receive these three
datatypes:

char*
char*[]
byte[]

Swig 1.3 has been great in helping me call a c++ function that uses a
std::string parameter but the 3 c datatypes mentioned above are eluding
me.

Thanks.

Many thanks!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top