正规dll使用共享MFC类库中的CSocket.Create莫名失败1.我没有调用AfxSocketInit(),导出的函数f()中,创建局部类CSocket cli,然后cli.Create(),应用第一次调用函数f()成功,从f()退出时,cli.Close(),第二次调用函数f()时(应用程序循环调用f()),运行到cli.Create(),立即崩溃。2.后来我尝试使用全局的CSocket cli(这样就只需要创建一次了),没想到,应用程序退出时,在析构函数中的cli.Close()处崩溃了。高手都说是AfxSocketInit()没有调用的原因,可是我不明白,
为啥1中的第一次调用时成功而在第二次崩溃了?局部的CSocket建立然后退出时销毁,我看没啥问题啊???
2中的cli.Close()崩溃的原因是啥呢?
为啥1中的第一次调用时成功而在第二次崩溃了?局部的CSocket建立然后退出时销毁,我看没啥问题啊???
2中的cli.Close()崩溃的原因是啥呢?
解决方案 »
- 新手提问:用ATL写Socket和用SDK写的区别
- spi层数据截获问题,具体涉及到lpWSPRecv 函数的数据
- 单击按钮调用某函数返回成功,直接添加代码调用调用返回失败,是什么原因?
- 在试图或者窗口上create控件,应该是重载Create还是响应WM_CREATE
- 在MFC或API中如何用C语言函数读写文件?
- 一个socket问题
- GDI+使用问题
- BMP文件中是否包含结构LPBITMAPINFO和LPBITMAPCOREINFO
- 中国人过什么圣诞节,至少我不过,ADD YOU?
- 关于windows鼠标消息和 Dinput的问题
- vc6的程序迁移vs2008后修改外观
- 获取系统中已安装的软件信息
VC6环境,自动生成了一个类,名字叫做CtestApp,然后定义了全局类theApp,CtestApp的instance成员函数调用了AfxSocketInit(),以上均是环境自动生成的代码。也就是说,我的程序中,已经调用了AfxSocketInit(),但是仍旧出现崩溃现象
int len = 4;
int request = 1, reply = 0;
//网络相关
int port = 16902;
int sock_type = SOCK_STREAM;
const char * IP = "127.0.0.1";
CSocket client; if (!client.Create()) {
return -1;
} //连接到服务器
if (!client.Connect(IP, port)) {
return -2;
} //发送数据
request = ntohl(request);
if (client.Send(&request, len) !=len) {
return -3;
} //接收数据
if (client.Receive(&reply, len) != len){
return EREAD;
} reply = htonl(reply); //关闭连接
client.Close();
}
//debug mfc静态链接
在Com的一个方法中用CSocket类
CSocket proxySK;
proxySK.create(); //这一句会出错解决方法:
在函数的开头加上以下代码来在CSocket所在的Thread中初始化CSocket和SOCKET做映射的链表:
#define _afxSockThreadState AfxGetModuleThreadState()
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
_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; 据说是一个Bug原文是这样说的
Hi, When using MFC sockets in secondary threads in a statically linked MFC
Visual C++ 6.0 application, an unhandled exception occurs. The reason for
the unhandled exception is that an object of type CMapPtrToPtr pointer,
pointed to by m_pmapSocketHandle, is never created.
To resolve it the handle maps used by the sockets need to be created for
each thread.The following code shows a function to do this:
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
}
This function should be called once in each secondary thread before the
first socket is created in the new thread. IMP : This bug was corrected in Visual Studio 6.0 Service Pack 3 Please refer to Q193101 from MSDN
就是这个博客
error C2679:binary '=':no operator found which takes a right-hand operand of type
'CMapPtrToPtr *'(or there is no acceptable conversion) 三个 = 号 赋值的语句都会报这个错误。