Ruby/DL question

G

Gin Mendi

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 :)
 
A

Aaron Patterson

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?

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.
 
G

Gin Mendi

Aaron said:
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! :)
 
A

Aaron Patterson

Aaron said:
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.

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. :)
 
G

Gin Mendi

Aaron said:
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?
 
G

Gin Mendi

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?
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top