Ruby/DL question

Been trying to learn how to use ruby and Ruby/DL and I’m unsure how to
implement this:

I have a callback function in C which I want to implement in ruby

void my_callback(my_struct resultList, DWORD count)
{
while (count != 0)
{
if (resultList != NULL)
{
printf(“field1 - %s , field2 - %f \n”, resultList->field1,
resultList->field2);
}
resultList++;
count–;
}
}

I started with this:

MY_CALLBACK = DL.callback(‘0PL’) { |result_list, count|
if result_list
record = MyStruct.new(result_list)
puts record.field1
end
}

My problem is I don’t know how to get the other records if result_list
points to 2 or more records. I want to be able to traverse the whole set
or records like in the C callback function. I understand that I will
need to loop “count” times but how do I make my pointer point to the
next record?

Thanks for the help :slight_smile:

On Thu, Aug 13, 2009 at 07:06:51PM +0900, Gin Mendi wrote:

{

MY_CALLBACK = DL.callback(‘0PL’) { |result_list, count|
next record?
Result list should always be a pointer to a pointer, right? If that is
the case, you need to create a struct of size count that contains that
array.

From the new list struct, you should be able to access each MyStruct.

Aaron P. wrote:

On Thu, Aug 13, 2009 at 07:06:51PM +0900, Gin Mendi wrote:

{

MY_CALLBACK = DL.callback(‘0PL’) { |result_list, count|
next record?
Result list should always be a pointer to a pointer, right? If that is
the case, you need to create a struct of size count that contains that
array.

From the new list struct, you should be able to access each MyStruct.

I’m not so sure how I should exactly implement this. Been researching
online and trying out stuff with the code but haven’t gotten any luck.

I first tried creating a structure that just contains an array:

ResultList = struct [
“PVOID list[]”
]

and then using this with resultList

rl = ResultList.new(resultList)

but this doesn’t seem to work for me.

How should I exactly use resultList to get the array of records (I’m
assuming it’s always an array of records). Right now the previous code i
posted just captures the first record and puts it in my structure
properly. Still can’t get the other records.

Thanks for reply! :slight_smile:

On Fri, Aug 14, 2009 at 01:43:24PM +0900, Gin Mendi wrote:

rl = ResultList.new(resultList)

but this doesn’t seem to work for me.

How should I exactly use resultList to get the array of records (I’m
assuming it’s always an array of records). Right now the previous code i
posted just captures the first record and puts it in my structure
properly. Still can’t get the other records.

Hmmm. You wouldn’t happen to have some sample C code I could compile
and try accessing with DL?

Thanks for reply! :slight_smile:

No problem! I’m happy to help solve this, but I want to make sure we’re
on the same page. :slight_smile:

Aaron P. wrote:

On Fri, Aug 14, 2009 at 01:43:24PM +0900, Gin Mendi wrote:

Hmmm. You wouldn’t happen to have some sample C code I could compile
and try accessing with DL?

All I have are the compiled DLLs and the callback sample that is called
by the function I use that I first posted:

void my_callback(my_struct resultList, DWORD count)
{
while (count != 0)
{
if (resultList != NULL)
{
printf(“field1 - %s , field2 - %f \n”, resultList->field1,
resultList->field2);
}
resultList++;
count–;
}
}

which I attempted to translate to:

MY_CALLBACK = DL.callback(‘0PL’) { |result_list, count|
if result_list
record = MyStruct.new(result_list)
puts record.field1
end
}

The C callback works fine. My ruby callback works if count is 1. If it’s
more than 1 I only end up showing the first record. MyStruct is just a
structure full of string, longs, int, and float values. my_struct data
type in the C callback is just a pointer for the structure. Is there any
other information I can give?

I’m assuming the API will return an array when count is greater than
one. So I was thinking I’d just convert the PtrData object I get from
the callback to an array. Been trying to look on how I can go about
doing that but can’t seem to get it done. Everything I find just shows
how to convert pointers to structures. I tried making a structure with
an array in it and using that but that doesn’t seem to work. How can I
convert this pointer to an array?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs