c++ - 使用 WSAIoctl 函数统计网络接口(interface)(WIN32 API)

标签 c++ c winapi

我正在尝试使用 WSAIoctl 函数列出可用的接口(interface)。我必须传入一个缓冲区来保存完整列表。我想在分配内存以保存接口(interface)详细信息之前获取接口(interface)的计数,但是如果我传入 NULL 指针,调用就会失败(我没有得到返回的有效计数)。在我必须分配内存之前有什么方法可以得到这个计数?

背景是我正在 Windows 机器上启动大量进程/线程,它们都连接到单个服务器。我希望服务器将这些单独的连接视为来自不同的 IP 地址,并且我已将大量别名添加到测试机器以允许这样做(很多)。 WSAIoct 确实正确地找到了我添加的所有内容。

干杯...

最佳答案

来自msdn documentation对于 WSAIoctl:

Note: If the output buffer is not large enough to contain the address list, SOCKET_ERROR is returned as the result of this IOCTL and WSAGetLastError returns WSAEFAULT. The required size, in bytes, for the output buffer is returned in the lpcbBytesReturned parameter in this case. Note the WSAEFAULT error code is also returned if the lpvInBuffer, lpvOutBuffer, or lpcbBytesReturned parameter is not completely contained in a valid part of the user address space.

所以你必须调用 WSAIoctl 函数两次。第一次使用任意缓冲区,然后检查文档中提到的错误代码。然后使用 lpcbBytesReturned 中返回的大小来分配缓冲区并再次调用 WSAIoctl 函数。

关于c++ - 使用 WSAIoctl 函数统计网络接口(interface)(WIN32 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/862934/

相关文章:

delphi - GetTitleBarInfo 函数不返回值

c++ - 如何从.txt单词列表中打印单词?

c++ - 为什么我的字符串第二次没有正确接受输入?

C 即使套接字为空,recv() 仍继续读取

c++ Win32 Api GetMessage 线程内关闭程序

c++ - 如何知道进程是否是应用程序 - Windows

c++ - 为什么悬空指针可以继续访问对象?

c++ - 如何在不复制数据的情况下组成装饰类的层次结构

c - 重写 C 中的函数调用

c - 为什么带有 MSG_PEEK 的 recv() 会阻塞?