Hi,

I'm trying to add a drop down combo box for user selection in the main window of my application. The program reads USERS.txt, loads each line into a const char*[] and (hopefully) adds them to the combo box. The problem is my program displays Chinese characters instead of the content of the .txt file.

This is my first C++ and first Win32 project, any insight is helpful

const char* users[50];

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	hInst = hInstance; 

	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, 575, 700, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

        HWND hwndSelectUser = CreateWindow (TEXT("COMBOBOX"),
             TEXT("User:"),
             WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWN | WS_VSCROLL,
             200, 450, 150, 60,
             hWnd,
             (HMENU) ID_USERCOMBO,
             hInstance,
             NULL);
       int y = LoadUsers();
	
       for(int i = 0; i < y; i ++){			
          SendMessage(hwndSelectUser, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>                   ((LPCTSTR)users[i]));
	}
}

int LoadUsers(){
	string line;
	int counter = 0;
	ifstream userlistfile ("USERS.txt");
	if(userlistfile.is_open()){
		while(userlistfile.good()){
			getline (userlistfile, line);
			users[counter] = line.c_str();
			counter++;
		}
	}
	return counter;

}

-Ben

Dani AI

Generated

Two separate bugs are likely happening here. As suggested, storing line.c_str() into a fixed const char* array gives you dangling pointers (the std::string temporary goes away). In addition, the control is probably built as Unicode (the default in modern Visual Studio) while your code is passing narrow (ANSI) bytes — that mismatch makes the bytes be interpreted as wide characters and produces the “Chinese” glyphs you see.

Recommended, robust approach:

  • Keep ownership of each line (use a container like std::vector<std::wstring> or std::vector<std::string>).
  • If your project uses Unicode (typical), convert input bytes to std::wstring and call the wide API (SendMessageW / CB_ADDSTRING maps to wide). If the file is UTF-8 use CP_UTF8 with MultiByteToWideChar; if it’s encoded in the current ANSI code page use CP_ACP.

Example pattern (illustrative — not a drop-in from the original post):

std::vector<std::wstring> users;
std::string line;
std::ifstream f("USERS.txt");
while (std::getline(f, line)) {
    int n = MultiByteToWideChar(CP_UTF8, 0, line.c_str(), -1, nullptr, 0);
    std::wstring w(n, L'\0');
    MultiByteToWideChar(CP_UTF8, 0, line.c_str(), -1, &w[0], n);
    if (!w.empty() && w.back() == L'\0') w.pop_back();
    users.push_back(std::move(w));
}
for (const auto &w : users)
    SendMessageW(hwndSelectUser, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(w.c_str()));

Other practical notes:

  • If you prefer ANSI, switch the project Character Set to “Multi-Byte” or call the explicit A-suffixed APIs (CreateWindowA/SendMessageA) — but using Unicode is recommended.
  • Verify the file encoding (UTF-8 vs. ANSI vs. UTF-16) — the wrong assumption there will still garble text.
  • Using std::vector or std::wstring avoids manual allocations/freeing that warned about, and CB_ADDSTRING will copy the string into the control during the call, so the lifetime requirement is only during that call.

function LoadUsers() must allocate space for each of the character arrays in the users array. All it is currently doing is using the same std::string over and over again, each time destroying the value of the previous time.

Two fixes:
1. re-declare char* users[100] as vector<string> users.

or
2. replace line 43 >>users[counter] = line.c_str()
with this
users[counter] = strdup(line.c_str());

strdup() does two things:
users[counter] = malloc(length+1);
strcpy(users[counter], line.c_str());

If you opt for option 2 then you need to free() each of those strings when finished with them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.