ListView Selection help

I'm trying to get the text of a selected item in a listview control.


HWND hFileList = GetDlgItem(MainWindow->hDlg1, IDC_LIST3);
LPTSTR szText;

int i = ListView_GetSelectionMark (hFileList);

if (i == -1){
MessageBox(0, "no selction you twat!", "blabla", MB_OK);
}
else{
ListView_GetItemText (hFileList, i, 0, szText, sizeof(szText));
MessageBox(0, szText, "blabla", MB_OK);
}


The index is returned correctly, but szText is just some garbage string...

Any idea why the text of the slected item isnt output instead of garbage?

Cheers.
[663 byte] By [gbrooks3] at [2007-11-20 11:31:00]
# 1 Re: ListView Selection help
You need to specify a string buffer for szText, not a pointer.

TCHAR szText[ MAX_PATH ];

not

LPTSTR szText;
Arjay at 2007-11-10 22:25:11 >
# 2 Re: ListView Selection help
What is szText and what is szText2 ? How are they both declared ?
kirants at 2007-11-10 22:26:15 >
# 3 Re: ListView Selection help
Arjay,

Tried that, still some garbage string is output to the MessageBox :(

Kirants,

Oops! Still the same error however :(
gbrooks3 at 2007-11-10 22:27:14 >
# 4 Re: ListView Selection help
Well, you still haven't answered what is szText2 declared as? That is what you are using for size parameter to. Should it not be szText instead ?
kirants at 2007-11-10 22:28:14 >
# 5 Re: ListView Selection help
exactly, it was a mistake. I actually use szText.

But still the same issue :(
gbrooks3 at 2007-11-10 22:29:24 >
# 6 Re: ListView Selection help
Issue ( probably not the reason why you are seeing the garbage ): The last parameter to GetItemText is supposed to be the no. of characters and not no. of bytes. sizeof() always gives no. of bytes. You can resolve it by passing sizeof(szText)/sizeof(szText[0])
Question. Are you by any chance trying to read this from a list control that is in another application ?
kirants at 2007-11-10 22:30:23 >
# 7 Re: ListView Selection help
Unfortunately that doesnt solve the issue :(

No, the ListView is in a dialog based application. I am able to Add items to it perfectly.

Thanks for the help :)
gbrooks3 at 2007-11-10 22:31:26 >
# 8 Re: ListView Selection help
What compiler are you using? VC6, VC7.1?

Are you compiling in ANSI or UNICODE?

If compiling in UNICODE on VC6, and are looking at the string in the debugger, you'll need to append a ,su in the watch window in order for a unicode string to show up.

ie.
szText,su
Arjay at 2007-11-10 22:32:23 >
# 9 Re: ListView Selection help
Using Orcas (VS2008) :)
gbrooks3 at 2007-11-10 22:33:20 >
# 10 Re: ListView Selection help
Could you show your actual code without previous "mistakes"?
VictorN at 2007-11-10 22:34:21 >