Announcement

Collapse
No announcement yet.

Passing an array from DLL to PBasic application.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing an array from DLL to PBasic application.

    I need to create an array of strings in a DLL then call the array from a PBasic Console programme. Embarrassed to say I can't find any help on the method.

    The array in the DLL might look something like:

    SUB GetArr(BYVAL FirstElement AS DWORD, Total AS LONG) EXPORT
    DIM A() AS STRING
    DIM i AS LONG
    DIM t AS LONG

    t = 3
    REDIM A(1 TO t) AS STRING AT FirstElement
    FOR i = 1 TO t
    A(i) = STR$(i)
    NEXT i
    Total = t ' (?)
    END SUB

    If the SUB is in error, please let me know. If not, how do I actually load the array into a PBCC program array? Any help gratefully received.

  • #2
    This should help you along


    Code:
    ' code for the Console app in CC50
    #COMPILE EXE
    #DIM ALL
    
    %TRUE  = 1
    %FALSE = 0
    
    DECLARE FUNCTION funDLLfunction LIB "TestDll.dll" (BYREF a_strAray() AS STRING) AS LONG
    
    FUNCTION PBMAIN () AS LONG
    
      funConsoleApp()
      STDOUT "Press any key to exit"
      WAITKEY$
      '
    END FUNCTION
    
    FUNCTION funConsoleApp() AS LONG
    ' this is the console app
     DIM a_strArray() AS STRING
     REDIM a_strArray(1 TO 1) AS STRING
     a_strArray(1) = "Unpopulated"
     '
     IF ISTRUE funDLLfunction(a_strArray()) THEN
       STDOUT a_strArray(1)
     ELSE
       STDOUT "DLL errored somehow"
     END IF
     '
    END FUNCTION

    Code:
    ' code for the DLL in PB9
    #COMPILE DLL "TestDll.bas"
    #DIM ALL
    
    %USEMACROS = 1
    #INCLUDE "Win32API.inc"
    
    GLOBAL ghInstance AS DWORD
    
    
    '-------------------------------------------------------------------------------
    ' Main DLL entry point called by Windows...
    '
    FUNCTION LIBMAIN (BYVAL hInstance   AS LONG, _
                      BYVAL fwdReason   AS LONG, _
                      BYVAL lpvReserved AS LONG) AS LONG
    
        SELECT CASE fwdReason
    
        CASE %DLL_PROCESS_ATTACH
            'Indicates that the DLL is being loaded by another process (a DLL
            'or EXE is loading the DLL).  DLLs can use this opportunity to
            'initialize any instance or global data, such as arrays.
    
            ghInstance = hInstance
    
            FUNCTION = 1   'success!
    
            'FUNCTION = 0   'failure!  This will prevent the EXE from running.
    
        CASE %DLL_PROCESS_DETACH
            'Indicates that the DLL is being unloaded or detached from the
            'calling application.  DLLs can take this opportunity to clean
            'up all resources for all threads attached and known to the DLL.
    
            FUNCTION = 1   'success!
    
            'FUNCTION = 0   'failure!
    
        CASE %DLL_THREAD_ATTACH
            'Indicates that the DLL is being loaded by a new thread in the
            'calling application.  DLLs can use this opportunity to
            'initialize any thread local storage (TLS).
    
            FUNCTION = 1   'success!
    
            'FUNCTION = 0   'failure!
    
        CASE %DLL_THREAD_DETACH
            'Indicates that the thread is exiting cleanly.  If the DLL has
            'allocated any thread local storage, it should be released.
    
            FUNCTION = 1   'success!
    
            'FUNCTION = 0   'failure!
    
        END SELECT
    
    END FUNCTION
    
    FUNCTION funDLLfunction(BYREF a_strLocalArray() AS STRING) EXPORT AS LONG
      TRY
        REDIM a_strLocalArray(1 TO 10) AS STRING
        a_strLocalArray(1) = "first element"
        FUNCTION = %TRUE
      CATCH
        FUNCTION = %FALSE
      FINALLY
      END TRY
    
    
    END FUNCTION

    Comment


    • #3
      >call the array from a PBasic Console programme

      If "PBasic Console" is not a new short form for "PowerBASIC Console Compiler" ......

      You can't share PowerBASIC Arrays [capital 'A' intentional ] by reference with any procedure other than a procedure compiled by PowerBASIC

      PB Arrays are a proprietary construct understood only by PowerBASIC. You MAY use the same arrays with code modules compiled with either PB/Windows or PB/CC, as the same proprietary constructs are used by both compilers.

      When reading Windows' documentation, "array" [small 'a'] does not mean the same thing.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Many thanks for your help Graham. I am studying this carefully. I was evidently a fair bit off track - but at least I am moving now in a more productive direction. Thank you again.

        Comment

        Working...
        X