Visual c++ LPTSTR字符串中的垃圾值

Visual c++ LPTSTR字符串中的垃圾值,visual-c++,Visual C++,我编写了一个函数从路径返回扩展,如下所示: LPTSTR GetExtension(LPCTSTR path1) { CString str(path1); int length = str.ReverseFind(L'.'); str = str.Right(str.GetLength()-length); LPTSTR extension= str.GetBuffer(0); str.ReleaseBuffer(); return ext

我编写了一个函数从路径返回扩展,如下所示:

 LPTSTR GetExtension(LPCTSTR path1)
{
    CString str(path1);
    int length = str.ReverseFind(L'.');
    str = str.Right(str.GetLength()-length);

    LPTSTR extension= str.GetBuffer(0);
    str.ReleaseBuffer();

    return extension;

}
我检查了该语句,发现在返回时扩展名有一个有效值(.txt),但在main方法中使用以下语句时,如下图所示

LPTSTR extension = GetExtension(L"C:\\Windows\\text.txt");
变量扩展名具有以下垃圾值: ﻮ

ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮ䞐瀘嗯᠀骰PꬰP⚜叕u


有人能告诉我它背后的原因吗?

我没有带编译器,但可能是您正在获取缓冲区并存储它的位置。然后在LPTSTR仍指向一个位置时释放它


或者,当您在函数中查看LPTSTR时,它可能位于堆栈上。退出该函数时,您将失去它。

您将返回一个指向已释放缓冲区的指针。缓冲区是函数的局部变量。两个大号码都将签名改为

 size_t GetExtension(LPCTSTR path, LPTSTR buffer, size_t bufferSize)
这样您就可以将结果复制到缓冲区中

<>或返回cStstring或STD::WSstring,使用C++,而不是C。使用TCHAR也是一种处理字符串的非常过时的方法,最后一个非Unicode版本的Windows 12年前就死了。