# How to do new-lines using SetDlgItemText() in WinAPI?

This topic is 2900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, basically, I've got a patcher/installer that I've got near completed. Fully functional, etc. Right now I'm working on fine points. I have an "Edit" class child window in the center of my application, like most installers that displays info as it downloads, patches, etc.

Unfortunately, the text totally runs together. How can I make the Edit window format new-line characters?

This is my current code for displaying:

void WriteInTextBox(LPSTR stringToWrite){	int maxBufferSize = SendMessage(hInfoBox, EM_GETLIMITTEXT, 0, 0);	LPSTR buffer = new char[maxBufferSize];	GetDlgItemTextA(hPatcher, H_INFO_BOX, buffer, maxBufferSize);	strcat(buffer, stringToWrite);	strcat(buffer, "\n");	SetDlgItemTextA(hPatcher, H_INFO_BOX,buffer);	SendMessage(hInfoBox, EM_LINESCROLL, 0, (SendMessage(hInfoBox, EM_GETLINECOUNT, 0, 0)));	return;}

Note: I've tried a few different approaches, none seem to work. Any help, guidance or pointers in the right direction would be GREATLY appreciated. Thanks.

Oh, and by the way, everything's using the "A" versions because Wide-character compatible functions tended to ignore formatting anyway... guess this extra step was pointless as it's not formatting them...

Thank you again!

##### Share on other sites
Um....

strcat(buffer, "\n");
strcat(buffer, stringToWrite);

strcat(buffer, stringToWrite);
strcat(buffer, "\n");

??

int maxBufferSize = SendMessage(hInfoBox, EM_GETLIMITTEXT, 0, 0);
LPSTR buffer = new char[maxBufferSize];

The size seems unsafe. Strcat doesn't care about the buffer length, so (maybe) there's a possibility of buffer overflow.

You should calculate the remaining character count and use strncat instead:
void WriteInTextBox(LPSTR stringToWrite){	int maxBufferSize = SendMessage(hInfoBox, EM_GETLIMITTEXT, 0, 0);	LPSTR buffer = new char[maxBufferSize];	int n;	GetDlgItemTextA(hPatcher, H_INFO_BOX, buffer, maxBufferSize);	n = maxBufferSize - strlen(buffer) - 2;	if( n > 0 )	{		strcat(buffer, "\n");		strncat(buffer, stringToWrite, n);	}	SetDlgItemTextA(hPatcher, H_INFO_BOX,buffer);	SendMessage(hInfoBox, EM_LINESCROLL, 0, (SendMessage(hInfoBox, EM_GETLINECOUNT, 0, 0)));	return;}
Not tested.

##### Share on other sites
Haha, I don't mean before it posts the stringToWrite variable that one time.

I'm saying, if I do this:

WriteInTextBox("Say This");
WriteInTextBox("Say That");
WriteInTextBox("Then This Again");

even with the strcat() adding in the "\n" character, it outputs:

Say ThisSay ThatThen This Again instead of line breaks between the calls

##### Share on other sites
Hmm, doesn't SetDlgItemText want an "\n\r" newline sequence? Or perhaps "\r\n". It's been a while.

##### Share on other sites
What happens if you comment out this:
SendMessage(hInfoBox, EM_LINESCROLL, 0, (SendMessage(hInfoBox, EM_GETLINECOUNT, 0, 0)));
?

##### Share on other sites
Message boxes only need '\n' for newline. Maybe dialogs are different?

##### Share on other sites
Quote:
 Original post by szecsint maxBufferSize = SendMessage(hInfoBox, EM_GETLIMITTEXT, 0, 0);LPSTR buffer = new char[maxBufferSize];The size seems unsafe. Strcat doesn't care about the buffer length, so (maybe) there's a possibility of buffer overflow.

StrCat doesn't, but SetDlgItemText() cares. It needs the size of the text, but since I'm installing dozens (hundreds?) of files to unknown directories, etc. I can't dictate the length.

PLUS, the buffer is meant to contain the entire Edit child-window contents. So, it only makes sense (in my opinion, at least) to have the buffer size equal to the max about of characters allowed within the edit window. Does this make sense?

##### Share on other sites
Quote:
 Original post by szecsWhat happens if you comment out this:SendMessage(hInfoBox, EM_LINESCROLL, 0, (SendMessage(hInfoBox, EM_GETLINECOUNT, 0, 0)));?

If I edit that out, then the text box stays at the first line instead of scrolling down automatically with the displayed text. ^_^

EM_LINESCROLL = Windows Message to make an Edit Window scroll.
wParam = horizontal scroll.
lParam = vertical Scroll.

SendMessage(hInfoBox, EM_GETLINECOUNT, 0, 0) returns the number of lines in an edit window with multi-lines.

So that statement tells my textbox to scroll down to the last line of text, automatically.

##### Share on other sites
Quote:
Original post by DotNetDev
Quote:
 Original post by szecsint maxBufferSize = SendMessage(hInfoBox, EM_GETLIMITTEXT, 0, 0);LPSTR buffer = new char[maxBufferSize];The size seems unsafe. Strcat doesn't care about the buffer length, so (maybe) there's a possibility of buffer overflow.

StrCat doesn't, but SetDlgItemText() cares. It needs the size of the text, but since I'm installing dozens (hundreds?) of files to unknown directories, etc. I can't dictate the length.

PLUS, the buffer is meant to contain the entire Edit child-window contents. So, it only makes sense (in my opinion, at least) to have the buffer size equal to the max about of characters allowed within the edit window. Does this make sense?

the local buffer[maxBufferSize] can overflow.

what happens, if maxBufferSize is say 100, and the text that already in the buffer is say 90 long? And you are trying to append a 30 long string to it?

SetDlgItemText() can't overflow (according to you), but buffer will.

##### Share on other sites
Quote:
 Original post by SneftelHmm, doesn't SetDlgItemText want an "\n\r" newline sequence? Or perhaps "\r\n". It's been a while.

Will check....

"\r\n" it is!!!!

Thank you!

But, you would really go above and beyond the call of duty if you could explain to me WHY it needs both of these escape-sequences? ^_^

• 39
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631367
• Total Posts
2999592
×