Sending SCI_GETTEXT (and others) crashes the application

478 views Asked by At

I am trying to use the Scintilla editor in my RealStudio application. I'm able to load the DLL, create the Scintilla child window and send messages to the window as long as the message parameters do not refer to a string. If I try to get or set a string Windows immediately terminates the application with error code 5 (access denied).

Here's my code, from the Open event of the containing window:

  #If TargetWin32 Then
    Declare Function CreateWindowExA Lib "User32" (ExStyle As Integer, ClassName As CString, WindowName As CString, Style As Integer, X As Integer, Y As Integer, Width As Integer, Height As Integer, Parent As Integer, Menu As Integer, Instance As Integer, Param As Ptr) As Integer
    Declare Function SendMessageA Lib "User32" (HWND As Integer, Message As UInt32, WParam As Ptr, LParam As Ptr) As Integer
    Const SCI_GETLENGTH = 2006
    Const SCI_GETTEXT = 2182
    Const WS_CHILD = &h40000000
    Const WS_CLIPCHILDREN = &h02000000
    Const WS_TABSTOP = &h00010000
    Const WS_VISIBLE = &h10000000


    ' IsFunctionAvailable calls LoadLibrary and GetProcAddress
    If System.IsFunctionAvailable("Scintilla_DirectFunction", "SciLexer") Then
      ' Create the Scintilla child window
      Dim SciHandle As Integer = CreateWindowExA(0, "Scintilla", "", WS_CHILD Or WS_CLIPCHILDREN Or WS_TABSTOP Or WS_VISIBLE, 5, 5, Me.Width - 10, Me.Height - 10, Me.Handle, 0, 0, Nil)

      Dim count, buffer As MemoryBlock
      ' Get the current character count
      Dim c As  Integer = SendMessageA(SciHandle, SCI_GETLENGTH, Nil, Nil) ' This works

      count = New MemoryBlock(4) ' Store the count in a MemoryBlock
      count.Int32Value(0) = c + 1
      buffer = New MemoryBlock(c + 1) ' allocate the buffer
      Call SendMessageA(SciHandle, SCI_GETTEXT, count, buffer) ' This crashes
    End If
  #endif

For the life of me I can't figure out why it's not working.

1

There are 1 answers

1
Thomas Tempelmann On BEST ANSWER

The SCI_GETTEXT is declared as follows:

SCI_GETTEXT(int length, char *text)

This means: You pass a length parameter as an integer, and a pointer to a text buffer.

Your code, however, passes a pointer to an integer for the length. Try this instead:

Declare Function SendMessageAip Lib "User32" (HWND As Integer, Message As UInt32, WParam As Integer, LParam As Ptr) As Integer
buffer = New MemoryBlock(c + 1) ' allocate the buffer
Call SendMessageAip(SciHandle, SCI_GETTEXT, c+1, buffer)