ProtectedVoid
ProtectedVoid

Reputation: 1315

Winapi ListView_GetItemText wrong output format

I'm using ListView_GetItemText this way:

int count = ListView_GetItemCount(procmon_lv); //Get Items count
wchar_t buffer[2048]; //Init buffer
ListView_GetItemText(procmon_lv, count-1, 0, buffer, 2048); //Call function
LPWSTR itemtxt = buffer; //Create LPWSTR var

stringstream s;
s << itemtxt;
MessageBoxA(NULL, s.str().c_str(), NULL, NULL);

Sleep(7000); //Sleep because this piece of code is inside a While loop

MessageBox function is showing this:

enter image description here

What I need is to get the row's entire text, but It seems I'm getting an hex string...

Upvotes: 0

Views: 391

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

std::stringstream treats a wchar_t* pointer as a general pointer and thus stores the value of the pointer, instead of the characters that it points at.

If you want to use MessageBoxA, you need to convert the wchar_t data to ANSI.

int count = ListView_GetItemCount(procmon_lv); //Get Items count
wchar_t buffer[2048] = {0}; //Init buffer
char buffer_ansi[2048 * 2] = {0};
ListView_GetItemText(procmon_lv, count-1, 0, buffer, 2048); //Call function
WideCharToMultiByte(CP_ACP, 0, buffer, -1, buffer_ansi, sizeof(buffer_ansi), NULL, NULL);

stringstream s;
s << buffer_ansi;
MessageBoxA(NULL, s.str().c_str(), NULL, 0);

Update: you shouldn't use NULL for the forth argument of MessageBoxA, which isn't a pointer.


Update 2: instead of converting the string to ANSI, using std::wstringstream instead of std::stringstream, and call MessageBoxW() instead of MessageBoxA().

int count = ListView_GetItemCount(procmon_lv); //Get Items count
wchar_t buffer[2048] = {0}; //Init buffer
ListView_GetItemText(procmon_lv, count-1, 0, buffer, 2048); //Call function

std::wstringstream s;
s << buffer;
MessageBoxW(NULL, s.str().c_str(), NULL, 0);

Note: you have the comment "Init buffer", so initialize the buffer.


Update 3: Or, just don't use a std::wstringstream at all.

int count = ListView_GetItemCount(procmon_lv); //Get Items count
wchar_t buffer[2048] = {0}; //Init buffer
ListView_GetItemText(procmon_lv, count-1, 0, buffer, 2048); //Call function

MessageBoxW(NULL, buffer, NULL, 0);

Upvotes: 1

Related Questions