LPCWSTR转换为char *,反之亦然

Uch*_*chi 0 c++ type-conversion char directx-9 lpcwstr

我正在使用DirectX9进行一个简单的项目。我对数据类型的转换有些迷惑,并且已经做过一些研究,但是还没有发现任何特别有用的东西。让我们从代码开始:

LPDIRECT3DSURFACE9 LoadSurface(char *fileName, D3DCOLOR transColor)
{
LPDIRECT3DSURFACE9 image = NULL;
D3DXIMAGE_INFO info;
HRESULT result;

result = D3DXGetImageInfoFromFile(fileName, &info);
if (result != D3D_OK) return NULL;

result = d3ddev->CreateOffscreenPlainSurface(
    info.Width,         //width of the surface 
    info.Height,        //height of the surface
    D3DFMT_X8R8G8B8,    //surface format
    D3DPOOL_DEFAULT,    //memory pool use
    &image,             //reference to image
    NULL);              //reserved - ALWAYS NULL

//make sure file loaded properly
if (result != D3D_OK) return NULL;

return image;

}
Run Code Online (Sandbox Code Playgroud)

在第6行,我得到了变量fileName的错误:

IntelliSense:“ char *”类型的参数与“ LPCWSTR”类型的参数不兼容

当尝试使用MessageBox时,我在第二个和第三个参数上也得到了完全相同的错误消息:

if (d3ddev == NULL)
{
MessageBox(hWnd, "Error Creating Direct3D Device", "Error", MB_ICONERROR);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我以前使用过完全像这样的代码,没有任何问题。不知道发生了什么-特别是因为LPCWSTR和char *本质上是同一件事...

任何帮助表示赞赏!谢谢

Ant*_*hev 5

您之前没有问题的最可能原因是您在Visual C ++项目的设置中打开了unicode。请参阅此处接受的答案以再次将其关闭(如果可能的话): 如何在VC ++项目中关闭Unicode?

否则,您需要转换char *wchar_t *使用MultiByteToWideChar函数