我正在從Visual Studio 2013更新到Visual Studio 2015,並注意到這種行爲差異。爲什麼創建一個std :: runtime_error重置WSAGetLastError?
#include <stdexcept>
#include <WinSock2.h>
#include <ws2tcpip.h>
int main()
{
WORD version = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(version, &wsaData) != 0)
{
throw std::runtime_error("This one is not thrown");
}
WSASetLastError(1);
if (WSAGetLastError() != 1)
{
throw std::runtime_error("This one neither");
}
#if 1
std::runtime_error test("an error");
#endif
if (WSAGetLastError() != 1)
{
throw std::runtime_error("This is thrown when the above code path is enabled");
}
return 0;
}
當我啓用的std :: runtime_error代碼路徑WSAGetLastError標誌復位。禁用它,程序返回0沒有任何問題。
你打給'WSAStartup'的地方在哪裏?閱讀文檔:https://msdn.microsoft.com/en-us/library/windows/desktop/ms742209%28v=vs.85%29.aspx – PaulMcKenzie
我刪除了所有代碼,這些代碼不是重現問題所必需的源更多的堆棧溢出友好。我原本是在WSAStartup開始的,行爲是一樣的。 – bofjas
修正它以避免將焦點從實際問題中刪除 – bofjas