Floats as a return param in DLL

Everything else that doesn't fall into one of the other PB categories.
Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Floats as a return param in DLL

Post by Progs »

a have a DLL

Code: Select all

#define API extern "C" __declspec(dllexport)
#define CALL _stdcall

API double CALL test_double() {
	return 0.12345;
}

API float CALL test_float() {
	return 0.12345f;
}

API int CALL test_int() {
	return 12345;
}
i make DLL and convert it to PB lib
now im making test PB source

Code: Select all

OpenConsole()

PrintN( StrF(test_int_()))
PrintN( StrF(test_float_()))
PrintN( StrF(test_double_()))


Input()
CloseConsole()
and what i see in console

Code: Select all

12345.000000
1.000000
1.000000
How can i get float from DLL functions ?
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

If a single is enough?
Instead of double.
Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Post by Progs »

Acctually i need floats.. double function was added just for test.
I have a good ODE wrapper library. Many function return floats.. and in PB i have BIG troubles with them. So i wrote this test DLL... all floating point results are converted to INTs
Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Post by Progs »

maybe its because wrong DLL import ? help !
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

try following:
OpenConsole()

PrintN( StrF(test_int_()))
PrintN( StrF(peekl(test_float_())))
PrintN( StrF(peekl(test_double_())))


Input()
CloseConsole()
SPAMINATOR NR.1
Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Post by Progs »

Rings wrote:try following:
OpenConsole()
PrintN( StrF(test_int_()))
PrintN( StrF(peekl(test_float_())))
PrintN( StrF(peekl(test_double_())))
Input()
CloseConsole()
Error... programm crashed :?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I'm afraid than you will need to create a real PB libs for this, as the DLL wrapper can't specify the return type (the float value is returned in ST0, that's why you get nothing). A little bit of ASM could help too:

Code: Select all

l.f

test_float_()
!FSTP dword [v_l] 

Debug l
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

Danilo wrote:Try this.

Code: Select all

Structure DOUBLE
  high.l
  low.l
EndStructure

Procedure Your_Function_That_Returns_Double()
  !FLDPI
EndProcedure

Procedure.f CatchDoubleReturn(*x.DOUBLE)
  !MOV  dword EAX,[ESP]
  !FST  qword [EAX]
  ;ProcedureReturn
EndProcedure

Procedure.f Double2Float(*x.DOUBLE)
  !MOV  dword EAX,[ESP]
  !FLD  qword [EAX]
  ;ProcedureReturn
EndProcedure

; Call your function
CallFunctionFast(@Your_Function_That_Returns_Double()) ; call DLL function
Debug CatchDoubleReturn(my.DOUBLE)

; now our double is in 'my'
; PB cant handle doubles directly, so
; lets convert it to float
Debug Double2Float(my)
Doubles are returned in ST(0), so CallFunctionF() and CallFunctionFastF()
would automatically convert it to float - if Fred adds this 1 day.

You should be careful, maybe your DLL returns a pointer to double.
In this case the above doesnt work and you have to do:
*x.DOUBLE = CallFunction....

To work with the DOUBLE variables in PureBasic after you got it
from the DLL, take a look at the F64 library from freedimension.
see viewtopic.php?t=8676
Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Post by Progs »

Txs for replays.
will try to use libraries...
Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Post by Progs »

and again... a can't make pure library...
i have C code

Code: Select all

#include <stdio.h>
#include <windows.h>

extern char *PB_DEBUGGER_ErrorDescription;
extern HWND	 PB_DEBUGGER_Window;
extern int	 PB_DEBUGGER_Control;
extern HINSTANCE PB_Instance;
extern char     *PB_StringBase;

static void SendDebuggerError(char *Text)
{
	if (PB_DEBUGGER_ErrorDescription == 0)
	{
		PB_DEBUGGER_ErrorDescription = Text;
		PB_DEBUGGER_Control = 2; // Halt the PureBasic program flow...
		SendMessage(PB_DEBUGGER_Window, WM_COMMAND, 6, 1);
	}
}

extern float _stdcall PB_myFloatReturn()
{
  return (float)15.21f;
}

extern float PB_myFloatReturn_DEBUG()
{
  return (float)15.21f;
}
Compile it in OBJ file... then run librarymaker with following DESC file

Code: Select all

;
; Langage used to code th library: ASM or C
C
;
; Number of windows DLL than the library need
0
; Library type (Can be OBJ or LIB)
;
OBJ
;
; Number of PureBasic library needed by the library
0
; Help directory name
;
Extension1
;
; Library functions (FunctionName, Arg1, Arg2, ...)
;
myFloatReturn
Float
when trying to compile code

Code: Select all

OpenConsole()
PrintN( StrF( myFloatReturn() ))
Input()
CloseConsole()
a get error
POLINK : uresolved external __fltused

any help ??? !!! plz !!!
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Post by Progs »

Txs for link !
Progs
User
User
Posts: 21
Joined: Sun Sep 19, 2004 12:42 pm
Location: Russia/Moscow

Post by Progs »

It is possible not to write debug function ? or it is requared thing ?
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

you can write a lib without debug functions :)
Post Reply