How to calculate LPWSTR size when using GetDlgItemText () function

My query is small I use to get text from a text field, but I have a problem in the fourth parameter (for example, in the buffer). GetDlgItem()

LPWSTR txtbuff;
GetDlgItemText(hwnd, IDC_EDIT1, txtbuff, 50); // the fourth parameter (50)
MessageBox(NULL,txtbuff,L"Error message",MB_OK)

How can I calculate the size txtbuff

+5
source share
4 answers

You can use GetDlgItem to get the HWND control and GetWindowTextLength to determine how many characters your control holds.

+8
source

You need to allocate a buffer yourself:

WCHAR txtbuff[50];
GetDlgItemText(hwnd, IDC_EDIT1, txtbuff, 50);
/* or... */
GetDlgItemText(hwnd, IDC_EDIT1, txtbuff, sizeof(textbuff)/sizeof(textbuff[0]));
MessageBox(NULL, txtbuff, L"Error message", MB_OK);

LPWSTR - , , . , GetDlgItem.

0

You can use sizeoflike this:

TCHAR txtbuff[50];
GetDlgItemText(hwnd, IDC_EDIT1, txtbuff, sizeof(txtbuff) * sizeof(TCHAR));
0
source

This is not a buffer size. This is the amount of line you really want to copy. You can set it to any nonzero size you want theoretically. On the other hand, the size of your own buffer is a different story, just make sure you are not overflowing.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms645489%28v=vs.85%29.aspx

0
source

All Articles