FindFirstFile的期望输入类型是什么? [英] What is the expected input type of FindFirstFile?

查看:347
本文介绍了FindFirstFile的期望输入类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会先说我基本上不知道宽字符串和Unicode支持。我让QString和QFile处理这99%的时间,但我试图编译为VC6写的别人的库。

I'll start by saying I know basically nothing about wide strings and Unicode support. I let QString and QFile handle that for me 99% of the time, but I'm trying to compile someone else's library written for VC6.

当我编译MSVC2010在Qt Creator我得到这个错误:

When I compile with MSVC2010 in Qt Creator I get this error:

error: C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

代码使用 FindFirstFile 函数,它是重载的(排序),取决于是否使用Unicode字符集进行编译。我不明白什么类型 FindFirstFile 期望,当FindFirstFileA和FindFirstFileW的输入看起来是两个完全不同的类型。

The code is using the FindFirstFile function, which is overloaded (sort of) depending on whether you're compiling with the Unicode character set. I don't understand what type FindFirstFile is expecting, when the input for FindFirstFileA and FindFirstFileW seems to be two completely different types.

这里是我的问题: FindFirstFile

推论:我接受类型 const char * 的文件名,并将其放入FindFirstType将接受的表单中。

Corollary: How do I take a filename of type const char* and put it into a form that FindFirstType will accept?

推荐答案

FindFirstFile是一个定义如下的宏:

FindFirstFile is a macro defined as follows:

#ifdef UNICODE
#define FindFirstFile  FindFirstFileW
#else
#define FindFirstFile  FindFirstFileA
#endif // !UNICODE


$ b b

这意味着它在使用 UNICODE 定义编译时扩展为 W 它扩展到一个 A 否则。

What this means is that it expands to the one with a W when compiled with UNICODE defined, and it expands to the one with an A otherwise.

现在, FindFirstFile 的第一个参数是 LPCSTR LPWCSTR LPCSTR const char * 的typedef,而 LPWCSTR const wchar_t * 的typedef。在您的错误消息中,您尝试传递 const char * 类型作为 FindFirstFileW 的第一个参数,的类型 const wchar_t * ,因此错误。

Now, FindFirstFile's first parameter is either LPCSTR or LPWCSTR. LPCSTR is a typedef for const char* while LPWCSTR is a typedef for const wchar_t*. In your error message, you try passing a type of const char* as the first argument to FindFirstFileW which takes an argument of type const wchar_t*, hence the error.

为了匹配类型,传递类型 const wchar_t * 的对象,您有几个选项:

In order for the types to match up, you need to pass an object of type const wchar_t*, you have several options:

std::wstring path1 = L"..."; // 1
const wchar_t* path2 = L"..."; // 2
wchar_t path3[] = L"..."; // 3

WIN32_FIND_DATA  w32fd;
FindFirstFile(path1.c_str(), &w32fd); // 1
FindFirstFile(path2, &w32fd); // 2
FindFirstFile(path3, &w32fd); // 3
FindFirstFile(L"...", &w32fd);




如何获取const char *类型的文件名,

How do I take a filename of type const char* and put it into a form that FindFirstType will accept?

如果文件名只包含基本ASCII字符集中的字符,可以将其转换为 std :: wstring ,如下所示: std :: wstring path(std :: begin(filename),std :: end (filename)); 。否则,您需要使用 MultiByteToWideChar 这里显示的许多选项。另一种选择是直接调用 FindFirstFileA ,但如果你在windows上,一般最好使用 wchar_t

If your filename only contains characters in the basic ASCII character set, then you can convert it to a std::wstring like the following: std::wstring path(std::begin(filename), std::end(filename));. Otherwise, you would need to use MultiByteToWideChar or many of the options shown here. Another option would be to call FindFirstFileA directly, but if you are on windows, it generally would be better to use wchar_t to begin with.

这篇关于FindFirstFile的期望输入类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆