如何将LPTSTR转换为LPCTSTR ?

How does one convert LPTSTR to LPCTSTR?

本文关键字:LPCTSTR 转换 LPTSTR      更新时间:2023-10-16

我需要转换:

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"              "*.bmp;*.dib;*.wdp;*.mdp;*.hdp;*.gif;*.png;*.jpg;*.jpeg;*.tif;*.ico"
"Windows Bitmap"               "*.bmp;*.dib"
"High Definition Photo"        "*.wdp;*.mdp;*.hdp"
"Graphics Interchange Format"  "*.gif"
"Portable Network Graphics"    "*.png"
"JPEG File Interchange Format" "*.jpg;*.jpeg"
"Tiff File"                    "*.tif"
"Icon"                         "*.ico"
"All Files"                    "*.*"
"";
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之前,它是混乱的。

只是将字符存储到gc.szFileTitleName中。

LPCTSTR只是LPTSTRconst版本。这种类型的转换是标准转换,因此您不需要显式地执行任何操作来获得所需的内容。你的错误一定是在别的地方。

除非你以某种方式编译你的代码与UNICODE集和一些没有它…

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")              TEXT("*.bmp;*.dib;*.wdp;*.mdp;*.hdp;*.gif;*.png;*.jpg;*.jpeg;*.tif;*.ico")
                            TEXT("Windows Bitmap")               TEXT("*.bmp;*.dib")
                            TEXT("High Definition Photo")        TEXT("*.wdp;*.mdp;*.hdp")
                            TEXT("Graphics Interchange Format")  TEXT("*.gif")
                            TEXT("Portable Network Graphics")    TEXT("*.png")
                            TEXT("JPEG File Interchange Format") TEXT("*.jpg;*.jpeg")
                            TEXT("Tiff File")                    TEXT("*.tif")
                            TEXT("Icon")                         TEXT("*.ico")
                            TEXT("All Files")                    TEXT("*.*")
                            TEXT("");
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);