LPCWSTR to char* and vice versa

Uchiha Itachi picture Uchiha Itachi · Sep 27, 2016 · Viewed 7.2k times · Source

I'm working on a simple project with DirectX9. I'm having a little snag with some conversion of data types, and I've done some research, but haven't found anything particularly useful. Let's start with the code:

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;

}

At line 6, I'm getting the error for the variable fileName:

IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"

I also get the exact same error message at the second and third parameters when trying to use MessageBox's:

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

I've used code exactly like this before and had no problems. No idea what is going on - especially because LPCWSTR and char* are essentially the same thing...

Any help is appreciated!! Thank you

Answer

Anton Malyshev picture Anton Malyshev · Sep 27, 2016

The most probable reason that you had no problem before is that you turned on unicode in Visual C++ project's settings. See the accepted answer here to turn it off again (if it's possible for you): How do I turn off Unicode in a VC++ project?

Otherwise you need to convert char * to wchar_t * using MultiByteToWideChar function.