fixed buffer problem

Started by jermy, May 02, 2016, 05:16:14 PM

Previous topic - Next topic

jermy

when I try to create a buffer, I cannot make it variable sized.


'Dim Buffer as String * 1024  ' this is oke

Dim i as Long
    i = 1024
Dim Buffer as String * i   ' this is not allowed



I' tried several times but I can't see to make it work


Paul Squires

To create a variable size string buffer you would set the dynamic string to whatever number of blank spaces that you need.

Dim i As Long = 1024
Dim Buffer As String = Space(i)

You can not use the Dim Buffer As String * i because fixed length strings need to be defined at compile time rather than at runtime.

Paul Squires
PlanetSquires Software

jermy

the program complies but crashes when i use this function below, so i cant use 'Dim Buffer As String = Space(i)'
translated from vb6

Public Function Read_(ByVal Bytes as Long = -1) as String

   On Error Goto ErrHandler

    Sleep(500)
    Dim tBytesR as Long 
    Dim tBytesA as Long
    Dim tMsg as Long
    Dim i as Long, Result as Long
    Dim ReturnStr as String
    If Not mvarRunning Then Exit Function
   


       Result = PeekNamedPipe(pipeOut.hReadPipe, ByVal 0, ByVal 0, ByVal 0, @tBytesA, ByVal 0)
    If Result <> 0 And tBytesA > 0 Then

'  Dim Buffer as String * 1024
Dim Buffer as String = Space(tBytesA) ' crashes the program


           Result = ReadFile(pipeOut.hReadPipe, @Buffer ,IIf(Bytes = -1, SizeOf(Buffer), Bytes), @tBytesR, ByVal 0&)
        If Result  = 0 Then   
           MessageBox (0,GetApiErrorInfo(GetLastError) & " ReadFile failed.", "GetLastError result",0)
          Goto ErrHandler
        End If
   
        ReturnStr = Left$(Buffer, tBytesR)
        Read_ = DOSDecode(ReturnStr)
    Else
       'Function =
                 MessageBox (0,GetApiErrorInfo(GetLastError) & " PeekNamedPipe", "GetLastError result",0)
          Goto ErrHandler
   
   
    End If

   Exit Function
ErrHandler:
   Terminate 
'   Function = 0   ' change according to your needs   
End Function



Paul Squires

I am not 100% certain of the GPF, but because you are now using a variable size string variable you should not use SizeOf(Buffer) in your ReadFile function call. SizeOf will return the size of the variable string descriptor rather than the size of the string itself. You should probably use LEN() or simple tBytesA.
Paul Squires
PlanetSquires Software

José Roca

And pass *Buffer, not @Buffer.