Hi friends,
Please am creating application and l want to get user input and checking if the user input buffer is empty or not. Code snippet below;
1 2 3 4 5 6 7 8 9 10 11
TCHAR userName[100];//user input buffer
hwndedit=GetDlgItem(hwnd,IDC_UNICALPTUME_SUBJECT_TBOX);//gets edit control's handle
userName[100]=(TCHAR) SendMessage(hwndedit,WM_GETTEXT,(WPARAM)sizeof(userName)/sizeof(userName[0]),(LPARAM)userName);//Sent message
return (INT_PTR)TRUE;
case IDOK://checks if buffer is empty or not when the user clicks OK on the dialog
if(userName!=NULL)
EndDialog(hwnd,TRUE);
return (INT_PTR)TRUE;
Please can someone guide me on how l can get user input and checking if the buffer is empty or not.
Thanks everyone in advance
@Freddie1: Thanks but l first did it before using the sendmessage type, it didn't work. But now, how can l check if something is in the buffer or not. I had done
if(userName!=NULL)
EndDialog(hwnd, TRUE)
return (INT_PTR)TRUE;
Still it doesn't work.
Thanks.
GetWindowText returns the number of chars copied to the buffer. If nothing is it it should be 0.
Alternatively you can use strlen to check the length of userName.
When using the Win Api realize that the concept of OUT parameters is very important. By that, I mean you provide the address of a valid buffer to a function, as is your userName[] buffer above. When the call returns, if the call succeeded (and the return value often indicates that), the buffer will contain valid data. Let me point out a possible flaw in your code above.
Assume you declare an auto/local stack based variable such as your userName[] character array above. C/C++ doesn't auto-initialize variables to zero or NULLs, so userName[] could contain memory garbage. Its state is undefined. If a function call fails then userName[] will be left untouched. It might still retain garbage values. If a strlen() call is made on it some positive number could possibly be returned. Moral of the story - do a memset() on your buffer before passing it to a function. That way, if the function either fails, or succeeds but returns nothing in the buffer (perhaps in your example the user entered nothing in the text box), then your buffer can be guaranteed to contain nothing but nulls, and a strlen() call made on it will be guaranteed to return 0 for length.