如何将CString 转换为LPCSTR ?
在使用SHFileOperation(...)时其中有两个参数pFrom, pTo是LPCSTR,而我的路径保存在CString类中.没办法将CString转换为LPCSTR.
请给位大虾指点!
问题点数:50、回复次数:27Top
1 楼mrwang2000(王先生)回复于 2006-03-03 21:45:11 得分 0
have a try
(LPCSTR)(LPCTSTR)MyString // MyString is a CString instanceTop
2 楼adaisong()回复于 2006-03-03 21:56:32 得分 0
不行!!
SHFILEOPSTRUCT lpsh;
ZeroMemory(&lpsh,sizeof(lpsh));
lpsh.hwnd= m_hWnd;
lpsh.fFlags=FOF_NOCONFIRMATION
| FOF_SIMPLEPROGRESS
| FOF_WANTMAPPINGHANDLE ;
lpsh.wFunc=FO_COPY;
CString from = "H:\\DIPDemo\\Boat.bmp";
lpsh.pFrom= (LPCSTR)(LPCTSTR)from;
CString to = "H:\\新建文件夹";
lpsh.pTo= (LPCSTR)(LPCTSTR)to;
if( 0 != SHFileOperation(&lpsh))
{
AfxMessageBox("复制文件出错,请检查");
return;
}Top
3 楼huziwu(虎子)回复于 2006-03-03 21:57:26 得分 0
LPCSTR lpstr;
CString csstr="abced";
lpstr=csstr.GetBuffer();Top
4 楼adaisong()回复于 2006-03-03 22:01:43 得分 0
不行!
SHFILEOPSTRUCT lpsh;
ZeroMemory(&lpsh,sizeof(lpsh));
lpsh.hwnd= m_hWnd;
lpsh.fFlags=FOF_NOCONFIRMATION
| FOF_SIMPLEPROGRESS
| FOF_WANTMAPPINGHANDLE ;
lpsh.wFunc=FO_COPY;
CString from = "H:\\DIPDemo\\Boat.bmp";
lpsh.pFrom= (LPCSTR)(LPCTSTR)from;
CString to = "H:\\新建文件夹";
lpsh.pTo= (LPCSTR)(LPCTSTR)to;
if( 0 != SHFileOperation(&lpsh))
{
AfxMessageBox("复制文件出错,请检查");
return;
}Top
5 楼adaisong()回复于 2006-03-03 22:05:48 得分 0
也不行!
SHFILEOPSTRUCT lpsh;
ZeroMemory(&lpsh,sizeof(lpsh));
lpsh.hwnd= m_hWnd;
lpsh.fFlags=FOF_NOCONFIRMATION
| FOF_SIMPLEPROGRESS
| FOF_WANTMAPPINGHANDLE ;
lpsh.wFunc=FO_COPY;
CString from = "H:\\DIPDemo\\Boat.bmp";
lpsh.pFrom= from.GetBuffer(0);
CString to = "H:\\新建文件夹";
lpsh.pTo= to.GetBuffer(0);
if( 0 != SHFileOperation(&lpsh))
{
AfxMessageBox("复制文件出错,请检查");
return;
}
from.ReleaseBuffer();
to.ReleaseBuffer();
Top
6 楼junliu_0(驴子)回复于 2006-03-03 22:07:47 得分 0
(LPCSTR)_bstr_t(CString)Top
7 楼adaisong()回复于 2006-03-03 22:18:32 得分 0
也不行也!
lpsh.pFrom= (LPCSTR)_bstr_t(from);Top
8 楼DrSmart(斯玛特)回复于 2006-03-03 22:19:16 得分 0
(LPCSTR)(LPCTSTR)Top
9 楼junliu_0(驴子)回复于 2006-03-03 22:20:01 得分 0
_variant_t(from)Top
10 楼adaisong()回复于 2006-03-03 22:31:48 得分 0
lpsh.pFrom= (LPCSTR)_variant_t(from);
无法通过编译Top
11 楼junliu_0(驴子)回复于 2006-03-03 22:47:11 得分 10
lpsh.pFrom= _variant_t(from);
Top
12 楼qiushuiwuhen(秋水无恨)回复于 2006-03-03 22:48:32 得分 0
lpsh.pFrom= (LPCSTR)(LPCTSTR)from;
lpsh.pFrom[_tcslen(lpsh.pFrom)]=0;Top
13 楼xing_xing_xing(哈哈)回复于 2006-03-03 23:05:38 得分 10
后面要多加一个字符串结束符号
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';
Top
14 楼striking(庸人自扰)回复于 2006-03-03 23:09:35 得分 10
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;
Top
15 楼vcmute(BCare4 H1Rest Good9!)回复于 2006-03-03 23:11:32 得分 10
lpsh.pFrom= from.GetBuffer(from.GetLength()+2);
from.ReleaseBuffer();Top
16 楼striking(庸人自扰)回复于 2006-03-03 23:12:24 得分 0
buf[_tcslen(buf)+1]=0; // need two NULLs at end 关键在这, 需要2个终止符Top
17 楼snlux(snlux)回复于 2006-03-03 23:13:34 得分 0
CString strPathName = TEXT("...");
char szFrom[1024] = { 0 };
_tcscpy(buf, T2A(strPathName));
lpsh.pFrom = szFrom;Top
18 楼cyblueboy83(爱情白痴—电脑迷)回复于 2006-03-04 00:02:37 得分 0
CString str;
str = "hello";
char * ch = "hello2";
LPSTR p=str.GetBuffer();Top
19 楼f9941220(无虚)回复于 2006-03-04 08:43:35 得分 10
使用成员函数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 );
Top
20 楼ZengMuAnSha(曾牧暗鲨)回复于 2006-03-04 08:56:10 得分 0
使用成员函数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 );Top
21 楼lonkil(www.vcfans.com)回复于 2006-03-04 09:16:30 得分 0
关于CString类型间的互相转换
http://www.vcfans.com/article/vc/2005/07/211756803.htmTop
22 楼JFlyer(一万年太久,只争朝夕)回复于 2006-03-04 10:43:11 得分 0
强制类型转换(LPCSTR)XXXXTop
23 楼nodefault(永不言败)回复于 2006-03-04 11:34:57 得分 0
LPCSTR DATA = str;
Top
24 楼Kaile(领头羊)回复于 2006-03-04 12:07:07 得分 0
CString strTmp = "abc";
LPCSTR pStr = (LPCSTR ) strTmp.GetBuffer(strTmp.GetLength());
Top
25 楼deutsch(人民)回复于 2006-03-04 12:14:20 得分 0
这是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中就有啊!!!!!!!!!Top
26 楼zyyoung(倡导开源)回复于 2006-03-04 12:33:12 得分 0
markTop
27 楼adaisong()回复于 2006-03-04 15:03:34 得分 0
3q everyone!Top




