Sharing data between ruby and a custom dll with Win32API

Hi All,

I’m building a ruby plugin for SketchUp. I created a havok dll (havok is
a physic engine) and I need to share/pass data between ruby and the dll.
So far I had no problem to send data from ruby to the dll, but I can’t
figure out the other way.

SketchUp 8 uses ruby 1.8.6, so I use the same Win32API version.

Here is my ruby code:

def get_matrix(index)

matrix = Array.new(16,0.1)
matrix2 = @getKaplaMatrix.call(index,matrix.pack(“f*”))
matrix3 = matrix2.unpack(“f*”)

puts “HELLO HELLO HELLO”

puts matrix3

if (matrix3.size() != 16)
puts “WRONG ARRAY”
else
puts “ARRAY OK”
end
return matrix3

end

Here is my dll code:

DECLDIR float* getKaplaMatrix (int indice, float* matrix)
{
float* kaplaMatrix = havokUtilities->getKaplaMatrix(indice);

for(int i=0 ; i < 16; i++)
{
matrix[i] = kaplaMatrix[i];
}
matrix[14]/= 2;
matrix[13]/= 2;
matrix[12]/= 2;

return(matrix);
}

I’m trying to send a 4x4 matrix (stored in a [16] array) from the dll to
ruby, but I always get errors because some returned arrays are too
short. I did some tests and found that then there is an “integer”
(declared as “1.0”) in the array, the value after the “integer” are
lost…

The result is that among all the arrays I send to ruby, some of then are
of the wrong dimension (because some matrix have some zeros values for
example). I have not the slightest idea of what is happening…

Thx

Hi Futar,

I suppose you are using ‘p’ as the return type specifier of Win32API;
this is interpreted as a pointer to a null-terminated string. Therefore,
any null byte in your array of floats will mark the end of the “string”
buffer.

In this case, I think you can simply reference the same buffer you pass
to @getkaplamatrix.

matrix2 = matrix.pack(“f*”)
@getKaplaMatrix.call(index, matrix2)
matrix3 = matrix2.unpack(“f*”)

And if you really do need to return a reference to something, you will
often have to return it as an integer address and manipulate (reference
and dereference) it through native interfaces such as RtlMoveMemory.

Su

Thank you very much su, your code works :slight_smile:

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