PlanetSquires Forums

Support Forums => General Board => Topic started by: jermy on May 02, 2016, 05:16:14 PM

Title: fixed buffer problem
Post by: jermy on May 02, 2016, 05:16:14 PM
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

Title: Re: fixed buffer problem
Post by: Paul Squires on May 02, 2016, 05:41:34 PM
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.

Title: Re: fixed buffer problem
Post by: jermy on May 02, 2016, 06:11:10 PM
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


Title: Re: fixed buffer problem
Post by: Paul Squires on May 02, 2016, 06:43:47 PM
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.
Title: Re: fixed buffer problem
Post by: José Roca on May 02, 2016, 06:55:52 PM
And pass *Buffer, not @Buffer.