• Welcome to Jose's Read Only Forum 2023.
 

Trying to understand working with dll's

Started by Dennis Hoskins, December 11, 2009, 09:35:51 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dennis Hoskins

Hi,

I'm looking for some advice on using dlls and, more importantly, some tricks for debugging.  I am writing a dll in Emergence Basic (I have my reasons) which I want to use from PowerBASIC.  Taking small steps first, the following is the code (EB) for my dll:

export myintadd
export mydoubleadd
export myconcat

DECLARE myintadd(in1 as INT, in2 as INT),INT
DECLARE mydoubleadd(in1 as double, in2 as double),double
DECLARE myconcat(in1 as string, in2 as string), string

SUB myintadd(in1 as INT, in2 as INT),INT
OPENCONSOLE
PRINT "In 1: ", in1
PRINT "In 2: ", in2
PRINT "Press any key to exit"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
RETURN in1 + in2
ENDSUB

SUB mydoubleadd(in1 as double, in2 as double),double
OPENCONSOLE
PRINT "In 1: ", in1
PRINT "In 2: ", in2
PRINT "Press any key to exit"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
RETURN in1 + in2
ENDSUB


SUB myconcat(in1 as string, in2 as string),string
OPENCONSOLE
PRINT "In 1: ", in1
PRINT "In 2: ", in2
PRINT "Press any key to exit"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
RETURN in1 + in2
ENDSUB


Forgetting "myconcat" for the moment, I can use this dll successfully from AutoIt.  So, I think the dll is okay.  I have written the following in PBWin 9.0


#COMPILE EXE
#DIM ALL

DECLARE FUNCTION zTrace LIB "ztrace.DLL" ALIAS "zTrace" (zMessage AS ASCIIZ) AS LONG

DECLARE FUNCTION mydoubleadd LIB "ebaDLL_Test.dll" ALIAS "mydoubleadd" (BYVAL DOUBLE, BYVAL DOUBLE) AS DOUBLE
DECLARE FUNCTION myintadd LIB "ebaDLL_Test.dll" ALIAS "myintadd" (BYVAL LONG, BYVAL LONG) AS LONG
'DECLARE FUNCTION myconcat LIB "ebaDLL_Test.dll" ALIAS "myconcat" (asciiz, asciiz) AS asciiz

FUNCTION PBMAIN () AS LONG
LOCAL dsum, d1, d2 AS DOUBLE
LOCAL isum, i1, i2 AS LONG

    isum = myintadd(25,50)
    ztrace("int add: " + STR$(isum))

    dsum = mydoubleadd(2.5, 3.4)
    ztrace("double add: " + STR$(dsum))
   
'    ztrace("concat:  " + myconcat("Hello ", "World"))
    MSGBOX "Click"

END FUNCTION


Initially, I did not have the BYVALs in the function declarations.  For "myintadd" PBWin seemed to be passing addresses and for "mydoubleadd" PBWin was passing 0.0 and 2.5.  After adding the BYVALs,  "myintadd" is working.  For "mydoubleadd", PBWin is now passing 2.5 and 3.4 (good) but the returned value is 0 (not good).

Down the road, I want to be able to pass strings and arrays.  The road at this point looks rocky.

Any suggestions for my next steps will be appreciated.

Thanks,

Dennis

José Roca

 
Sorry, but I never have used Emergence Basic. Also I don't know if both compilers return double values the same way.

Regarding strings, PB uses BSTRs whereas EB uses asciiz strings, even if it uses the keyword STRING.

One way to solve these problems is to pass a variable by reference that will be filled by your EB procedure instead of returning the value as the result of the function.

Frank Brübach

hello dennis.

perhaps my little example can help you. I have used two functions for strings as you can see and gives wished results. I don't know Emergence Basic at all. but I can show powerbasic handling with dll can looks like this one.

#COMPILER PBWIN 9
#COMPILE EXE
'--------------------------------------------------------------------
DECLARE FUNCTION MyFunction1 LIB "FrankoAdd.DLL" _
          ALIAS "MyFunction1" (BYVAL Param1 AS LONG) AS LONG
'--------------------------------------------------------------------

'--------------------------------------------------------------------
DECLARE FUNCTION RandomAdd LIB "FrankoAdd.dll" _
          ALIAS "RandomAdd" (BYVAL x AS LONG) AS LONG
'--------------------------------------------------------------------

'--------------------------------------------------------------------
DECLARE FUNCTION fstrings LIB "FrankoAdd.DLL" _
         ALIAS "fstrings" (BYREF s1 AS ASCIIZ, BYREF s2 AS ASCIIZ) AS DWORD
'--------------------------------------------------------------------

'--------------------------------------------------------------------
DECLARE FUNCTION AddOne LIB "FrankoAdd.DLL" _
          ALIAS "AddOne" (BYVAL x AS DOUBLE, BYVAL y AS DOUBLE) AS DOUBLE
'--------------------------------------------------------------------

'--------------------------------------------------------------------
DECLARE FUNCTION StringTest LIB "FrankoAdd.DLL" _
         ALIAS "StringTest" (BYVAL a AS STRING, BYVAL t AS DOUBLE) AS STRING
'--------------------------------------------------------------------


FUNCTION PBMAIN () AS LONG
    LOCAL lRes AS LONG
    LOCAL myResult AS LONG
    LOCAL d AS LONG
    LOCAL str AS STRING
    LOCAL m AS STRING
    LOCAL a AS LONG
    LOCAL b AS LONG
    LOCAL r AS LONG

    lRes = MyFunction1(lRes)
    r = Addone(r,1)
    str = stringTest(str,1)
    m = STR$(fstrings("",""))

    b = 2000
    a = RandomAdd(b)

    MSGBOX "yes we have won this random gold price: " + STR$(a), %MB_ICONINFORMATION, "TestScript"
    MSGBOX "Function feedback:" + STR$(lRes), %MB_OKCANCEL, " TestScript " + DATE$
    MSGBOX "Tell me more string secrets!: " & str, %MB_ICONQUESTION, " ..my boring String Test "
    MSGBOX "fstrings gives: : " & STR$(1,2), %MB_ICONQUESTION, " Test "

END FUNCTION


all in zip file. dll, pb example, exe file.

good night, frank