Page 1 of 1
Import a LIB function that return a float
Posted: Mon Oct 27, 2008 3:42 pm
by freddix
Hi All,
I need to import a .lib file that return float numbers.
For example :
Code: Select all
Import "MyPlugin.lib"
RotateObject( iid.l , fx.f , fy.f , fz.f ) As "?RotateObject@@YAXHMMM@Z"
EndImport
I must chen use the function this way :
MyFloat.f = RotateObject( ObjectID.l, FX.f, FY.f, FZ.f )
The problem is that I don't get MyFloat correctly as floating number.
I must do that to get things works :
MyInteger.l = RotateObject( ObjectID.l, FX.f, FY.f, FZ.f )
MyFloat.f = PeekF( @MyInteger )
Is it a PureBASIC import BUG or Am I doint something wrong ?
Posted: Mon Oct 27, 2008 3:44 pm
by ts-soft
Code: Select all
Import "MyPlugin.lib"
RotateObject.f( iid.l , fx.f , fy.f , fz.f ) As "?RotateObject@@YAXHMMM@Z"
EndImport
Posted: Mon Oct 27, 2008 3:55 pm
by freddix
I've already tried this
It doesn't work.
It gives the same result than when I don't add the .f at the end of function name.
Posted: Mon Oct 27, 2008 6:19 pm
by tinman
How it gets returned depends on your compiler that you used to create the .lib. You should create a test application in the same language, check the ASM output from it and see how the test application gets the returned value.
I don't know if PB supports anything other than returning values in EAX (on Windows/x86), so if it gets returned somewhere else, such as the top of the FPU stack, then you'll need to write a small assembly language procedure to call the function for you.
Posted: Mon Oct 27, 2008 7:44 pm
by freddix
The .lib to import was developed using Microsoft Visual C++ ..
From the author, it directly send back simple precision floating number.
Posted: Mon Oct 27, 2008 8:34 pm
by srod
@tinman, PB takes its floating point returns from the top of the fpu stack - both for internal functions and imported ones.
@Freddix : the fact that you get the same return when importing the function with .f as without suggests that you are perhaps mis-using this function somehow!
**EDIT : just seen the
I must do that to get things works :
MyInteger.l = RotateObject( ObjectID.l, FX.f, FY.f, FZ.f )
MyFloat.f = PeekF( @MyInteger )
All this means is that the function is returning the float value in eax rather than leaving it at the top of the floating point stack - which is fair enough I guess (if a little strange!)

Posted: Tue Oct 28, 2008 8:41 am
by freddix
@srod :
In fact, I don't exactly get the same result. I don't get the same number but both number are recognized as integer (.l) by PureBASIC ... and never change ... apparently, I always get the pointer to the float and not the float itself... I'll check with the author of the plugin concerning this.
Thank you.
Regards,
Fred
Odyssey-Creators
Posted: Tue Oct 28, 2008 10:48 am
by srod
Sorry I don't understand. Is this code :
Code: Select all
MyInteger.l = RotateObject( ObjectID.l, FX.f, FY.f, FZ.f )
MyFloat.f = PeekF( @MyInteger )
giving you the correct result?
If so then there is no pointer being returned, simply that the result is being placed in eax. Returning a pointer would require that the function reserved some memory to hold the float value and it is very unlikely that any public function is going to do that because there would be the problem of freeing that memory!
Posted: Tue Oct 28, 2008 12:48 pm
by tinman
Just for info, I've tested with MSVC 2005 and it returns single precision floats in the FPU stack. Using .f in the import worked fine with the PB code I tested.
Posted: Tue Oct 28, 2008 1:57 pm
by srod
Yes my understanding is that VC, with all supported calling conventions, will return floats from functions by simply leaving them on the top of the fpu stack. This code of Freddix's must be taking steps to leave the result in eax for some reason as yet unknown?

Posted: Tue Oct 28, 2008 4:05 pm
by freddix
@Srod : You're probably right.
I'm not the author of the .lib files I'm working to integrate in PureBASIC ... but with:
Code: Select all
MyInteger.l = RotateObject( ObjectID.l, FX.f, FY.f, FZ.f )
MyFloat.f = PeekF( @MyInteger )
it working fine.
As I've done a small program to get the functions names an create the Import file, I've modified it to create WRAP Procedure for .lib functions that'll return a .f in eax format ...
Now all is correctly working...
In fact, I can reveal what these .lib files are ... They're DarkSDK From TheGameCreators.
I've found the way to make DarkSDK become compatible with PureBASIC and it's top :p
I'm working now on translating DarkSDK C++ source code samples to PureBASIC source code samples

I've checked with my contact(s) at TheGameCreators and we will check how it will be released ....
Posted: Tue Oct 28, 2008 4:22 pm
by ts-soft
I think DarkBasic uses CDECL, so you have to ImportC and not Import
Posted: Tue Oct 28, 2008 4:28 pm
by freddix
@ts-soft : It's already what I do

Posted: Tue Oct 28, 2008 4:31 pm
by ts-soft
freddix wrote:@ts-soft : It's already what I do

The info of darkbasic comes to late
