hstrerror() 的替换函数

replacement function for hstrerror()

我正在将 linux 套接字库转换为 windows,但 winsock 没有 hstrerror() 函数。 hstrerror() 会接受 h_errno 并吐出一个错误字符串。我认为类似于strerror。我环顾四周,但实际上没有人说用什么来代替它。除了 "It's deprecated, use getaddrinfo() or getnameinfo() instead",我还没有发现任何关于 hstrerror 被替换的内容。具体来说,我有一个例外 class 使用它。代码如下:

//
// 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() and FormatMessage()

WSAGetLastError() 在 MSDN 的所有 winsock-related 函数中都有明确提及。