I want to display the network statistics but can't get this code to work. The
NetStatisticsGet() function returns a success status but when I print the buffer contents it displays the same thing on every run. What am I doing wrong? The official Windows docs are not very helpful.
Here's the entire code:
#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;
}
Linking:
gcc netstats.c -lNetapi32
Output:
lpbuf: 0000025805c75660
432e32876ce0d901307f080000000000850200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000
lpbuf: 0000025805c74000
432e32876ce0d901307f080000000000860200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000
lpbuf: 0000025805c76160
432e32876ce0d901307f080000000000870200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000
lpbuf: 0000025805c768d0
432e32876ce0d901307f080000000000880200000000000000000000000000000000000000000000000000000000000000000000000000009e3d08000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340000000000000000000000000000004c4d454dd800000038d08401f67f00000000000000000000
Edit: So I did print the field values individually but most of the fields are still zero. The SmbsTransmitted field keeps incrementing by one every iteration, the UseCount, StatisticsStartTime, BytesReceived, BytesTransmitted fields are nonzero but constant with every iteration whereas all other fields stay zero. Can anyone help me understand what's happening here?
As @SimonMourier said, use IP Helper and Network List Manager API. See Getting Started with IP Helper and Get network profile associated with a network adapter for more information.