多线程传参问题(急啊!)
blury 2007-09-07 10:44:32 typedef struct ThreadPara
{
CString commandCode;
CString blockId;
int length;
unsigned char* dataBuff;
}THREADPARA;
void CClientSocket::OnReceive(int nErrorCode)
{
CSocket::OnReceive(nErrorCode);
if (nErrorCode == 0)
{
unsigned char m_buffer[1024];
memset(m_buffer,0,sizeof(char)*1024);
int len = 0;
len = Receive(m_buffer,1024,0);
if (len <= 0) //if(len < 12)
return;
if (m_buffer[0]==0xFF && m_buffer[1]==0x0E)
{
CString strLength = "" ,command = "";
int length = 0;
strLength = HexToStr(8,10,m_buffer);
sscanf(strLength, "%x", &length);
command = HexToAscistr(10,11,m_buffer);
if (length == 0)
{
;
}
else
{
THREADPARA* threadParam = new THREADPARA;
threadParam->blockId.Format("%d",(int)m_buffer[7]);
threadParam->length = length;
threadParam->commandCode = command;
threadParam->dataBuff = m_buffer;
CString threadD = "";
threadD = HexToStr(0,len,threadParam->dataBuff);
AfxMessageBox("threadD:"+threadD); //已经测试数据正确
AfxBeginThread(newThreadProc,(LPVOID)threadParam); //creat a new thread
}
}
}
}
UINT CClientSocket::newThreadProc(LPVOID pParam)
{
BOOL terminalIs;
THREADPARA* threadParam = (THREADPARA*)pParam;
CString strstr = HexToStr(0,66,threadParam->dataBuff);
AfxMessageBox("strstr"+strstr); //有时候前32字节及倒数2字节正确,
//有时候就前5个字节正确,后面就不只是
//什么了,奇怪的是后面的错误数据有时候几次出现都是一样的,好像不是随机的,怎么回事?哪里
//出了问题?
AfxMessageBox(threadParam->blockId); //这里数据也正确
AfxMessageBox(threadParam->commandCode); //这里数据也正确
CString idTemp = HexToStr(2,6,threadParam->dataBuff);
AfxMessageBox("idid="+idTemp); //这就不对了,即使前32字节正确时,也不对
................................
delete threadParam;
return 0; //线程关了没? 需要void AfxEndThread(0); ???
}
CString CClientSocket::HexToStr(int i,int n, unsigned char* strPara) //类静态成员函数
{
char pTemp[2050];
char pDest[2050];
memset(pTemp,0,2050);
memset(pDest,0,2050);
for(int j = i;j <n; j++)
{
sprintf(pTemp,"%02X",strPara[j]);
strcat(pDest,pTemp);
}
return pDest;
}
高手帮忙看看,急啊!!!