Dll created with FreeBasic does not return String

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 does not return String

Post by Tolo68 »

Hello everyone. A few months ago I created a DLL that returned a String value, but now I compile it and it returns an empty value, but I found another code that works well, does anyone know what could be the cause, and which function is better and more compatible???
I have compiled it with FreeBasic 1.0.9.0 32-bit
Thank you all

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

EXTERN "windows-ms"

' This function returns an empty value, and before it worked fine.
FUNCTION Mayusculas_A (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

' This function works for me
FUNCTION Mayusculas_B (BYVAL Texto as String) Byref As String Export
Static As String s
s = UCASE(Texto)
Function = s
END FUNCTION

END EXTERN

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

' And this is the code to use the DLL
#include once "windows.bi"
#include once "win/ole2.bi"
#Inclib "fbdll"

extern "windows-ms"
Declare Function Mayusculas_A Lib "fbdll.dll" (ByVal s As String) As String
Declare Function Mayusculas_B Lib "fbdll.dll" (ByVal s As String) As String
end extern

Print "*" & Mayusculas_A("this is a string") & "*"
Print "*" & Mayusculas_B("this is a string") & "*"

sleep
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Dll created with FreeBasic does not return String

Post by srvaldez »

hello Tolo68 :)
I don't think that your conversion function is complete

Code: Select all

FUNCTION Mayusculas_A (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
however, using static may at least return a non-empty string

Code: Select all

FUNCTION Mayusculas_A (BYVAL Texto as zString ptr) AS BSTR EXPORT
    static res AS BSTR
    DIM s AS STRING
    s = UCASE(*Texto)
    res = SysAllocStringByteLen(STRPTR(s), LEN(s))
    RETURN res
END FUNCTION
the implementation is faulty, you are allocating memory but not releasing it, since this is to be in a dll, I suggest using a global return variable, ideally inside dllmain attach and release it in dll detach
you are on your own on this as I don't have example code to offer, you can search the forum for dllmain, one possibly helpful thread might be viewtopic.php?p=136223#p136223
another idea is to use shared pointers, Paul Doe has a start on that here https://github.com/glasyalabolas/fb-collections
you should ask Paul for any questions related to his code
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Dll created with FreeBasic does not return String

Post by caseih »

It's not unheard of to have a DLL allocate memory that a caller has to free. It's actually fairly common, and if that is understood clearly, okay. Using just one allocation within the DLL works alright, but the caller has to understand they must copy the string, lest it get destroyed by another call to the DLL.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Dll created with FreeBasic does not return String

Post by srvaldez »

caseih wrote: Dec 24, 2022 4:25 It's not unheard of to have a DLL allocate memory that a caller has to free. It's actually fairly common, and if that is understood clearly, okay. Using just one allocation within the DLL works alright, but the caller has to understand they must copy the string, lest it get destroyed by another call to the DLL.
yes, the simplest way is to pass a variable by reference to the dll doing the memory allocation/de-allocation in the program that uses the dll, although it would be an interesting exercise to use shared pointers
Post Reply