
求助: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中不行 急等啊 展开
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中不行 急等啊 展开
3个回答
展开全部
使用这个试试
CString str ="abc";
pcBuffer = (char*)(LPCTSTR)(str);
或者使用
LPTSTR p = s.GetBuffer( s.length+1 );
CString str ="abc";
pcBuffer = (char*)(LPCTSTR)(str);
或者使用
LPTSTR p = s.GetBuffer( s.length+1 );
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
LPCSTR是个宏,相当于char *
CString strSource; //定义strSource是string类,不能强制转换成char *
strcpy( pcBuffer , strSource.GetBuffer(0) );//这样试一下
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;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
个人感觉是LPCSTR不能够接收CString对象作为参数,你可以试试其他方法看看如何将一个CString转化为一个char*
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询