How to pass a decimal value to a function in a dll? And a struct?

Started by newold, June 24, 2013, 01:29:54 pm

Previous topic - Next topic

newold

Hi all. See if you can help me.

FirstQuestion

I have a function in C for a dll.

 DllImport float sum (float a, float b)
{
     return a + b;
}


How do I give to that function float values ​​from RGSS?
My code in RGSS (i cant use 'f' or 'd' as param):

 n = Win32API.new ('test', 'sum', 'ii', 'i')
a = 5.10
b = 8.90
n.call p (a, b)


Last Question

I defined a point class in RGSS

class Point # by Newold
  #--------------------------------------------------------------------------
  attr_accessor   :x
  attr_accessor   :y
  #--------------------------------------------------------------------------
  def initialize(x=0,y=0)
    @x = x; @y = y
  end
  #--------------------------------------------------------------------------
end


in dll i defined this struct:

struct point
{
    float x;
    float y;
};


How do I give to a function a point ​​from RGSS? i want say, If i have this point in RGSS => Point.new(150,150), how i pass this point to a function in c as struct point.

G_G

For your first question, try using a pointer. Replace "i" with "p" and try that. I dunno if it'll work but it might. As for your second question, you could try to pack the variable in Ruby, then pass it to your DLL, then unpack it in your C side.

newold

First question: If i use 'p ', i get error: cannot convert float into string. I can convert float into string with to_s and change param in c function for char (1.85 => "1.85"), then how i convert in c this char into float

Second question. I know how to pack and unpack in ruby but no in c.

ruby ===> float = 15.5      pack ==>  packed = [15.5].pack("f")     unpack =>>> packed.unpack("f")[0]
c => Recive packed param (using 'p')    how i unpack this value??


PS: I Know, i have very low nivel in c :D

Blizzard

That should actually work. But you should make the arguments in the C function also pointers so that there are no problems.

DllImport float sum (float a, float b)
{
     return a + b;
}

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.