首页
社区
课程
招聘
[讨论]C++ 多线程的一段可能导致崩溃的代码分析
发表于: 2018-1-25 11:38 6997

[讨论]C++ 多线程的一段可能导致崩溃的代码分析

2018-1-25 11:38
6997
static bool Http_GP(bool bIsHttps,
CString szGetPost,//GET POST
CString szHost , CString szPath,
CString szData , CString szCookie,
int nPort,
bool bHeader, //要头部吗
CString &strContent, CString &strErrMsg
)
{
DWORD m_Flags = 0;
CString sz_Referer;
if (bIsHttps)
{
// m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES|
m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|

INTERNET_FLAG_SECURE| //启用ssl模式

INTERNET_FLAG_IGNORE_CERT_CN_INVALID| //忽略ssl模式下的证书名称错误  
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
sz_Referer = "https://";
}else{
// m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES;
m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE;
sz_Referer = "http://";
}
CString m_Header = Ini_szHeaders;
if (Ini_bReferer)
{
sz_Referer = sz_Referer + szHost;
m_Header += "Referer: "+ sz_Referer +"/\r\n";
}
if (Ini_bX_Forwarded_For)
{
m_Header += "X-Forwarded-For: "+ RandIP() +"\r\n";
}
if (szCookie.GetLength() >10)
{
m_Header += "Cookie: ";
m_Header += szCookie+"\r\n";
}
//////////////////////////////////////////////////////////////////////////
HINTERNET m_hInternet;
HINTERNET m_hSession;
HINTERNET m_hRequest;

try {
m_hInternet = InternetOpen(Ini_szUser_Agent, INTERNET_OPEN_TYPE_PRECONFIG , NULL, NULL, 0);
if (!m_hInternet) {
strErrMsg = "Cannot open internet";//m_lastErrorCode = GetLastError();
return false;
}

m_hSession = InternetConnect(m_hInternet, szHost, nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0,0);
if (!m_hSession) {
strErrMsg = "Cannot connect to internet";//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
return false;
}
m_hRequest = HttpOpenRequest(m_hSession, szGetPost, szPath, NULL, "", NULL, m_Flags, 0);
if (!m_hRequest) {
strErrMsg = "Cannot perform http request";//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
return false;
}
}catch(...) {
strErrMsg = "Memory Exception occured";//m_lastErrorCode = GetLastError();
return false;
}
//////////////////////////////////////////////////////////////////////////
try {
if (bIsHttps)
{
DWORD dwFlags;
DWORD dwBuffLen = sizeof(dwFlags);
InternetQueryOption (m_hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
InternetSetOption (m_hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof (dwFlags) );
}
BOOL BSendRequest = FALSE;
if (szGetPost == "GET")
{
BSendRequest = HttpSendRequest(m_hRequest, m_Header, m_Header.GetLength(), NULL, 0);
}else{
BSendRequest = HttpSendRequest(m_hRequest, m_Header, m_Header.GetLength(), szData.GetBuffer(0), szData.GetLength());
}
if ( BSendRequest )
{
//return true;
}else{
switch( GetLastError() )
{
case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
{
//if (!SetClientCert()) {
strErrMsg = "Cannot perform http request, client authentication needed but couldnt detect required client certificate";
//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
//}
break;
}
case ERROR_INTERNET_INVALID_CA:
{
strErrMsg = "Cannot perform http request, client authentication needed, invalid client certificate is used";
//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
break;
}
default:
{
strErrMsg = "Cannot perform http request";
//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
break;
}
}
}
}catch(...) {
strErrMsg = "Memory Exception occured"; //m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
//strContent = "";
char buffer[1024];
memset(buffer, 0, 1024);
if (bHeader)
{
DWORD nLength = 1024;//这一句很重要,,因为HttpQueryInfo返回的时候,会改变此值
HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, buffer,(unsigned long *)&nLength, NULL);
strContent.Format("%s", buffer);
memset(buffer, 0, 1024);
}
//////////////////////////////////////////////////////////////////////////
DWORD dwNumberOfBytesRead;
BOOL result; 
while(true)
{
result = InternetReadFile(m_hRequest, buffer, 1023, &dwNumberOfBytesRead);
if (dwNumberOfBytesRead == 0 || !result)
{
break;
}
//////////////////////////////////////////////////////////////////////////
for (int i_1=0; i_1<=1023; i_1++)
{
if (buffer[i_1] == '\0')
{
buffer[i_1] = ' ';
}
}
//////////////////////////////////////////////////////////////////////////
buffer[dwNumberOfBytesRead] = '\0';
strContent += buffer;
memset(buffer, 0, 1024);
}
//////////////////////////////////////////////////////////////////////////
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return true;
}

从别的开源工程里扣来的,用来GET,POST网络数据,写了一个多线程程序中调用这个,当线程设置为几十个线程时,过一会程序就崩溃了,这个崩溃的时间不确定,也没发现规律,求大牛分析下这段代码有没什么问题


[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (11)
雪    币: 364
活跃值: (1581)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
windbg    OD分析就可以了
2018-1-25 16:57
0
雪    币: 244
活跃值: (454)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
自己都有工程代码为什么不F5挂着?
2018-1-25 20:28
0
雪    币: 4
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
互斥锁
2018-1-25 20:49
0
雪    币: 220
活跃值: (701)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
+了.lock,.unlock了
2018-1-28 11:45
0
雪    币: 2056
活跃值: (1471)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
VC6  debug  最后一个  internetCloseHandle()  会挂。release  18线程  9000任务没毛病。
2018-1-28 12:19
0
雪    币: 220
活跃值: (701)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
回youxiaxy  的话,我运行的都是release版本的,,,
2018-2-4 16:29
0
雪    币: 220
活跃值: (701)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
youxiaxy VC6 debug 最后一个 internetCloseHandle() 会挂。release 18线程 9000任务没毛病。
还有我用的是VS2010编译的
2018-2-4 16:36
0
雪    币: 23080
活跃值: (3432)
能力值: (RANK:648 )
在线值:
发帖
回帖
粉丝
9
抓的HTTP数据有没有超过1024字节?超过了的话把HTTP头  Expection  选项关掉试试
2018-2-4 19:24
0
雪    币: 249
活跃值: (14)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
调试能力难道不是程序员的基本素养?
2018-2-4 20:24
0
雪    币: 220
活跃值: (701)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
当我把最后的if  (m_hInternet)  {
InternetCloseHandle(m_hInternet);
m_hInternet  =  NULL;
}
if  (m_hSession)  {
InternetCloseHandle(m_hSession);
m_hSession  =  NULL;
}
if  (m_hRequest)  {
InternetCloseHandle(m_hRequest);
m_hRequest  =  NULL;
}
注释掉,就不崩溃了,不知道这样会影响功能么
2018-2-5 21:50
0
雪    币: 3343
活跃值: (1243)
能力值: ( LV4,RANK:40 )
在线值:
发帖
回帖
粉丝
12
我也遇到了……所以现在有比较好的解决方案吗?
2019-1-25 16:22
0
游客
登录 | 注册 方可回帖
返回
//