Getting text from an edit box

How can I access the text from edit box?
I create it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Global

HWND EditBox;
//WndProc
case WM_CREATE:
{
//buttons,editboxes...EditBox=CreateWindowEx(
	WS_EX_CLIENTEDGE,
	L"EDIT",
	L"",
	WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL|ES_PASSWORD,
	10,
	80,
	180,
	25,
	hWnd,
	(HMENU)IDC_PW,
	GetModuleHandle(NULL),
	NULL);
}

Now I try to get after a buttonpress:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case WM_COMMAND:
{
	switch(wParam)
	{
		case IDC_SUBMIT:
			char buffer[256];
			SendMessage(EditBox,
			WM_GETTEXT,
			sizeof(buffer)/sizeof(buffer[0]),
			reinterpret_cast<LPARAM>(buffer));
			MessageBox(NULL,
				(LPWSTR)buffer,
				"Information",
				MB_ICONINFORMATION);
			break;
	}
}


EDIT:Fixed that by making it global...
Now I still cannot read the content, the program crashes when I click the button...
Last edited on
Wow, didn't know about that function xD
Thanks!
Your original code crashes because you try to cast a char to LPWSTR. Never do that !.
Your original code crashes because you try to cast a char to LPWSTR. Never do that !.

True! Change from "MessageBox" to "MessageBoxA" or simply in your Project Settings disable Unicode Encoding.
WM_COMMAND for controls like Edit boxes are also handy because they carry the HWND in the lParam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
case WM_COMMAND:
{
  switch(LOWORD(wParam))
  {
    case IDC_SUBMIT:
    {
      char buffer[256] = {0};
      if(GetWindowText((HWND)lParam,buffer,256)) // will return 0 on failure
      {
        MessageBox(hwnd,buffer,"Information",MB_ICONINFORMATION);
      }
    }
  }
}
Topic archived. No new replies allowed.