c++winapilpcstrlptstr

How does one convert LPTSTR to LPCTSTR?


I need to convert:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646839(v=vs.85).aspx

lpstrFileTitle
Type: LPTSTR
The file name and extension (without path information) of the selected file. This member can be NULL.

Even though in MSVC++ 2012 Express it says its LPSTR.

To

http://msdn.microsoft.com/en-us/library/windows/desktop/bb172802(v=vs.85).aspx

pSrcFile [in]
Type: LPCTSTR
Pointer to a string that specifies the filename. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.

I would very much appreciate this. :)

char szFileName[MAX_PATH] = {0};
char szFileTitleName[MAX_PATH] = {0};
HRESULT hr = S_OK;
RtlZeroMemory(&gc.ofn, sizeof(gc.ofn));

gc.ofn.lStructSize      =   sizeof(gc.ofn);
gc.ofn.hwndOwner        =   hWnd;
gc.ofn.lpstrFilter      =   "All Image Files\0"              "*.bmp;*.dib;*.wdp;*.mdp;*.hdp;*.gif;*.png;*.jpg;*.jpeg;*.tif;*.ico\0"
"Windows Bitmap\0"               "*.bmp;*.dib\0"
"High Definition Photo\0"        "*.wdp;*.mdp;*.hdp\0"
"Graphics Interchange Format\0"  "*.gif\0"
"Portable Network Graphics\0"    "*.png\0"
"JPEG File Interchange Format\0" "*.jpg;*.jpeg\0"
"Tiff File\0"                    "*.tif\0"
"Icon\0"                         "*.ico\0"
"All Files\0"                    "*.*\0"
"\0";
gc.ofn.nMaxFileTitle = MAX_PATH;
gc.ofn.lpstrFileTitle = gc.szFileTitleName; // its a char
gc.ofn.lpstrFile       = szFileName;
gc.ofn.nMaxFile        = MAX_PATH;
gc.ofn.lpstrTitle      = "Open Image";
gc.ofn.Flags           = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;

if(GetOpenFileName(&gc.ofn)) {
gc.render_on = true;
}

.

D3DXCreateTextureFromFileEx (d3dDevice, gc.szFileTitleName , D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0,
        D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
        colorkey, &init_map->image_info, NULL, &init_map->texture_buffer)

This will have a blank image. I have tested this with a messagebox right after GetOpenFile, and it returns great.

MessageBoxA ( NULL , gc.ofn.lpstrFileTitle , "" , 0 );

But before the D3DXCreateTextureFromFileEx, its messed up.

Just storing the the char into gc.szFileTitleName. No idea why the other way was going out of scope ...


Solution

  • LPCTSTR is just a const version of LPTSTR. That type of conversion is a standard conversion, so you shouldn't need to do anything explicitly to get what you want. Your error must be somewhere else.

    Unless you are somehow compiling a piece of your code with UNICODE set and some without it...