View Full Version : what is wrong with using this PB DLL, and how to solve it
i have made this PB 9.07 dll to call the Retain$ function inside it from thinbasic, but i got errors, (the same with calling it in PB : errors even the exe compiled successfully),
the PB DLL code:
#COMPILE DLL 'directs compiler to create DLL
FUNCTION Retain ALIAS "Retain" (BYVAL st1 AS STRING, BYVAL st2 AS STRING) EXPORT AS STRING
a$ = st1: b$ = st2
Retain = RETAIN$(a$, ANY b$)
END FUNCTION
thinbasic caller code:
Declare Function Retain Lib "dll_pb.dll" Alias "Retain" (st1 As String, st2 As String) As String
Dim tst As String
Dim tst2 As String
Dim res As String
tst = "abgdhowz56uy"
tst2= "adoy6867"
res = Retain(tst, tst2)
MsgBox (0,res)
i come very close to the solution, in fact it now runs okay in PB, but still i got errors calling the dll from thinbasic
PB DLL
#COMPILE DLL 'directs compiler to create DLL
FUNCTION Retain ALIAS "Retain" (st1 AS STRING,st2 AS STRING) EXPORT AS DWORD
DIM a AS STRING
DIM b AS STRING
a = st1: b = st2
DIM RSLT AS STRING
RSLT = RETAIN$(a, ANY b)
FUNCTION = STRPTR(RSLT)
END FUNCTION
PB 9.07 Code (which works):
#COMPILE EXE
DECLARE FUNCTION Retain LIB "dll_pb.dll" ALIAS "Retain" (st1 AS STRING, st2 AS STRING) AS DWORD
FUNCTION PBMAIN () AS LONG
DIM tst AS STRING
DIM tst2 AS STRING
DIM res AS DWORD
tst = "abgdhowz56uy"
tst2= "adoy6867"
res = Retain(tst, tst2)
ss$ = PEEK$(res, LEN(tst2))
MSGBOX (ss$)
END FUNCTION
the code output the characters ado6y it is all the characters of adoy6867 found in string abgdhowz56uy , it is really a great function, it is like a cleaner machine but works on letters.
thinbasic code: (does not work yet)
Declare Function Retain Lib "dll_pb.dll" Alias "Retain" (st1 As String, st2 As String) As DWord
Dim tst, tst2, ss As String
Dim res As DWord
tst = "abgdhowz56uy"
tst2= "adoy6867"
res = Retain(tst, tst2)
ss = Peek$(res, Len(tst2))
MsgBox (0,ss)
while looking at the thinbasic help file i have found these functions:
s = LETTER$(string_expression)
s = LETTER_SetMask$(string_expression)
s = LETTER_GetMask$
so i suspect they are usable in my problem, and incredibly they works with thinbasic so i don't need to call the PB retain$ inside a DLL.
but i'm still need advice for why the above 2 attempts does not work, or at least in the second attempt it works in PB but not in thinbasic
here is a pure thinbasic code to output all the characters of adoy6867 found in string abgdhowz56uy, the result should be ado6y:
Dim tst, tst2, ss As String
tst = "abgdhowz56uy"
tst2= "adoy6867"
ss = LETTER_SetMask$(tst2)
ss = LETTER$(tst)
MsgBox (0,ss)
very simple and very short
Petr Schreiber
15-01-2017, 23:41
Hi Primo,
thanks for sharing an interesting problem.
First, PowerBASIC license prohibies to wrap PB functions to be exported via the approach you do, please be careful about doing this.
Second, do you realize you return pointer to local variable in function? Local variables are released once the function is over. It may work magically in PB, but this is something which will not work in calls from other language.
How to solve it?
Implement Retain your own way, and return string from function:
PB DLL:
#COMPILE DLL "dll_pb.dll"
FUNCTION Retain ALIAS "Retain" (BYREF mainString AS STRING, BYREF matchString AS STRING) EXPORT AS STRING
REGISTER i AS LONG, position AS LONG
DIM mainByte(1 TO LEN(mainString)) AS BYTE AT STRPTR(mainString)
DIM matchByte(1 TO LEN(matchString)) AS BYTE AT STRPTR(matchString)
DIM result AS STRING
result = SPACE$(LEN(mainString))
DIM resultByte(1 TO LEN(result)) AS BYTE AT STRPTR(result)
DIM resultCharIndex AS LONG
FOR i = 1 TO LEN(mainString)
ARRAY SCAN matchByte(), =mainByte(i), TO position
IF position THEN
INCR resultCharIndex
resultByte(resultCharIndex) = mainByte(i)
END IF
NEXT
FUNCTION = LEFT$(result, resultCharIndex)
END FUNCTION
ThinBasic code:
Declare Function Retain Lib "dll_pb.dll" Alias "Retain" (byref st1 As String, byref st2 As String) As string
string tst = "abgdhowz56uy"
string tst2 = "adoy6867"
msgbox 0, Retain(tst, tst2)
Petr
Thank you Petr
as always a great helper
your PB dll and TB code works okay
best wishes