如何将CString 转换为LPCSTR ?

adaisong 2006-03-03 09:42:00
在使用SHFileOperation(...)时其中有两个参数pFrom, pTo是LPCSTR,而我的路径保存在CString类中.没办法将CString转换为LPCSTR.
请给位大虾指点!
...全文
5861 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyyoung 2006-03-04
  • 打赏
  • 举报
回复
mark
deutsch 2006-03-04
  • 打赏
  • 举报
回复
这是msdn中现有的例子:
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
//... modify lpsz as much as you want
很多问题msdn中就有啊!!!!!!!!!
Kaile 2006-03-04
  • 打赏
  • 举报
回复
CString strTmp = "abc";
LPCSTR pStr = (LPCSTR ) strTmp.GetBuffer(strTmp.GetLength());

herman~~ 2006-03-04
  • 打赏
  • 举报
回复
CString str;
str = "hello";
char * ch = "hello2";
LPSTR p=str.GetBuffer();
nodefault 2006-03-04
  • 打赏
  • 举报
回复
LPCSTR DATA = str;
飞驰的青蛙 2006-03-04
  • 打赏
  • 举报
回复
强制类型转换(LPCSTR)XXXX
lonkil 2006-03-04
  • 打赏
  • 举报
回复
关于CString类型间的互相转换

http://www.vcfans.com/article/vc/2005/07/211756803.htm
  • 打赏
  • 举报
回复
使用成员函数GetBufferSetLength。
CString szTmp;
szTmp.GetBufferSetLength(100);
MSDN解释:
CString::GetBufferSetLength
Call this member function to retrieve a pointer to the internal character buffer for the CString object, truncating or growing its length if necessary to exactly match the length specified in nNewLength. The returned LPTSTR pointer is not const and thus allows direct modification of CString contents.

Syntax
LPTSTR GetBufferSetLength( int nNewLength );
f9941220 2006-03-04
  • 打赏
  • 举报
回复
使用成员函数GetBufferSetLength。
CString szTmp;
szTmp.GetBufferSetLength(100);
MSDN解释:
CString::GetBufferSetLength
Call this member function to retrieve a pointer to the internal character buffer for the CString object, truncating or growing its length if necessary to exactly match the length specified in nNewLength. The returned LPTSTR pointer is not const and thus allows direct modification of CString contents.

Syntax
LPTSTR GetBufferSetLength( int nNewLength );

adaisong 2006-03-04
  • 打赏
  • 举报
回复
3q everyone!
snlux 2006-03-03
  • 打赏
  • 举报
回复
CString strPathName = TEXT("...");

char szFrom[1024] = { 0 };
_tcscpy(buf, T2A(strPathName));
lpsh.pFrom = szFrom;
striking 2006-03-03
  • 打赏
  • 举报
回复
buf[_tcslen(buf)+1]=0; // need two NULLs at end 关键在这, 需要2个终止符
vcmute 2006-03-03
  • 打赏
  • 举报
回复
lpsh.pFrom= from.GetBuffer(from.GetLength()+2);
from.ReleaseBuffer();
striking 2006-03-03
  • 打赏
  • 举报
回复
CString会去掉SHFILEOPSTRUCT必须的双NULL终止符中的一个。所以建议用字符数组来构建SHFILEOPSTRUCT所需的参数。

for example

CString pszPath = "c:\\";

TCHAR buf[_MAX_PATH + 1]; // allow one more character
_tcscpy(buf, pszPath); // copy caller's path name
buf[_tcslen(buf)+1]=0; // need two NULLs at end


lpsh.pFrom= buf;
xing_xing_xing 2006-03-03
  • 打赏
  • 举报
回复
后面要多加一个字符串结束符号
pFrom
Address of a buffer to specify one or more source file names. These names must be fully qualified paths. Standard Microsoft MS-DOS wild cards, such as "*", are permitted in the file-name position. Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of pFrom.

char buf[MAX_PATH + 1];
strcpy(buf,"H:\\DIPDemo\\Boat.bmp");
buf[strlen(buf)] = '\0';
qiushuiwuhen 2006-03-03
  • 打赏
  • 举报
回复
lpsh.pFrom= (LPCSTR)(LPCTSTR)from;
lpsh.pFrom[_tcslen(lpsh.pFrom)]=0;
junliu_0 2006-03-03
  • 打赏
  • 举报
回复
lpsh.pFrom= _variant_t(from);
adaisong 2006-03-03
  • 打赏
  • 举报
回复
lpsh.pFrom= (LPCSTR)_variant_t(from);
无法通过编译
junliu_0 2006-03-03
  • 打赏
  • 举报
回复


_variant_t(from)
DrSmart 2006-03-03
  • 打赏
  • 举报
回复
(LPCSTR)(LPCTSTR)
加载更多回复(7)
wince上各种类型转换特别是字符串是很头疼的,这个类是项目用的, 每个都测试好用,使用静态方法,直接引用后,写【::cv::就可以用】 typedef int u32; //转换类,所有的转换函数在这里静态 2是to的意思 //cs:cstring //s:string //i:int class cv { public: static char * s2charx(string s); //static char * cs2charx_unicode(CString s); static char * cs2charx_unicode(CString s); static CString charx2cs(char *s); cv(); virtual ~cv(); static int cstring_to_int(CString t); static CString s2cs(string text); static CString i2cs(int i); static string cs2s(CString s); static void Utf8ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static void Gb2312ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static CString string_to_cstring(string text); static int string_to_int(string str); static string cstring_to_string(CString text); static void Gb2312ToUtf8(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static BOOL IsNumeric(string text); static void UnicodeToGb2312(char* pstrOut, u32 dwOutLen, const wchar_t* pstrIn, u32 dwInLen); static void Utf8ToGb2312(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static int cs2i(CString s); static string int_to_string(int n); static CString ChineseCapitalMoney(double Num); //static wstring s2ws( string s ); static std::wstring s2ws(const string& s); static std::string WChar2Ansi(LPCWSTR pwszSrc); static string ws2s(wstring& inputws); static const char * s2constcharx(string s); static std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen); static char * cs2charx_ansi(CString s); //static string ws2s(wstring ws); };

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧