将字符串解析为LPCWSTR

use*_*804 3 c++ winapi boost boost-filesystem

我正在使用boost-filesystem来搜索具体路径中的所有文件.我还想检索这个文件的创建数据,最后打开和最后更新,所以当我在Windows中工作时,我需要使用GetFileTime(这需要一个我将通过CreateFile函数得到的HANDLE.

关键是通过boost文件系统我得到一个字符串,如

string filename ="C:\ Users\MyUser\Desktop\PDN.pdf";

我需要将此字符串转换为LPCWSTR.

因此我做了几次尝试都失败了,例如:

HANDLE hFile = CreateFile((LPCWSTR)fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

但是当这样做时,它成功了:

HANDLE hFile = CreateFile(L"C:\\Users\\MyUSer\\Desktop\\PDN.pdf", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何使用字符串变量将字符串解析为PWSTR?如果可能的话(我猜不是),是否有任何函数可以改变原始路径添加斜杠哪里找到另一个斜杠?

非常感谢

编辑:这是我在这里阅读后的方式:

wstring fileFullPathWstring = winAPII.stringToWstring(iter-> path().string());

HANDLE hFile = CreateFile(fileFullPathWstring.c_str(),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);

使用功能:

wstring WinAPIIteraction::stringToWstring(string stringName){
    int len;
    int slength = (int)stringName.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, stringName.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, stringName.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*lev 5

您可以使用MultibyteToWideChar()函数执行实际转换(MSDN页面).不需要添加斜杠 - 它们只是'\'在程序代码中表示单个的转义序列.