Why? lazarus / 34-Windows ---------------------------------------------------------------------- typo · 2013-05-10 11:20 Could someone explain me what is going on here: Code: [Select]    SendMessage(lzRichEdit.Handle, EM_GETSELTEXT, 0, LongInt(PChar((Result))));     showmessage(Result);        // returns selected text string     showmessage(Result + 'a');  // returns "a" ---------------------------------------------------------------------- Martin_fr · 2013-05-10 11:51 I am not 100% sure, but I take a ***guess***: Code: [Select]LongInt(PChar((Result)))Result contains a memory address. The address is either nil, or pointing at something like (in32 bit): Addr - 8: ref count addr -4: len addr: text + #0 http://msdn.microsoft.com/en- gb/library/windows/desktop/bb774190%28v=vs.85%29.aspx QuotewParam    This parameter is not used; it must be zero. lParam    Pointer to a buffer that receives the selected text. The calling application must ensure that the buffer is large enough to hold the selected text. So I assume Result does contain some string before. because if not, you pass nil as lparam, and that would not fit the above requirement. Then the memory at addr will be changed to: addr: newtext + #0 but no ref count, no len are touched. So your string is not a valid string anymore (and had a lot of other potential site effects, like overwriting random memory after the string, or altering other strings (if ref count > 1)) showmessage probably internally handles it as pchar (does not use the len). Result + 'a' looks at the ref count and len. And it dis-behaves. ---------------------- If you do   string := somepchar; fpc adds the conversion. But if you typecast a string to a function param, it seems that is not added. ---------------------------------------------------------------------- typo · 2013-05-10 13:57 So what should I do in order to "extract" the string from Result? ---------------------------------------------------------------------- Leledumbo · 2013-05-10 14:41 Typecast Result to PChar and extract using StrPas. ---------------------------------------------------------------------- typo · 2013-05-10 14:50 Code: [Select]    SendMessage(lzrichedit.Handle, EM_GETSELTEXT, 0, LongInt(PChar(Result)));     result := strpas(pchar(result));     ShowMessage(IntToStr(Length(Result)));  // returns 1     ShowMessage(Result);                    // returns first letter     ShowMessage(Result + 'a'); ---------------------------------------------------------------------- Leledumbo · 2013-05-10 15:18 Err... shouldn't you be using another variable to store result of strpas?  I don't know the internal implementation, but it might overwrite result. ---------------------------------------------------------------------- typo · 2013-05-10 15:25 Code: [Select]var   p :PChar; begin   SendMessage(re.Handle, EM_GETSELTEXT, 0, LongInt(PChar(Result)));   p := pchar(result);   Result := StrPas(p);   ShowMessage(IntToStr(Length(Result)));  // returns 1   ShowMessage(Result);                    // returns first letter   ShowMessage(Result + 'a'); end;  No change. ---------------------------------------------------------------------- Martin_fr · 2013-05-10 15:34 Use an array of byte, or an array of char. You must set it's length before calling the function QuotelParam    Pointer to a buffer that receives the selected text. The calling application must ensure that the buffer is large enough to hold the selected text. ---------------------------------------------------------------------- Jurassic Pork · 2013-05-10 16:13 hello, it seems that the returned string is a wideString ? ??? try this : Code: [Select]  MyResult : Widestring;   CharRange: TCharRange;   nbCar,TheLength : Integer; begin   // number of selected char   SendMessage(lzrichedit1.Handle, EM_EXGETSEL, 0, Longint(@CharRange));   NbCar := CharRange.cpMax - CharRange.cpMin;   SetLength(MyResult, NbCar + 1);   TheLength := SendMessage(lzrichedit1.Handle, EM_GETSELTEXT, 0, LongInt(PChar(MyResult)));   // adjust String Length   SetLength(MyResult, TheLength );   ShowMessage(IntToStr(Length(MyResult)));   ShowMessage(MyResult);                      ShowMessage(MyResult + 'a');  // easy way :-)   ShowMessage(lzrichedit1.SelText); Friendly , J.P ---------------------------------------------------------------------- ChrisF · 2013-05-10 16:23 Code: [Select]  SetLength(result,256);             <-----------   SendMessage(lzRichEdit.Handle, EM_GETSELTEXT, 0, LongInt(PChar((Result))));   showmessage(Result);        // returns selected text string   showmessage(Result + 'a');  // returns "a" Anyway, it's highly risky to use it like this. ---------------------------------------------------------------------- typo · 2013-05-10 16:41 Code: [Select]var   TheLength: integer;   NbCar: Integer;   CharRange: TCharRange; begin   // number of selected char   SendMessage(lzrichedit.Handle, EM_EXGETSEL, 0, Longint(@CharRange));   NbCar := CharRange.cpMax - CharRange.cpMin;   //SetLength(Result, NbCar + 1);   SetLength(Result, 256);   TheLength := SendMessage(lzrichedit.Handle, EM_GETSELTEXT, 0, LongInt(PChar(Result)));   ShowMessage(IntToStr(TheLength));  // returns correct length   // adjust String Length   SetLength(Result, TheLength );   ShowMessage(Result);                  // returns first letter   ShowMessage(Result + 'a');            // returns first letter end; ---------------------------------------------------------------------- Jurassic Pork · 2013-05-10 16:45 result is a wideString ? ---------------------------------------------------------------------- typo · 2013-05-10 17:01 I have just changed Result to a widestring and this worked. Thanks.