Author Topic: Trying to understand working with dll's  (Read 3039 times)

0 Members and 1 Guest are viewing this topic.

Offline Dennis Hoskins

  • Newbie
  • *
  • Posts: 1
  • User-Rate: +0/-0
Trying to understand working with dll's
« on: December 11, 2009, 09:35:51 PM »
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:
Code: [Select]
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

Code: [Select]
#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

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2483
  • User-Rate: +204/-0
Re: Trying to understand working with dll's
« Reply #1 on: December 11, 2009, 11:55:33 PM »
 
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.

Offline Frank Brübach

  • Full Member
  • ***
  • Posts: 109
  • User-Rate: +13/-5
Re: Trying to understand working with dll's
« Reply #2 on: December 12, 2009, 12:29:54 AM »
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.

Code: [Select]
#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