Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4595 > unrolled thread

Sharing data between ruby and a custom dll with Win32API

Started by"futar C." <bourinax@yopmail.com>
First post2011-05-16 05:30 -0500
Last post2011-05-18 06:24 -0500
Articles 3 — 2 participants

Back to article view | Back to comp.lang.ruby


Contents

  Sharing data between ruby and a custom dll with Win32API "futar C." <bourinax@yopmail.com> - 2011-05-16 05:30 -0500
    Re: Sharing data between ruby and a custom dll with Win32API Su Zhang <zhangsu@live.com> - 2011-05-17 15:00 -0500
    Re: Sharing data between ruby and a custom dll with Win32API "futar C." <bourinax@yopmail.com> - 2011-05-18 06:24 -0500

#4595 — Sharing data between ruby and a custom dll with Win32API

From"futar C." <bourinax@yopmail.com>
Date2011-05-16 05:30 -0500
SubjectSharing data between ruby and a custom dll with Win32API
Message-ID<5daf6b7d2acef28191055cb217178954@ruby-forum.com>
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 why this is
happening...

Thx

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#4690

FromSu Zhang <zhangsu@live.com>
Date2011-05-17 15:00 -0500
Message-ID<5da0f9c6a53ac8a2ac026645fdbafd2e@ruby-forum.com>
In reply to#4595
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4729

From"futar C." <bourinax@yopmail.com>
Date2011-05-18 06:24 -0500
Message-ID<1126cb4cfc6fe9c447093fd261bfea5a@ruby-forum.com>
In reply to#4595
Thank you very much su, your code works :)

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web