简体   繁体   English

LPCTSTR的非常奇怪的行为

[英]Very Strange Behavior of LPCTSTR

I've been working on a system to reversivly search directories with FindFirstFile and FindNextFile but I've encountered an issue that I don't understand. 我一直在使用FindFirstFile和FindNextFile反向搜索目录的系统上工作,但是遇到了我不理解的问题。

Below is a code snippet. 下面是一个代码片段。

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine
                  ,int nCmdShow)
{    
    searchDrive((LPCTSTR)"C:\\",(LPCTSTR)"*.bdjf");
    return 0;
}

bool searchDrive(LPCTSTR lpFolder, LPCTSTR lpFilePattern)
{

   TCHAR  szFullPattern[MAX_PATH];

   WIN32_FIND_DATA FindFileData;

   HANDLE hFile = INVALID_HANDLE_VALUE;

   PathCombine(szFullPattern, lpFolder, L"x");

   MessageBox(NULL,szFullPattern,lpFilePattern,MB_ICONWARNING | 
                                               MB_CANCELTRYCONTINUE | 
                                               MB_DEFBUTTON2);

   HANDLE hFind = FindFirstFile(szFullPattern, &FindFileData);

I'm using Visual Studio 2008. 我正在使用Visual Studio 2008。

在此处输入图片说明

As you can see all except but the last character and the '\\' can't be seen, the the rest have come out in Asian characters. 如您所见,除了最后一个字符和'\\'以外,其他所有字符均以亚洲字符显示。

(NOTE don't worry about any of the other issues with my code.) (注意,我的代码不会担心其他任何问题。)

Any ideas on why this is happening would be appreciated. 任何想法为什么会发生将不胜感激。

Here's your problem: 这是您的问题:

searchDrive((LPCTSTR)"C:\\",(LPCTSTR)"*.bdjf");

By default, Visual Studio compiles programs in Unicode mode. 默认情况下,Visual Studio以Unicode模式编译程序。 So you're casting both of the "ANSI" (8-bit characters) strings to "Unicode" (16-bit character) string type. 因此,您要将两个“ ANSI”(8位字符)字符串都转换为“ Unicode”(16位字符)字符串类型。

This doesn't convert the strings. 这不会转换字符串。 It just tells the compiler to pretend that they were Unicode strings all along. 它只是告诉编译器假装它们一直都是Unicode字符串。 It's hardly surprising that this doesn't work; 这样做并不奇怪,这不足为奇。 the upshot is that each pair of ANSI characters is treated as a single Unicode character. 结果是每对ANSI字符对都被视为一个Unicode字符。

You can fix the problem like this: 您可以像这样解决问题:

searchDrive(TEXT("C:\\"), TEXT("*.bdjf"));

But unless you have a specific reason to support ANSI mode, it would be better still to use 但是,除非有特殊原因支持ANSI模式,否则最好还是使用

searchDrive(L"C:\\", L"*.bdjf");

and change the declaration of searchDrive to use LPCWSTR instead of LPCTSTR . 并将searchDrive的声明searchDrive为使用LPCWSTR而不是LPCTSTR

Change this line: 更改此行:

searchDrive((LPCTSTR)"C:\\",(LPCTSTR)"*.bdjf");

To this: 对此:

searchDrive(L"C:\\", L"*.bdjf");

You can also say TEXT("C:\\\\") and TEXT("*.bdjf") to do the appropriate string literal conversion. 您也可以说TEXT("C:\\\\")TEXT("*.bdjf")进行适当的字符串文字转换。

But in general, you should just stop using TCHAR and just use Unicode and long strings everywhere. 但通常,您应该只停止使用TCHAR ,而到处都使用Unicode和长字符串。

Please remove all casts where you don't know exactly what you are doing and can explain why the cast is neccessary for correct working. 请移除所有类型转换,你不知道自己在做什么并能解释为什么中投neccessary为正确的工作。 Never muzzle the compiler, instead ask it to speak up: Use -Wall -Wextra , and handle all warnings appropriately. 切勿-Wall -Wextra编译器,而要大声疾呼:使用-Wall -Wextra ,并适当处理所有警告。

Doing so will make the error glaringly obvious. 这样做会使错误非常明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM