Uchiha Itachi
Uchiha Itachi

Reputation: 1271

LPCWSTR to char* and vice versa

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

Upvotes: 1

Views: 11018

Answers (3)

Uchiha Itachi
Uchiha Itachi

Reputation: 1271

Figured it out through looking through the DirectX header files... If anyone is having this same problem, rather than using the:

D3DXGetImageInfoFromFile(varName, &info);

Instead, use:

D3DXGetImageInfoFromFileA(varName, &info);

Same goes for MessageBox's... Use

MessageBoxA(handleVar, messageVar, titleVar, iconType);    

*This comes with the caveat that your project property settings for character sets are set to Unicode. It becomes unnecessary if you switch that to Multi-byte.

As always, Thanks to those that actually contributed and helped me with this; and No Thanks to those who simply post on boards to ridicule or demean those who are less experienced - great use of your time.

Upvotes: 2

Anton Malyshev
Anton Malyshev

Reputation: 8861

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.

Upvotes: 5

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

I've used code exactly like this before and had no problems.

No, you haven't.

LPCWSTR and char* are essentially the same thing...

No, they're not.

If you read the documentation, you'll see that a LPCWSTR is a const wchar_t*.

You should take a const wchar_t* into your function instead.

If you really want to convert to a char*, and don't mind that (at least in one of the two directions) this makes no sense, then you can read the answers to the following questions:

… and apply const_cast afterwards. But, seriously, please don't.

Upvotes: 2

Related Questions