Reputation: 6870
For:
DWORD GetAdaptersInfo(
__out PIP_ADAPTER_INFO pAdapterInfo,
__inout PULONG pOutBufLen
);
The description of pOutBufLen
is as follows:
pOutBufLen
[in, out] Pointer to the size, in bytes, of the buffer indicated by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, this function fills in the buffer with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW.
Now my Question is that how can we know what should be the buffer size i.e. pOutBufLen? And what is the correct way if we have more than 16 NICs ?
Source msdn
Upvotes: 1
Views: 805
Reputation: 490108
The usual way to use it is something like this:
IP_ADAPTER_INFO *buffer= NULL;
ULONG length = 0;
// call with length of 0. It'll fail, but tell us needed size.
GetAdaptersInfo(buffer, &length);
// allocate space needed.
buffer = malloc(length);
// Call again, with necessary size.
if (buffer != NULL)
GetAdaptersInfo(bufer, &length);
At least in theory, you should really do this in a while
loop, or something on that order -- call, allocate, call again, and continue to re-allocate and re-call until it succeeds.
This will let it (eventually) succeed, even if the user happens to plug in a network adapter just between the first and second calls, so even though you allocated the space it thought would be needed, it becomes inadequate before you can make the second call.
Upvotes: 2