[英]MFC(unicode): how do i convert a wstring filepath to a string path?
我有一个使用 MFC、C++ 创建并使用 Unicode 的应用程序。
我必须使用 wstring 文件路径加载纹理。 这个 wstring 文件路径是由系统给出的,我不需要在任何地方显示它。 我只需要将它传递给纹理加载方法。 不幸的是,纹理加载方法(来自 lib)只采用字符串 filePath。 像下面这样:
wstring wstringPath = L"abc路徑";// how do I convert it to a string path to used in the following method
stbi_load(stringPath, &width, &height, &nrChannels, 0); //texture loading method
我搜索了很多,没有找到一个好的答案(或太复杂)来解决这个问题。 任何人都可以帮忙吗?
我尝试过但不起作用的东西:
size_t len = wcslen(wstringPath .c_str()) + 1;
size_t newLen = len * 2;
char* newPath = new char[newLen];
wcstombs(newPath, wstringPath .c_str(), sizeof(newPath));
glGenTextures(1, &m_textureID);
int width, height, nrComponents;
unsigned char *data = stbi_load(newPath, &width, &height, &nrComponents, 0);
您可以使用(仅适用于 MS Windows)函数WideCharToMultiByte
。 您可以设置为CP_ACP
目标代码页。
我查看了 API stbi_load,它似乎为文件名使用了const char*
。
在这种情况下,最懒惰和最好的方法是使用 CString 类。
#include <atlstr.h> // might not need if you already have MFC stuff included
CStringA filePathA(wstringPath.c_str()); // it converts using CP_THREAD_ACP
stbi_load(filePathA, ....); // automatically casts to LPCSTR (const char*)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.