# Ruby/DL question

**URL:** <https://www.ruby-forum.com/t/ruby-dl-question/173691>\
**Category:** Ruby\
**Created:** [August 13, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/173691 "2009-08-13T12:06:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 13, 2009, 12:06pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/173691/1 "2009-08-13T12:06:41Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 13, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/173691/2 "2009-08-13T18:53:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 14, 2009, 6:43am UTC](https://www.ruby-forum.com/t/ruby-dl-question/173691/3 "2009-08-14T06:43:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 14, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/ruby-dl-question/173691/4 "2009-08-14T19:32:56Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 15, 2009, 6:06am UTC](https://www.ruby-forum.com/t/ruby-dl-question/173691/5 "2009-08-15T06:06:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![grish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e9c0ed/32.png) [@grish](https://www.ruby-forum.com/u/grish)\
**Post date:** [August 18, 2009, 6:46am UTC](https://www.ruby-forum.com/t/ruby-dl-question/173691/6 "2009-08-18T06:46:20Z")

</div>

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?
