c++ - 如何将 LPTSTR 转换为 LPCTSTR?

标签 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 之前,它搞砸了。

只是将字符存储到 gc.szFileTitleName 中。不知道为什么另一种方式会超出范围......

最佳答案

LPCTSTR 只是 LPTSTR 的一个const 版本。这种类型的转换是标准转换,因此您无需明确执行任何操作即可获得所需内容。你的错误一定是在别的地方。

除非您以某种方式编译一段设置了 UNICODE 的代码,而另一些则没有设置...

关于c++ - 如何将 LPTSTR 转换为 LPCTSTR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14074726/

相关文章:

c++ - 对 Winsock DEFAULT_PORT 的 PCSTR 的 Int

存储在 LPCSTR 中的 C++ 字符 .. 损坏了吗?

c++ - 视觉 C++ : Convert data type PCSTR to LPCWSTR

c++ - 如何自定义 C++ 列表容器,使其可以容纳不同类型的结构?

c++ - Visual C++ 错误 C3867 和 C2664

c++ - OpenGL - 绘制存储在 VBO 中的大量信息

c# - 当编辑框有多个时如何获取hWnd?

c++ - 如何从主应用程序解析库的编译器特定运行时初始化函数

windows - Closehandle() 不终止进程

c++ - WINAPI:查看来自其他进程的消息