如何将LPTSTR转换为LPCTSTR?

时间:2012-12-28 19:34:26

标签: c++ winapi lpcstr lptstr

我需要转换:

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.

即使在MSVC ++ 2012 Express中它也说它的LPSTR。

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.

我非常欣赏这一点。 :)

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)

这将有一个空白图像。我已经在GetOpenFile之后用消息框测试了它,它返回很棒。

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

但在D3DXCreateTextureFromFileEx之前,它搞砸了。

只需将char存储到gc.szFileTitleName中。不知道为什么另一种方式超出了范围...

2 个答案:

答案 0 :(得分:5)

LPCTSTR只是const的{​​{1}}版本。这种类型的转换是标准转换,因此您无需明确执行任何操作即可获得所需内容。您的错误必须在其他地方。

除非你以某种方式使用LPTSTR设置编译一段代码,否则编写一些代码...

答案 1 :(得分:1)

GetOpenFileName()D3DXCreateTextureFromFileEx()MessageBox()都处理基于TCHAR的字符串,但您实际上并未在代码中使用TCHAR。试试这个:

TCHAR szFileName[MAX_PATH] = {0};
TCHAR 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      =   TEXT("All Image Files\0")              TEXT("*.bmp;*.dib;*.wdp;*.mdp;*.hdp;*.gif;*.png;*.jpg;*.jpeg;*.tif;*.ico\0")
                            TEXT("Windows Bitmap\0")               TEXT("*.bmp;*.dib\0")
                            TEXT("High Definition Photo\0")        TEXT("*.wdp;*.mdp;*.hdp\0")
                            TEXT("Graphics Interchange Format\0")  TEXT("*.gif\0")
                            TEXT("Portable Network Graphics\0")    TEXT("*.png\0")
                            TEXT("JPEG File Interchange Format\0") TEXT("*.jpg;*.jpeg\0")
                            TEXT("Tiff File\0")                    TEXT("*.tif\0")
                            TEXT("Icon\0")                         TEXT("*.ico\0")
                            TEXT("All Files\0")                    TEXT("*.*\0")
                            TEXT("\0");
gc.ofn.nMaxFileTitle = MAX_PATH;
gc.ofn.lpstrFileTitle = gc.szFileTitleName;
gc.ofn.lpstrFile       = szFileName;
gc.ofn.nMaxFile        = MAX_PATH;
gc.ofn.lpstrTitle      = TEXT("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)

MessageBox(NULL, gc.ofn.lpstrFileTitle, TEXT(""), 0);