Search code examples
c++variableswinapibuttonmessagebox

Why am I getting hieroglyphic text in message box


I am creating a button on my Win32 Desktop App using C++ that when pressed, displays a message box that prints out the user input from a text box along with some additional text. However, the message box displays with hieroglyphic text and gives the error C6054: String 'buff' might not be zero-terminated. I'm not sure why the text is not displaying on the message box correctly.

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
    
#define ENTER_BUTTON                2345

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR heading[] = _T("CD ROM READER");
    TCHAR CSCI_No_Inst[] = _T("Please enter the CSCI No below:");

    switch (message)
    {
    case WM_CREATE:
    {
        TextBox = CreateWindow(TEXT("EDIT"), TEXT(""),
            WS_VISIBLE | WS_CHILD | WS_BORDER,
            150, 140, 250, 25,
            hWnd, NULL, NULL, NULL); 

        HWND hwndButton_Enter = CreateWindow(
            L"BUTTON", L"ENTER", 
            WS_VISIBLE | WS_CHILD | WS_BORDER,  //Styles
            405, 140, 70, 25,
            hWnd, (HMENU)ENTER_BUTTON, NULL, NULL);
      return 0;
    }

     case WM_COMMAND:
    {
        switch (LOWORD(wParam))
        {
         case ENTER_BUTTON:
              
            int gwtstat = 0;
            TCHAR title[] = _T("CSCI_NO");
            gwtstat = GetWindowText(TextBox, &CSCI_NO[0], 60);
            
            TCHAR buff[100];
            TCHAR name[] = _T("CSCI_NO");
            _tprintf(buff, L"The CSCI No you entered is: %s", &CSCI_NO[0]);
            MessageBox(hWnd, buff, title, MB_OK);
            break;
        }
    }
}

Solution

  • First you need to initialize the array buff, otherwise there will be uninitialized characters garbled when outputting.

    Then you should use swprintf instead of _tprintf (wprintf) to output the string to the buff array

    I modified the code as follows, and you can refer to it:

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
        TCHAR heading[] = _T("CD ROM READER");
        TCHAR CSCI_No_Inst[] = _T("Please enter the CSCI No below:");
        TCHAR CSCI_NO[60] = L"";
        switch (message)
        {
        case WM_CREATE:
        {
            TextBox = CreateWindow(TEXT("EDIT"), TEXT(""),
                WS_VISIBLE | WS_CHILD | WS_BORDER,
                150, 140, 250, 25,
                hWnd, NULL, NULL, NULL);
    
            HWND hwndButton_Enter = CreateWindow(
                L"BUTTON", L"ENTER",
                WS_VISIBLE | WS_CHILD | WS_BORDER,  //Styles
                405, 140, 70, 25,
                hWnd, (HMENU)ENTER_BUTTON, NULL, NULL);
            return 0;
        }
    
        case WM_COMMAND:
        {
            switch (LOWORD(wParam))
            {
            case ENTER_BUTTON:
    
                int gwtstat = 0;
                TCHAR title[] = _T("CSCI_NO");
                gwtstat = GetWindowText(TextBox, &CSCI_NO[0], 60);
    
                TCHAR buff[100] = L"";
                TCHAR name[] = _T("CSCI_NO");
                wsprintf(buff, L"The CSCI No you entered is: %s", CSCI_NO);
                MessageBox(hWnd, buff, title, MB_OK);
                break;
            }
            return 0;
        }
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    

    And it works for me:

    enter image description here