(一)C++中的字符串转换
char*与string的转换
void pCharToString()
{
//from char* to string
char * ch = "hello world";
string s1 = ch; //直接初始化或赋值
string s2(ch), s3;
s3 = string(ch);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
//from string to char*
string str = string("string is commonly used.");
/*************************************************************************
其实没有很大的必要将string转换成char*,因为string可以直接当成字符数组来使用,
即通过下标来访问字符元素,如str[1]表示第1个字符't'
**************************************************************************/
const char *ch1 = str.c_str();
cout << ch1 << endl;
}
char*与CString
void pCharToCString()
{
//from char* to CString
char *ch = "char pointer.";
CString cStr1 = ch;
CString cStr2 = CString(ch);
printCString(cStr1);
printCString(cStr2);
//from CString to char*
CString cstr = "CString";
char* chs=cstr.getbuffer(0);
cout << chs << endl;
}
string与CString
void stringToCString()
{
//from string to CString
string s1 = "string1 to CString";
string s2 = "string2 to CString";
string s3 = "string3 to CString";
CString cstr(s1.c_str());
printCString(cstr);
CString cstr2;
cstr2.Format("%s", s2.c_str()); // string to CString
printCString(cstr2);
cstr2.Format("%s", s3.data()); // string to CString
printCString(cstr2);
//from CString to string
CString cstr3 = "CString to string3";
CString cstr4 = "CString to string4";
string str;
str=cstr3.GetBuffer(0);
cout << str << endl;
str = LPCSTR(cstr4);
cout << str << endl;
}
c_str()和data()区别是:前者返回带'/0'的字符串,后者则返回不带'/0'的字符串.
在VS2010环境下,cstr2.Format("%s", s2.c_str());cstr2.Format("%s", s3.data());及str=cstr3.GetBuffer(0);str = LPCSTR(cstr4); 可能会编不过,会报类似error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'的错误。这是因为你的工程的字符集不是多字节字符集,将你的工程属性设置为多字节字符集即可,方法是:右键点击你的工程,选择Properties\Configurations Properties\General,在右侧的Project Defaults下的Character Set选择Use Multi-Byte Character Set。
总结
从灵活度来说,string最灵活易用,其次是CString,char*的拓展性和灵活性比较差。 一般来说在基于标准库开发时用string,在在MFC和ATL编程时用CString。
CString、string之间的转换还有其它的一些方向,但基本上都是通过char*作为桥梁,因为char*即可以方便地转换成string,也可以方便地转换成CString
(二)CreateProcess的使用
STARTUPINFO si = { sizeof(si) };
si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=SW_HIDE;
利用上面的代码可以设置创建一个隐藏的进程
CreatProcess(LPCSTR lpApplicationName,LPSTR lpCommandLine,LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcesInformation);
lpApplicationName是应用程序的名称
lpCommandLine是命令行参数
lpProcessAttributes是进程的属性
lpThreadAttributes是线程的属性
bInheritHandles 是否继承父进程的属性
dwCreationFlags 是创建标志
lpEnvironment 是环境变量
lpCurrentDirectory 是当前目录
lpStartupInfo是传给新进程的信息
lpProcessInformation是进程返回的信息
第一个参数是之间创建一个进程,第二个是通过命令行创建进程,可以发送参数
_
tmain(int argc, char * argv[])方法解读
该方法是一个控制台程序的入口程序,但是程序启动后会主动寻找该函数,并运行该函数.
该方法有2个参数.第一个是给该方法传递了几个参数,第二个是所传递的参数,比如:
notepad.exe c:\1.txt 则该方法第一个参数为2 有2个,分别是notepad.exe和c:\1.txt
(三) 关于使用CAsyncSocket的问题.
如果需要在静态库中使用MFC然后使用CAsyncSocket的话,就必须要加入下面的代码
void SocketThreadInit()
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if(pState->m_pmapSocketHandle == NULL)
pState->m_pmapSocketHandle = new CMapPtrToPtr;
if(pState->m_pmapDeadSockets == NULL)
pState->m_pmapDeadSockets = new CMapPtrToPtr;
if(pState->m_plistSocketNotifications == NULL)
pState->m_plistSocketNotifications = new CPtrList;
#endif
}
然后在CAsyncSocket的Create前调用这个方法,就能正常使用了。
如果需要在控制台程序使用CAsyncSokcet的话就必须加入下面的代码
WSADATA wsaD;
WSAStartup(MAKEWORD(2,2),&wsaD);
AfxWinInit(GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
在使用URLDownloadToFile的时候会使用浏览器缓存,有时候下载的文件是缓存,下载不了最新的.
GetSourceTextFile(CString url,CString FileName)
{
BOOL bRet=FALSE;
CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 2000); //2秒的连接超时
session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 2000); //2秒的发送超时
session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 2000); //2秒的接收超时
session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 2000); //2秒的发送超时
session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 2000); //2秒的接收超时
DWORD dwFlag = INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD ;
CHttpFile *cFile=NULL;
char *pBuf=NULL;
int nBufLen=0;
try
{
cFile=(CHttpFile*)session.OpenURL(url,1,dwFlag);
DWORD dwStatusCode;
cFile->QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode==HTTP_STATUS_OK)
{
DWORD nLen=0;
cFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,nLen);
nBufLen=nLen;
if(nLen<=0)
return bRet;
pBuf=(char*)malloc(nLen+8);
ZeroMemory(pBuf,nLen+8);
char *p=pBuf;
long nProgress = 0;
long nAverage = 100/(nBufLen/8);
while(nLen>0)
{
int n=cFile->Read(p,(nLen<RECVPACK_SIZE)?nLen:RECVPACK_SIZE);
if(n<=0)
break;
p+=n;
nLen-=n;
nProgress+=nAverage;
m_processsun.SetPos(nProgress);
}
if(nLen!=0)
return bRet;
CFile file(FileName, CFile::modeCreate | CFile::modeWrite);
file.Write(pBuf,nBufLen);
file.Close();
bRet=TRUE;
}
}
catch (CException* e)
{
e->Delete();
bRet=FALSE;
return bRet;
}
//释放内存
if(pBuf)
{
free(pBuf);
pBuf=NULL;
nBufLen=0;
}
if(cFile)
{
cFile->Close();
session.Close();
delete cFile;
}
return bRet;
}
使用上面的下载不会下载浏览器缓存的文件,可以下载最新的
分享到:
相关推荐
vc++ c++ mfc 日志 log 记录
排序问题C++MFC
Visual C++MFC编程实例 Visual C++MFC编程实例
C++ MFC学习资料整合,包含 MFC学习课件,MFC中文类库参考,MFC编程实例和《深入浅出MFC》简体版第二版,资料全面清晰,非常适合学习MFC使用,欢迎下载交流
选择本地图片,在c++ MFC中pictureCtrl控件中进行显示。
Visual C++ MFC编程实例 Visual C++ MFC Programming by Example 源码
它是一个全面的应用程序开发环境,使用它你充分利用具有面向对象特性的 C++ 来开发出专业级的 Windows 应用程序。为了能充分利用这些特性,你必须理解 C++ 程序设计语言。掌握了C++,你就必须掌握 Microsoft 基本...
从CSDN中下了不少东西,所以以一种感恩的心情上传一些东西,这本书及其源码在我初学VC时对我帮助很大,希望对朋友们有一些帮助.
用C++的MFC上添加代码,做成拥有数字计算器功能的计算器,本实例属个人制作,如有疏漏或疑问,可以联系我,内有博客地址。
Visual C++MFC编程实例 Visual C++MFC编程实例
VISUAL C++MFC扩展编程实例VISUAL C++MFC扩展编程实例VISUAL C++MFC扩展编程实例VISUAL C++MFC扩展编程实例VISUAL C++MFC扩展编程实例
mfc开发手册,实用教程,对MFC的精细剖析。
c++ MFC画图 带采样线,老师留的作业答案,我自己看不懂
Visual C++ MFC 编程实例教程,全20讲,完整版,供大家学习和提升C++ MFC编程能力
很好的关于MFC的学习资源,你可以详细的了解关于C++的MFC的可视化操作。
利用Visual C++/MFC开发Windows程序的优势 利用Visual C++/MFC开发Windows程序的优势 利用Visual C++/MFC开发Windows程序的优势
C++ MFC 绘画圆源代码,实现用鼠标绘画圆
Visual_C++MFC入门教程
C++ MFC 绘制玫瑰花,有源码,vs工程,解压后直接用vs打开即可编译运行................................
关于c++ MFC的例子