Announcement

Collapse
No announcement yet.

ReDim At

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

  • Daniel Corbier
    replied
    I need to have an array that can be accessed both by VB and the DLL. This array is dimensioned in VB. An initializing routine will pass the base address of this array to the DLL, so that the DLL can do its thing to the array, and VB will later be able to directly access the values of the array. There are several functions in the DLL which will need to access this array. But the array address will not be submitted with each function call. So the array which is to be set once and for all is made global in the DLL.

    The following code is removed from its original context, so it may not look like anything useful, but it's enough for you to duplicate the error.

    The VB module declare statements look like this:
    Code:
    Declare Sub DoSomething Lib "test.dll" ()
    Declare Sub SetAddress Lib "test.dll" (ByRef ArrayAddress As Long)
    The program in the VB form looks like this:
    Code:
    Dim MyArray(10) As Long
    
    Private Sub Command1_Click()
        SetAddress MyArray(0)
        Call DoSomething
    End Sub
    The PowerBASIC code looks like this:
    Code:
    $Compile Dll "test.dll"
     
    Global MyArray() As Long
    
    Function LibMain(ByVal hInstance   As Long, _
                     ByVal fwdReason   As Long, _
                     ByVal lpvReserved As Long) Export As Long
        If fwdReason = 1 Then
            LibMain = 1
            ReDim MyArray(10) As Long
        End If
    End Function
    
    Sub SetAddress Alias "SetAddress" (ByRef ArrayBase As Long) Export
        ReDim MyArray(10) As Long At ArrayBase
        ' The above line does not crash if I use Dim instead of ReDim
    End Sub
    
    Sub DoSomething Alias "DoSomething" Export
        MyArray(1) = 0
    End Sub
    If I use Dim instead of ReDim in the SetAddress routine, it works. Now if I remove the ReDim line that's in LibMain, then it crashes either way. I'm using VB 5 and PB 5 for this.

    As for pointers, they work great in the context in which they're used within in my component, but that does not negate the usefulness of being able to dim a variable at a location. For instance, I can't do "For @x = 1 To 10" since @x isn't considered a scalar variable. Although I don't have an immediate need for this in my current project, I think it can come in handy in other situatios.

    ------------------
    Daniel Corbier
    UCalc Fast Math Parser
    http://www.ucalc.com

    Leave a comment:


  • Lance Edmonds
    replied
    I would suspect your GPF errors were because the address (plus the block of memory adjacent to that address) was not allocated to your process...

    However, the compiler *may* treat REDIM like a DIM statement if it did not encounter a prior DIM statement - it depends on your code, which I would need to see to be able to comment upon. Obviously this is commercial code, so if you can either post a cut-down version or email the code that fails to tech support (along with any support files, etc).

    I personally don't see any need to be able to DIM a scalar at a given address - that is what pointers are for, and you have the advantage of moving the pointer around - this would be more complex with a Scalar.

    For example, if you have a double precision located at address dPtr???, then you access it like this:
    Code:
    DIM DblPtr AS DOUBLE PTR
    DblPtr = xPtr??? ' xPtr??? is a DWORD containing the address of the double.
    INCR @DblPtr ' Increment the double precision variable stored at xPtr???

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Daniel Corbier
    started a topic ReDim At

    ReDim At

    I had a few lines similar to:

    ReDim MyArray(123) As Long At MyAddress

    I was getting GPF errors until I changed ReDim to
    Dim. This may or may not be what was causing the
    problem, as I was changing other things too. But
    I wanted to check the help file to find out for
    sure if REDIMing an array At a location is allowed.
    When I compile, it does not stop to flag this line
    as an error, however, the help file does not specifically
    mention the "AT" clause under REDIM. So is this
    allowed? If so, can you also REDIM PRESERVE at a
    location as well?

    Also, I saw another message which said you couldn't
    DIM scalar variables AT a location. Would you guys
    at PB consider allowing this in a future version? I
    know you can do something like DIM MyVar(0) At MyAddress,
    but it seems a little less elegant than simply DIM MyVar at MyAddress.

    ------------------
    Daniel Corbier
    UCalc Fast Math Parser
    http://www.ucalc.com
Working...
X