gpt4 book ai didi

c++ - boost::asio::io_service 抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:35:51 28 4
gpt4 key购买 nike

好吧,我真的想不通。

我在 MSVC 中有一个 DLL 项目试图使用 Asio(来自 Boost 1.45.0),但每当我创建 io_service 时,都会抛出异常。这是我出于测试目的所做的事情:

void run()
{
boost::this_thread::sleep(boost::posix_time::seconds(5));
try
{
boost::asio::io_service io_service;
}
catch (std::exception & e)
{
MessageBox(NULL, e.what(), "Exception", MB_OK);
}
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
boost::thread thread(run);
}
return TRUE;
}

这是消息框显示的内容:

winsock: WSAStartup cannot function at this time because the underlying system it uses to provide network services is currently unavailable

这是 MSDN 对它的描述(错误代码 10091,WSASYSNOTREADY):

Network subsystem is unavailable. This error is returned by WSAStartup if the Windows Sockets implementation cannot function at because the underlying system it uses to provide network services is currently unavailable. Users should check: That the appropriate Windows Sockets DLL file is in the current path. That they are not trying to use more than one Windows Sockets implementation simultaneously. If there is more than one Winsock DLL on your system, be sure the first one in the path is appropriate for the network subsystem currently loaded. The Windows Sockets implementation documentation to be sure all necessary components are currently installed and configured correctly.

然而这些似乎都不适用于我(或者我认为如此)。

这是我的命令行:

/O2 /GL /D "_WIN32_WINNT=0x0501" /D "_WINDLL" /FD /EHsc /MD /Gy /Fo"Release\" /Fd"Release\vc90.pdb" /W3 /WX /nologo /c /TP /errorReport:prompt

如果有人知道哪里出了问题,请帮帮我!谢谢。

最佳答案

微软推荐not to call WSAStartup from DllMain ,所以最好的办法是在别处创建一个 IO 线程。

The WSAStartup function typically leads to protocol-specific helper DLLs being loaded. As a result, the WSAStartup function should not be called from the DllMain function in a application DLL.

关于c++ - boost::asio::io_service 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668306/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com