Calling a c function from ruby?

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.

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

“Randy Lawrence” jm@zzzzzzzzzzzz.com schrieb im Newsbeitrag
news:F_ztc.15523$eH1.6931874@newssvr28.news.prodigy.com

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.

Christian Kaiser wrote:

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

“Randy Lawrence” jm@zzzzzzzzzzzz.com schrieb im Newsbeitrag
news:F_ztc.15523$eH1.6931874@newssvr28.news.prodigy.com

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!!!