DLL created with FreeBasic, Integer returns value, but String does not

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

DLL created with FreeBasic, Integer returns value, but String does not

Post by Tolo68 »

Hello to all the forum.

In the FreeBasic examples, there is the example of compiling a Dll, which adds 2 numbers.

Suma y Resta, they work fine in FreeBasic, Visual Basic 6 and VB2010 (Net)
but with String strings it doesn't work well, it doesn't give an error, but it returns an empty String.

'-----------------------------------------------------

#include once "windows.bi"

EXTERN "windows-ms"

FUNCTION Suma(BYVAL N1 AS Integer, BYVAL N2 AS Integer) AS Integer EXPORT
RETURN N1 + N2
END FUNCTION

FUNCTION Resta(BYVAL N1 AS Integer, BYVAL N2 AS Integer) AS Integer EXPORT
RETURN N1 - N2
END FUNCTION

' ' 'The following 2 functions return an empty value.

FUNCTION Mayusculas (BYVAL Texto as String) AS String EXPORT
RETURN Ucase(Texto)
END FUNCTION

FUNCTION Minusculas (BYVAL Texto as String) AS String EXPORT
RETURN Lcase(Texto)
END FUNCTION

END EXTERN

'-----------------------------------------------------

Thank you very much to all!!!
Have a nice day :)
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: DLL created with FreeBasic, Integer returns value, but String does not

Post by Josep Roca »

FreeBasic dynamic strings aren't compatible with Visual Basic. The functions return a string, but VisualBasic does not know how to deal with it. You may need to return an OLE unicode string (aka BSTR) allocated with SysAllocString.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: DLL created with FreeBasic, Integer returns value, but String does not

Post by srvaldez »

hello Tolo68 :)
I played with converting to/from bstr long ago, maybe it will be of some help, hopefully others more qualified will point out improvments
note that you need to SysFreeString any bstr strings after use, that may be a problem in a DLL unless your DLL returns the string by reference

Code: Select all

#Include Once "win/wtypes.bi"
#Include Once "win/unknwn.bi"
#Include Once "win/oleauto.bi"
'
Sub StringToBSTR( Byref sb As BSTR, Byref cnv_string As Zstring )
    Dim As Integer length
    length = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, @cnv_string, -1, NULL, 0)
    sb=SysAllocStringLen(sb,length)
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, @cnv_string, -1, sb, length)
End Sub


Function BstrToString(Strng As bstr Ptr) As String
  Dim As String result
  Dim As Integer length
  Dim As Zstring Ptr buffer
  Result = "" 
  
  If Strng Then
    length = WideCharToMultiByte(CP_ACP, 0, *Strng, -1, NULL, 0, NULL, NULL) 
    Buffer = Allocate(length) 
    
    If Buffer Then
      WideCharToMultiByte(CP_ACP, 0, *Strng, -1, Buffer, length, NULL, NULL) 
      Result = *Buffer 
      Deallocate(Buffer) 
    Endif 
  Endif 
  
  Return Result 
End Function

Dim As decimal x, y, z
Dim As LCID lcid
Dim As bstr st
Dim As Integer i
Dim As Double a=0.5,d=0.1

VarDecFromI4( 12345, @x )           'convert integer to decimal in x
x.scale=3                           'x=12.345
VarDecAdd( @x, @x, @y ) 'y=x+x
VarBstrFromDec( @y, lcid, 0, @st )  'convert decimal to Bstr string

Print "integer value 12345 is scaled to 3 decimal places and then added to itself"
Print "12.345 + 12.345 = ";BstrToString(@st)'convert Bstr string to standard string and print
'st=SysAllocStringLen(st,70)         'allocate memory for Bstr string
StringToBStr (st,"3.14159265358979323846264338328")
VarDecFromStr( st, lcid, 0, @x)     'convert the string st to decimal
VarDecAdd( @x, @x, @y )             'y=x+x
VarBstrFromDec( @y, lcid, 0, @st )  'convert y to Bstr string

Print "Pi + Pi = ";BstrToString(@st)                 'convert Bstr string to standard string and print

VarDecFromI4( 1, @x ) 
VarDecFromI4( 10, @z )
VarDecDiv( @x, @z, @z )
VarDecFromI4( 5, @x )
x.scale=1
Print
Print "accumulate decimal 0.5 to 1.1 step 0.1"
For i=5 To 10
    VarDecAdd( @x, @z, @x )         'x=x+z
    VarBstrFromDec( @x, lcid, 0, @st )
    Print BstrToString(@st)
Next
Print "accumulate double 0.5 to 1.1 step 0.1"
For i=5 To 10
    a=a+d
    Print a
Next
SysFreeString(st)                   'free the memory taken by st
#Print Typeof(st)
Print __fb_version__, __fb_build_date__
Print "press RETURN to end "
Sleep
Last edited by srvaldez on Jun 13, 2022 20:35, edited 1 time in total.
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

Re: DLL created with FreeBasic, Integer returns value, but String does not

Post by Tolo68 »

Hi Joseph, thank you very much.
I remembered that I had an example from the internet a long time ago, and I found it.
The code was this...

'---------------------------
#include once "windows.bi"
#include once "win/ole2.bi"

EXTERN "windows-ms"

FUNCTION Mayusculas (BYVAL Texto as zString ptr) AS BSTR EXPORT
DIM res AS BSTR, s AS STRING
s = UCASE(*Texto)
res = SysAllocStringByteLen(STRPTR(s), LEN(s))
RETURN res
END FUNCTION

END EXTERN
'---------------------------

i tried it and it works
Is this ok, or can it be improved?
Thanks a lot
Post Reply