-1

我不断收到底部行的编译错误

hFind = FindFirstFile(fileFilter.c_str()), &FindFileData); 

编译器不断抛出error C2664 back at me, : cannot convert argument 1 from 'const char *' to 'LPCWSTR'

如何将 LPCWSTR 创建到 std::string 以传递给 FindFirstFile?

这部分代码供参考。

实际代码如下。

using namespace std;

void GetFileListing(string directory, string fileFilter, bool recursively = true)    
{    
    if (recursively)
        GetFileListing(directory, fileFilter, false);

    directory += "\\";
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind ;
    string filter = directory + (recursively ? "*" : fileFilter);
    string Full_Name;
    string Part_Name;

// the line causing the compile error

    hFind = FindFirstFile(fileFilter.c_str()), &FindFileData);
4

1 回答 1

2

WinAPI 数据类型是可爱的简短缩写。LPCWSTR简称:

Long
Pointer to the start of
Const
Wide
STRing

因此,它是指向 const 宽字符串 ( const wchar_t*) 的第一个字符的指针(长指针是历史记录),这意味着您需要使用std::wstring::c_str()而不是std::string::c_str().

旁注:请确保#define UNICODE在您使用 WinAPI 的任何地方都使用,否则您将收到有关转换为LPCSTR. 或者,显式使用W它们存在的 WinAPI 函数的版本。

于 2017-05-10T11:45:15.950 回答