Results 1 to 5 of 5

Thread: what is wrong with using this PB DLL, and how to solve it

  1. #1

    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)
    

  2. #2
    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)
    
    Last edited by primo; 14-01-2017 at 13:20.

  3. #3
    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
    Last edited by primo; 15-01-2017 at 10:00.

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Last edited by Petr Schreiber; 15-01-2017 at 23:49.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Thank you Petr
    as always a great helper
    your PB dll and TB code works okay
    best wishes

Similar Threads

  1. What's wrong here?
    By ReneMiner in forum thinBasic General
    Replies: 3
    Last Post: 31-10-2012, 00:41
  2. something wrong
    By danbaron in forum O2 JIT Compiler / Assembler / Oxygen project
    Replies: 3
    Last Post: 30-08-2011, 08:40
  3. Replies: 11
    Last Post: 15-08-2011, 03:53
  4. wrong text XML help
    By Michael Hartlef in forum Fixed or cleared errors in help material
    Replies: 1
    Last Post: 16-09-2008, 16:15

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •