使用 Netapi32 库在 C 中获取网络统计信息

问题描述 投票:0回答:1

我想显示网络统计信息,但无法让此代码工作。这

NetStatisticsGet()
函数返回成功状态,但是当我打印缓冲区内容时,它在每次运行时显示相同的内容。我究竟做错了什么? Windows 官方文档不是很有帮助。

这是完整的代码:

#include <stdio.h>
#include <windows.h>
#include <lmstats.h>
#include <lmapibuf.h>

NET_API_STATUS status;
LPWSTR lpwsn = L"LanmanWorkstation";
LPBYTE lpBuf;
DWORD dwSize = 0;

int main() {
    while (1) {
        /* Print network stats at 500 ms intervals */
        status = NetStatisticsGet(NULL, lpwsn, 0, 0, &lpBuf);
        printf("lpbuf: %p\n", lpBuf);

        if (status == NERR_Success) {
            LPSTAT_WORKSTATION_0 pStats = (LPSTAT_WORKSTATION_0)lpBuf;
            NetApiBufferSize (lpBuf, &dwSize);

            for (int i = 0; i < dwSize; i++) printf("%02x", lpBuf[i]);
            printf("\n\n");
            
        } else {
            printf("status code %d\n", status);
            return 0;
        }
        Sleep(500);
    }
    NetApiBufferFree(lpBuf);
    return 0;
}

链接:

gcc netstats.c -lNetapi32

输出:

lpbuf: 0000025805c75660
432e32876ce0d901307f080000000000850200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000

lpbuf: 0000025805c74000
432e32876ce0d901307f080000000000860200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000

lpbuf: 0000025805c76160
432e32876ce0d901307f080000000000870200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000

lpbuf: 0000025805c768d0
432e32876ce0d901307f080000000000880200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000

编辑:所以我确实单独打印了字段值,但大多数字段仍然为零。

SmbsTransmitted
字段在每次迭代中都会递增 1,
UseCount
StatisticsStartTime
BytesReceived
BytesTransmitted
字段不为零,但每次迭代都保持不变,而所有其他字段保持为零。谁能帮我理解这里发生了什么?

winapi netapi32
1个回答
0
投票

正如@SimonMourier所说,使用IP Helper网络列表管理器API。有关更多信息,请参阅IP Helper 入门获取与网络适配器关联的网络配置文件

© www.soinside.com 2019 - 2024. All rights reserved.