Page 1 of 1
Floats as a return param in DLL
Posted: Tue Oct 19, 2004 11:49 am
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
How can i get float from DLL functions ?
Posted: Tue Oct 19, 2004 11:50 am
by Edwin Knoppert
If a single is enough?
Instead of double.
Posted: Tue Oct 19, 2004 11:58 am
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
Posted: Tue Oct 19, 2004 12:21 pm
by Progs
maybe its because wrong DLL import ? help !
Posted: Tue Oct 19, 2004 6:39 pm
by Rings
try following:
OpenConsole()
PrintN( StrF(test_int_()))
PrintN( StrF(peekl(test_float_())))
PrintN( StrF(peekl(test_double_())))
Input()
CloseConsole()
Posted: Wed Oct 20, 2004 5:28 am
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

Posted: Wed Oct 20, 2004 9:21 am
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
Posted: Wed Oct 20, 2004 12:31 pm
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
Posted: Wed Oct 20, 2004 3:11 pm
by Progs
Txs for replays.
will try to use libraries...
Posted: Sun Oct 24, 2004 1:52 pm
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 !!!
Posted: Sun Oct 24, 2004 2:11 pm
by jack
Posted: Sun Oct 24, 2004 3:11 pm
by Progs
Txs for link !
Posted: Sun Oct 24, 2004 7:27 pm
by Progs
It is possible not to write debug function ? or it is requared thing ?
Posted: Sun Oct 24, 2004 10:25 pm
by jack
you can write a lib without debug functions
