-1

I keep getting compile errors with the line at the bottom

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

The compiler keeps throwing error C2664 back at me, : cannot convert argument 1 from 'const char *' to 'LPCWSTR'

How do I create a LPCWSTR to a std::string to pass to into FindFirstFile?

Zhe section of code is for reference.

The actual code follows below.

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);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

1 Answers1

3

The WinAPI data types are lovely short abbreviations. LPCWSTR is short for:

Long
Pointer to the start of
Const
Wide
STRing

As such it is a pointer (long pointers are history) to the first character of a const wide string (const wchar_t*), meaning you need to use std::wstring::c_str() instead of std::string::c_str().

Side note: just be sure to #define UNICODE everywhere you use the WinAPI, otherwise you'll get other errors about conversion to LPCSTR. Alternatively, explicitly use the W versions of the WinAPI functions where they exist.