Two things
#1 As you're already working with standard containers, I would use std::vector for the buffer rather than a raw pointer.
#2 If you're working with wchar_t's it's prob better to use SendMessageW explictly.
SendMessage is actually a macro which evaluates to either SendMessageW (if UNICODE is defined) or SendMessageA (if it isn't). If your Visual Studio project is configured to use the Unicode character set, then all should be well. But if this gets changed, all could go horribly wrong. So either:
A. Switch all Win32 calls to their explicit W form and use L
Easy :-)
B. Replace all wchar_t's with TCHAR's, wrap all strings and chars in the _T() macro, etc.
Painful. :-(
And it would be a bit pointless. The TCHAR mechanism was introduced so you could build Unicode for Windows NT and ANSI for Windows 95 (which was originally too stupid to understand Unicode, though they later provided a conversion layer.)
Almost all standard Win32 calls which take string have A and W variants. The exceptions are COM calls, which have their own rules, and a few others (e.g. GetProcAddress only has an ANSI form.)
(Also note there is _UNICODE as well as UNICODE to worry about. But _UNICODE effects the CRT TCHAR macros rather that the Win32 ones, which are controlled by UNICODE.)
So...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// Get the listbox selection
iListBoxSelection = (int)SendMessageW(hListBox, LB_GETCURSEL, 0, 0);
if(LB_ERR != iListBoxSelection) // assumes a single-select list box
{
int str_len = (int)SendMessageW(hListBox, LB_GETTEXTLEN, iListBoxSelection, 0);
// if it's not a read-only text box, you might want to check for zero length here?
// use vector as buffer
vector<wchar_t> buf(str_len + 1);
// Put the text from the listbox item into the buffer
SendMessageW(hListBox, LB_GETTEXT, iListBoxSelection, (LPARAM)&buf[0]);
if (!largeTextboxString.empty())
largeTextboxString += L" ";
largeTextboxString += &buf[0];
}
|
where I've assumed
1 2 3
|
HWND hListBox;
int iListBoxSelection;
wstring largeTextboxString;
|
and assumed the list box is single select to I can test against LB_ERR to check there's a selection.
I also simplified you if-else sequence a bit, and am using wstring::empty() to test for a blank string (or rather, here, to test that it's not blank.)
And note that when you pass a vector to a function that expects a buffer pointer, you need to use the form
&buf[0]
, i.e. get the address of the first element in the buffer. (For C++03, this is only legal for std::vector, as this is the only standard container that must provde contiguous storage. See PS for the C++11 position.)
Andy
PS The storage rules for std::string have changed for C++11 -- the storage must (now) be contiguous, as it happens to already be with most common implentations (there are other parts of the C++ standard which make it hard work to use non-contiguous storage...)
The cplusplus.com entry on std::string is unclear, so here's the en.cppreference.com entry's take:
The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of a CharT[] array. |
std::basic_string
http://en.cppreference.com/w/cpp/string/basic_string
Microsoft Layer for Unicode
http://en.wikipedia.org/wiki/Microsoft_Layer_for_Unicode