求助:VS2010下,strcpy函数的问题!

CStringstrSource;if(OpenClipboard())//打开剪贴板{HGLOBALclipBuffer;char*pcBuffer;EmptyClip... CString strSource;

if(OpenClipboard()) //打开剪贴板
{
HGLOBAL clipBuffer;
char*pcBuffer;
EmptyClipboard(); //清空剪贴板内容
GetDlgItemText(IDC_EDIT1, strSource); //从指定ID文本框获取字符串
clipBuffer = GlobalAlloc(GMEM_DDESHARE, strSource.GetLength()+1); //获取内存,长度为字符串长度
pcBuffer = (char*)GlobalLock(clipBuffer);
strcpy(pcBuffer, LPCSTR(strSource)); //strcpy_s比strcpy内部多了安全性判断,消除不安全警告
GlobalUnlock(clipBuffer);
SetClipboardData(CF_TEXT, clipBuffer); //设置剪贴板内容
CloseClipboard(); //必须关闭剪贴板
}

在VS2010下,上面的代码中strcpy函数中第2个参数处,提示:不存在从CSTRING到LPCSTR的适当转换函数!
各位这是怎么回事呀。要怎么改。怎样将CSTRING转换成char *呀?求助呀!!!!
这个在孙鑫教程里 就是这样的,而且在VC++6.0中也是可以的,只是在Vs中不行 急等啊
展开
 我来答
qiny1010
2012-10-27 · TA获得超过232个赞
知道小有建树答主
回答量:315
采纳率:0%
帮助的人:150万
展开全部
使用这个试试
CString str ="abc";
pcBuffer = (char*)(LPCTSTR)(str);

或者使用
LPTSTR p = s.GetBuffer( s.length+1 );
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
kaixingui2012
2012-10-27 · TA获得超过4.2万个赞
知道大有可为答主
回答量:1.4万
采纳率:81%
帮助的人:6811万
展开全部
LPCSTR是个宏,相当于char *

CString strSource; //定义strSource是string类,不能强制转换成char *

strcpy( pcBuffer , strSource.GetBuffer(0) );//这样试一下
追问
不行
错误1error C2664: “strcpy”: 不能将参数 2 从“wchar_t *”转换为“const char
追答
查了MSDN你的用法没有错误,可以直接使用LPCSTR转换
是不是缺少头文件啊?#inlcude
以下是MSDN中提供的用例,希望对你有帮助。
The following example demonstrates the use of CString::operator LPCSTR.
// If the prototype of a function is known to the compiler,

// the LPCTSTR cast operator may be invoked implicitly
CString strSports(_T("Hockey is Best!"));
TCHAR sz[1024];
lstrcpy(sz, strSports);
// If the prototype isn't known, or is a va_arg prototype,
// you must invoke the cast operator explicitly. For example,
// the va_arg part of a call to sprintf() needs the cast:
sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports);
// while the format parameter is known to be an LPCTSTR and
// therefore doesn't need the cast:
sprintf(sz, strSports);
// Note that some situations are ambiguous. This line will
// put the address of the strSports object to stdout:
cout << strSports;
// while this line will put the content of the string out:
cout << (LPCTSTR) strSports;
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ASimpleDotaer
2012-10-27 · TA获得超过325个赞
知道答主
回答量:75
采纳率:0%
帮助的人:63.6万
展开全部
个人感觉是LPCSTR不能够接收CString对象作为参数,你可以试试其他方法看看如何将一个CString转化为一个char*
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式