The function is returning the address of the local TCHAR array variable Text, which is the struct TVITEMEX tvi's pszText member is set to point to.
When the function returns the variable Text goes out of scope as the call stack is popped.
Under some circumstances you can get at the data after you pop the stack, but only if the compiler hasn't reused it for another purpose. As several calls are going on immediately after each other here, the memory is being reused here.
Returning _T("Testing"); is ok as that is a string literal, which have static storage.
I was using a wchar_t earlier. It really made no difference. According to Microsoft Visual C++ 2010, typedef WCHAR TCHAR, so they are both the same thing.
It depends on your compiler settings.
Whether or not the UNICODE symbol is defined determines how TCHAR is defined. It can be defined either as a char or a wchar_t.
Your particular setting has it as a wchar_t, so converting between them will work. But doing that is a bad idea because if that setting changes, your program will break.
Personally, I find the TCHAR fiasco to be a mess and avoid it completely. When working with WinAPI I prefer to just use wchar_ts for everything (then just use the accompanying 'W' forms of WinAPI functions... like MessageBoxW(), etc)
Thanks guys, I've made my code consistent and will continue to do so as a good programming habit.
Is there a redundant call to MessageBox at the end of TreeviewGetName?
Yeah, just to test if I was getting the correct input inside the function and outside. The first code I provided had it and it was giving the right values in the function but bad outside of it. I use it for debugging.