C++ CString 到 LPCTSTR 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12620820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CString to LPCTSTR conversion
提问by Ionut Daniel
I have a CString variable that i a need to convert to LPCTSTR(const char*) .I need this conversion so that i can use it as an argument in a function .
我有一个 CString 变量,我需要将其转换为 LPCTSTR(const char*) 。我需要这种转换,以便我可以将它用作函数中的参数。
The CString look like :
CString 看起来像:
CString sqlTemp = _T("INSERT INTO "+ sw1 +" (filename, "+ sw2 +") VALUE ("+ sw7 +","+ sw3 +" ) ");
It contains an query. The prototype of the function is :
它包含一个查询。函数原型为:
int WriteBlob(LPCTSTR szSqlStat, LPCTSTR szFilePath)
So could you show me an exemple of how to convert to LPCTSTR ? It may be trivial but i am a c++ beginner and i still get a hang of it.
那么你能告诉我一个如何转换为 LPCTSTR 的例子吗?这可能是微不足道的,但我是 C++ 初学者,我仍然掌握它。
Thanks .
谢谢 。
回答by Ionut Hulub
One method of conversion is like this:
一种转换方法是这样的:
CString str;
str = "Hello";
LPCSTR szTemp = (LPCSTR)(LPCTSTR)str;
回答by JD-V
CString str; // the given string
CStringA strA(str); // a helper string
LPCSTR ptr = strA;