fixed buffer problem

New to FreeBASIC? Post your questions here.
Jermy
Posts: 5
Joined: Apr 12, 2016 10:29

fixed buffer problem

Postby Jermy » Feb 22, 2018 17:03

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

Code: Select all

'--------------------------------------------------------------------------------
Function FORM1_COMMAND1_BN_CLICKED ( _
                                   ControlIndex     as Long, _      ' index in Control Array
                                   hWndForm         as HWnd, _      ' handle of Form
                                   hWndControl      as HWnd, _      ' handle of Control
                                   idButtonControl  as Long   _     ' identifier of button
                                   ) as Long

Dim Hnotepad as HANDLE, TextHandle as HANDLE, result as HANDLE, temp as String 
 
      '    Hnotepad = FindWindow(Null,"Naamloos - Kladblok")     
          Hnotepad = FindWindow(Null,"Untitled - Notepad")
                   
 If Hnotepad = Null Then
 Print "cannot find 'Untitled - Notepad', its running ? "
 Exit Function
 End If         

TextHandle = FindWindowEx(Hnotepad, result, "Edit", Null)

 
   If TextHandle > Null Then

Temp = " but a portion of this sentence is sent to Notepad "   ' 51 charters long

'Dim Buffer as String
'Dim Buffer as String * Len((Temp)) 'error 11: Expected constant in 'Dim Buffer as String * Len((Temp))
'Dim Buffer as String * 51   ' Fixed size works, i need dynamic size
Dim Buffer as String * SizeOf(Len(Temp))  'incorrect result, just a part   
'Dim Buffer as String = Space(Len(Temp))  'incorrect result, unreadable characters in notepad

Buffer = Temp

SendMessage(TextHandle, WM_SETTEXT, 0,  Cast(LPARAM, @Buffer))' correct result with fixed size. 


Print "Message Send : " & Buffer

End If         


   Function = 0   ' change according to your needs
End Function



I' tried several times but I can't see to make it work.
fxm
Posts: 8057
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: fixed buffer problem

Postby fxm » Feb 22, 2018 17:46

Try:
.....
Dim Buffer as String = Temp
.....
SendMessage(TextHandle, WM_SETTEXT, 0, Cast(LPARAM, StrPtr(Buffer)))
.....
Jermy
Posts: 5
Joined: Apr 12, 2016 10:29

Re: fixed buffer problem

Postby Jermy » Feb 22, 2018 18:15

Thanks fxm, that seems to do it.
fxm
Posts: 8057
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: fixed buffer problem

Postby fxm » Feb 22, 2018 19:33

Welcome to the forum!

A (variable-length) string (Dim As String Buffer) has a descriptor that contains the address of the string character data, the number of characters used, and the number of characters available (because the allocated memory is sized dynamically):
- @Buffer provides the address of the descriptor (that contains the address of the string characters data).
- StrPtr(Buffer) provides directly the address of the string character data.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests