hstrerror() 的替换函数

replacement function for hstrerror()

本文关键字:替换 函数 hstrerror      更新时间:2023-10-16

我正在将Linux套接字库转换为Windows,但winsock没有hstrerror()函数。 hstrerror()会拿h_errno并吐出一个错误字符串。我认为类似于斯特里夫。我环顾四周,但实际上没有人说用什么代替它。除了"它已被弃用,改用getaddrinfo()getnameinfo()"之外,我没有找到任何关于hstrerror被替换的提及。具体来说,我有一个使用它的异常类。代码如下:

//
// class socket_h_error
// subclass to record status of extern int h_errno variable
//
class socket_h_error : public socket_error {
public:
    int host_errno;
    //TODO: replace hstrerror()
    explicit socket_h_error(const string& what) :
        socket_error(what + ": " + hstrerror(h_errno)),
        host_errno(h_errno) {}
};

我确实必须更换strerror(),但我没有看到提到这是hstrerror()的可能替代品。它在 winsock2 库中也不存在,所以我没有来自 MS 的建议。

使用 WSAGetLastError(( 和 FormatMessage((。

WSAGetLastError(( 在 MSDN 中所有与 winsock 相关的函数中都明确提及。