Pergunta

i was trying to extract text from an EDIT control and display it in a MessageBox in win32, the code compiles and runs but whenever i click the button to do the task, the program stops working and crashes, i tried to trace it on my own and somehow figured out that the MessageBoxEx made the crash, here is the fragment of the code,

case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case IDB_BTN1:
                    {
                        LPTSTR str
                        Edit_GetText(hEdit1,str,255);
                        MessageBoxEx(hwnd,str,"INPUT",MB_ICONINFORMATION,0);
                        break;
                    }
            }

            return 0;
        }

how do i fix this?

Foi útil?

Solução 2

The first failure occurs at Edit_GetText. You pass it an uninitialized pointer. You then pass on that same uninitialized pointer to MessageBoxEx.

Change your code to allocate a buffer:

TCHAR str[255];
Edit_GetText(hEdit1, str, 255);
MessageBoxEx(hwnd, str, _T("INPUT"), MB_ICONINFORMATION, 0);

If you had checked the return value of Edit_GetText for errors you would have learnt that something was wrong there. Always check return values for errors with the Windows API.

Outras dicas

You need to allocate a buffer to receive the text:

case WM_COMMAND:
{
    switch(LOWORD(wParam))
    {
        case IDB_BTN1:
        {
            TCHAR szBuffer[255];
            Edit_GetText(hEdit1,szBuffer,255);
            MessageBoxEx(hwnd,szBuffer,"INPUT",MB_ICONINFORMATION,0);
            break;
        }
    }

    return 0;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top