高手进---0xC0000008: An invalid handle was specified
本人新手接触VC没多长时间,最近利用异步套接字写了一个服务端,其中调用Release版中MySql接口写入数据库出现以下错误:
0xC0000008: An invalid handle was specified
Debug版出现access violation错误,将Mysql调用部分注释后则无任何问题,还请各位高手帮忙!
调用Mysql代码如下:
HRESULT CWebUnionServerDlg::OnNetEvent(WPARAM wParam, LPARAM lParam){ int iEvent = WSAGETSELECTEVENT(lParam); SOCKET CurSock = (SOCKET)wParam; switch(iEvent) { case FD_READ: { SOCKADDR_IN sockAddr; memset(&sockAddr, 0, sizeof(sockAddr)); int nSockAddrLen = sizeof(sockAddr); getpeername(CurSock, (SOCKADDR *)&sockAddr, &nSockAddrLen); int nPeerPort = ntohs(sockAddr.sin_port); CString sPeerIp = inet_ntoa(sockAddr.sin_addr); char szText[1024] = {0}; recv(CurSock, szText,1024,0); CString strItem ="["+sPeerIp+"]:" + CString(szText); AddLog(strItem); if (!sPeerIp.IsEmpty()) { LOG_MSG logMsg; strncpy(logMsg.szText,szText,strlen(szText)+1); strncpy(logMsg.ipAddr,(char *)((LPCTSTR)sPeerIp),sPeerIp.GetLength()+1); logMsg.nPort = nPeerPort; //此处发消息调用数据库接口 [color=#FF0000]PostMessage(WM_ADD_DB_LOG,0, (LPARAM)&logMsg);[/color] } } break; case FD_ACCEPT: { if (m_nClient <MAX_SOCKET) { SOCKET client = accept(CurSock,NULL,NULL); int i = WSAAsyncSelect(client, m_hWnd, NETWORK_EVENT, FD_READ|FD_WRITE|FD_CLOSE); AddClient(client); } else { MessageBox("Too much client!"); } } break; case FD_CLOSE: { RemoveClient(CurSock); closesocket(CurSock); } break; } return 0;}
HRESULT CWebUnionServerDlg::OnAddDbLog(WPARAM wParam, LPARAM lParam){ LOG_MSG * msg = (LOG_MSG *)lParam; CString strSql ; char * strStmt = "insert into sys_log (login_ip, login_port, message) values('%s', %d, '%s')"; sprintf((char *)((LPCTSTR)strSql), strStmt, msg->ipAddr, msg->nPort, msg->szText); //int i = mysql_query(m_dbOpera.m_conn, strSql); sql::Statement *stmt = m_db.GetStatment(m_conn); stmt->execute(std::string((LPCTSTR)strSql)); delete stmt; return 0;}
CString strSql; char * strStmt = "insert into sys_log (login_ip, login_port, message) values('%s', %d, '%s')"; sprintf((char *)((LPCTSTR)strSql), strStmt, msg->ipAddr, msg->nPort, msg->szText);
[解决办法]
Debug下call stack看看函数调用堆栈
[解决办法]
看call stack窗口,找到自己的出错代码。