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
Dll created with FreeBasic does not return String
Re: Dll created with FreeBasic does not return String
hello Tolo68
I don't think that your conversion function is complete
however, using static may at least return a non-empty string
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
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
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
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
Re: Dll created with FreeBasic does not return String
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.
Re: Dll created with FreeBasic does not return String
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 pointerscaseih 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.