Problems with memo.lines.add

I am trying to make a chat application that will post a message in a memo in a form similar to this:

USERNAME-> Message 

but it goes to my note as follows:

 USERNAME 

Here is my code:

 const cnMaxUserNameLen = 254; var sUserName: string; dwUserNameLen: DWORD; text : string; begin dwUserNameLen := cnMaxUserNameLen - 1; SetLength(sUserName, cnMaxUserNameLen); GetUserName(PChar(sUserName), dwUserNameLen); SetLength(sUserName, dwUserNameLen); text:= sUserName + '-> ' + edit1.Text; memo1.Lines.Add(text); 

Any suggestions for fixing it?

+4
source share
1 answer

The value returned in dwUserNameLen includes a null terminator . And so you include it in text . When a string is sent to the Windows editing control behind TMemo , the string is passed as a zero-terminated string. And so the wandering zero on behalf of the user completes the data transfer.

Change the code as follows:

 SetLength(sUserName, dwUserNameLen-1); 

You should also check the return value of GetUserName in case of an error, but I will leave you this information.

+8
source

Source: https://habr.com/ru/post/1382375/


All Articles