C, Win32 API: l need help getting user input

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
Last edited on
If you have the HWND of the edit control on the dialog, it would be a lot easier to just do this...

1
2
3
TCHAR userName[100];
hwndedit=GetDlgItem(hwnd,IDC_UNICALPTUME_SUBJECT_TBOX);
GetWindowText(hwndedit,100,(LPARAM)userName);


You can forget about that casting of the result of SendMessage to a TCHAR and that crazy assignment.
@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.
Ok, l understand. You mean l should check for the return value of GetWindowText
You all are the best. Thanks for the answers.
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.
Thanks freddie1, thomas1965 and everyone here.
Topic archived. No new replies allowed.