error C2664:不能将参数 1 从“CString”转换为“const char *”
2016-01-03 15:37
501 查看
提示错误“error C2664: "gethostbyname": 不能将参数 1 从"CString"转换为"const char *"”。
CString host;
lpHost = gethostbyname(host);
最快的解决办法:
Since this function requires Ansi string, I think you have to convert your Unicode one. There are different approaches. I would suggest trying this simple MFC-based method:
CString yourString = . . .;
CStringA ansiString = yourString;
gethostbyname(ansiString);
I hope it works.
See also: CT2A macro, WideCharToMultiByte function.
其他办法:
CString,char*,const char *,LPCTSTR 的转换
如何将CString类型的变量赋给char*类型的变量
1、GetBuffer函数:
使用CString::GetBuffer函数。
char *p;
CString str="hello";
p=str.GetBuffer(str.GetLength());
str.ReleaseBuffer();
将CString转换成char * 时
CString str("aaaaaaa");
strcpy(str.GetBuffer(10),"aa");
str.ReleaseBuffer();
当我们需要字符数组时调用GetBuffer(int n),其中n为我们需要的字符数组的长度.使用完成后一定要马上调用ReleaseBuffer();
还有很重要的一点就是,在能使用const char *的地方,就不要使用char
*
2、memcpy:
CString mCS=_T("cxl");
char mch[20];
memcpy(mch,mCS,20);
3、用LPCTSTR强制转换: 尽量不使用
char *ch;
CString str;
ch=(LPSTR)(LPCTSTR)str;
CString str = "good";
char *tmp;
sprintf(tmp,"%s",(LPTSTR)(LPCTSTR)str);
4、
CString Msg;
Msg=Msg+"abc";
LPTSTR lpsz;
lpsz = new TCHAR[Msg.GetLength()+1];
_tcscpy(lpsz, Msg);
char * psz;
strcpy(psz,lpsz);
CString类向const char *转换
char a[100];
CString str("aaaaaa");
strncpy(a,(LPCTSTR)str,sizeof(a));
或者如下:
strncpy(a,str,sizeof(a));
以上两种用法都是正确地. 因为strncpy的第二个参数类型为const
char *.所以编译器会自动将CString类转换成const char *.
CString转LPCTSTR (const char *)
CString cStr;
const char *lpctStr=(LPCTSTR)cStr;
LPCTSTR转CString
LPCTSTR lpctStr;
CString cStr=lpctStr;
将char*类型的变量赋给CString型的变量
可以直接赋值,如:
CString myString = "This is a test";
也可以利用构造函数,如:
CString s1("Tom");
将CString类型的变量赋给char []类型(字符串)的变量
1、sprintf()函数
CString str = "good";
char tmp[200] ;
sprintf(tmp, "%s",(LPCSTR)str);
(LPCSTR)str这种强制转换相当于(LPTSTR)(LPCTSTR)str
CString类的变量需要转换为(char*)的时,使用(LPTSTR)(LPCTSTR)str
然而,LPCTSTR是const char *,也就是说,得到的字符串是不可写的!将其强制转换成LPTSTR去掉const,是极为危险的!
一不留神就会完蛋!要得到char *,应该用GetBuffer()或GetBufferSetLength(),用完后再调用ReleaseBuffer()。
2、strcpy()函数
CString str;
char c[256];
strcpy(c, str);
char mychar[1024];
CString source="Hello";
strcpy((char*)&mychar,(LPCTSTR)source);
CString host;
lpHost = gethostbyname(host);
最快的解决办法:
Since this function requires Ansi string, I think you have to convert your Unicode one. There are different approaches. I would suggest trying this simple MFC-based method:
CString yourString = . . .;
CStringA ansiString = yourString;
gethostbyname(ansiString);
I hope it works.
See also: CT2A macro, WideCharToMultiByte function.
其他办法:
CString,char*,const char *,LPCTSTR 的转换
如何将CString类型的变量赋给char*类型的变量
1、GetBuffer函数:
使用CString::GetBuffer函数。
char *p;
CString str="hello";
p=str.GetBuffer(str.GetLength());
str.ReleaseBuffer();
将CString转换成char * 时
CString str("aaaaaaa");
strcpy(str.GetBuffer(10),"aa");
str.ReleaseBuffer();
当我们需要字符数组时调用GetBuffer(int n),其中n为我们需要的字符数组的长度.使用完成后一定要马上调用ReleaseBuffer();
还有很重要的一点就是,在能使用const char *的地方,就不要使用char
*
2、memcpy:
CString mCS=_T("cxl");
char mch[20];
memcpy(mch,mCS,20);
3、用LPCTSTR强制转换: 尽量不使用
char *ch;
CString str;
ch=(LPSTR)(LPCTSTR)str;
CString str = "good";
char *tmp;
sprintf(tmp,"%s",(LPTSTR)(LPCTSTR)str);
4、
CString Msg;
Msg=Msg+"abc";
LPTSTR lpsz;
lpsz = new TCHAR[Msg.GetLength()+1];
_tcscpy(lpsz, Msg);
char * psz;
strcpy(psz,lpsz);
CString类向const char *转换
char a[100];
CString str("aaaaaa");
strncpy(a,(LPCTSTR)str,sizeof(a));
或者如下:
strncpy(a,str,sizeof(a));
以上两种用法都是正确地. 因为strncpy的第二个参数类型为const
char *.所以编译器会自动将CString类转换成const char *.
CString转LPCTSTR (const char *)
CString cStr;
const char *lpctStr=(LPCTSTR)cStr;
LPCTSTR转CString
LPCTSTR lpctStr;
CString cStr=lpctStr;
将char*类型的变量赋给CString型的变量
可以直接赋值,如:
CString myString = "This is a test";
也可以利用构造函数,如:
CString s1("Tom");
将CString类型的变量赋给char []类型(字符串)的变量
1、sprintf()函数
CString str = "good";
char tmp[200] ;
sprintf(tmp, "%s",(LPCSTR)str);
(LPCSTR)str这种强制转换相当于(LPTSTR)(LPCTSTR)str
CString类的变量需要转换为(char*)的时,使用(LPTSTR)(LPCTSTR)str
然而,LPCTSTR是const char *,也就是说,得到的字符串是不可写的!将其强制转换成LPTSTR去掉const,是极为危险的!
一不留神就会完蛋!要得到char *,应该用GetBuffer()或GetBufferSetLength(),用完后再调用ReleaseBuffer()。
2、strcpy()函数
CString str;
char c[256];
strcpy(c, str);
char mychar[1024];
CString source="Hello";
strcpy((char*)&mychar,(LPCTSTR)source);
相关文章推荐
- java replaceall 用法
- STM32 定时器输入捕获注意点
- apache与nginx的默认虚拟主机的配置及作用
- Scrollviewer 无法滚动 的原因
- Scrollviewer 无法滚动 的原因
- PKU3461Oulipo
- Exchange 2013 边缘服务器配置IPBlockListProvider
- 【CSS+DIV】——基础+格局设计
- 阿里云ECS服务器挂载磁盘
- 导入android sample时遇到的问题以及解决方案
- 一维数组(C# 编程指南)
- 解决:Some projects cannot be imported because they already exist in the workspace
- python的github3模块详解
- 我的书单
- 2016年国家公务员考试公告
- GCD定时器
- Python标准库01 正则表达式(re包)
- VB.NET和C# Ruby语言之间的转换
- Android 性能优化学习笔记
- Python List Comprehensions