【发布时间】:2014-01-04 23:22:55
【问题描述】:
我正在使用 boost-filesystem 来搜索具体路径中的所有文件。 我还想检索此文件的创建数据、上次打开和上次更新,以便在 Windows 中工作时我需要使用 GetFileTime(这需要 HANDLE,我将通过 CreateFile 函数获取。
关键是通过提升文件系统我得到一个字符串,例如
字符串文件名="C:\Users\MyUser\Desktop\PDN.pdf";
我需要将此字符串转换为 LPCWSTR。
因此我做了几次尝试都失败了,例如:
HANDLE hFile = CreateFile((LPCWSTR)fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
但是当这样做时,它成功了:
HANDLE hFile = CreateFile(L"C:\\Users\\MyUSer\\Desktop\\PDN.pdf", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
所以我的问题是,如何使用字符串变量将字符串解析为 PWSTR? 如果可能的话(我猜不是),是否有任何函数可以改变原始路径,在其中找到另一个斜线的斜线?
非常感谢
已编辑: 这是我在这里阅读后所做的方式:
wstring 文件FullPathWstring = 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;
}
【问题讨论】:
-
string更可能是LPCSTR,wstring是LPCWSTR- 这是 UNICODE 和 ANSI 的混合。最好保持一致。 -
你为什么不使用 wchar_t* fileName = "C:\\Users\\MyUSer\\Desktop\\PDN.pdf" ?
标签: c++ winapi boost boost-filesystem