简体   繁体   中英

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

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM