if (SHGetPathFromIDList (pitemidl, path)) result = path;
This line is your problem. result
isn't a class like std::string
, it's simply a pointer to a buffer of one or more TCHAR
. Assigning it like that simply changes the pointer to point to path
, it doesn't copy path
into the buffer that result
points to.
If you don't want to change to use a string
class then you need to call a function to copy the string into the supplied buffer. For example:
StringCchCopy(result, MAX_PATH, path);
(this assumes the buffer is MAX_PATH
characters in size).