关于C++ MFC中开发遇到的问题记录

(一)C++中的字符串转换
[color=blue][b]char*与string的转换[/b][/color]

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;
}

[color=blue][b]char*与CString[/b][/color]

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;
}

[color=blue][b]string与CString[/b][/color]

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。

[size=large][color=orange][b]总结[/b][/color][/size]
[color=darkred]从灵活度来说,string最灵活易用,其次是CString,char*的拓展性和灵活性比较差。 一般来说在基于标准库开发时用string,在在MFC和ATL编程时用CString。

CString、string之间的转换还有其它的一些方向,但基本上都是通过char*作为桥梁,因为char*即可以方便地转换成string,也可以方便地转换成CString
[/color]

(二)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是进程返回的信息

第一个参数是之间创建一个进程,第二个是通过命令行创建进程,可以发送参数
_[size=medium][color=orange][b]tmain(int argc, char * argv[])方法解读
该方法是一个控制台程序的入口程序,但是程序启动后会主动寻找该函数,并运行该函数.
该方法有2个参数.第一个是给该方法传递了几个参数,第二个是所传递的参数,比如:
notepad.exe c:\1.txt 则该方法第一个参数为2 有2个,分别是notepad.exe和c:\1.txt[/b][/color][/size]
(三) 关于使用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;
}

[size=medium][color=orange]使用上面的下载不会下载浏览器缓存的文件,可以下载最新的[/color][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值