Import a LIB function that return a float

Just starting out? Need help? Post your questions and find answers here.
freddix
Enthusiast
Enthusiast
Posts: 100
Joined: Sun Feb 08, 2004 7:22 pm
Location: South France
Contact:

Import a LIB function that return a float

Post 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 ?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

Import "MyPlugin.lib"
  RotateObject.f( iid.l , fx.f , fy.f , fz.f ) As "?RotateObject@@YAXHMMM@Z"
EndImport
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
freddix
Enthusiast
Enthusiast
Posts: 100
Joined: Sun Feb 08, 2004 7:22 pm
Location: South France
Contact:

Post 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.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
freddix
Enthusiast
Enthusiast
Posts: 100
Joined: Sun Feb 08, 2004 7:22 pm
Location: South France
Contact:

Post by freddix »

The .lib to import was developed using Microsoft Visual C++ ..
From the author, it directly send back simple precision floating number.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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!) :)
I may look like a mule, but I'm not a complete ass.
freddix
Enthusiast
Enthusiast
Posts: 100
Joined: Sun Feb 08, 2004 7:22 pm
Location: South France
Contact:

Post 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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!
I may look like a mule, but I'm not a complete ass.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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? :)
I may look like a mule, but I'm not a complete ass.
freddix
Enthusiast
Enthusiast
Posts: 100
Joined: Sun Feb 08, 2004 7:22 pm
Location: South France
Contact:

Post 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 ....
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

I think DarkBasic uses CDECL, so you have to ImportC and not Import
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
freddix
Enthusiast
Enthusiast
Posts: 100
Joined: Sun Feb 08, 2004 7:22 pm
Location: South France
Contact:

Post by freddix »

@ts-soft : It's already what I do ;)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

freddix wrote:@ts-soft : It's already what I do ;)
The info of darkbasic comes to late :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply