NayJo
21 years ago
Hi:
I am using GetIpNetTable() to get a list of IP and MAC addresses. The
program runs fine for a few times but then GetIpNetTable() begins returning
ERROR_NO_DATA - The pipe is being closed.
I am sort of new to this but I'm assuming that some resource is not being
released but I'm not sure what that resource may be.
The program is compiled with VC++ 6.0 and is running on Windows 2000 Pro.
Many thanks in advance.
J
<code>
//
// Link with ws2_32.lib and iphlpapi.lib
//
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iphlpapi.h>
#define IP_BUFFER_SIZE 4096
char ucipTableBuffer[IP_BUFFER_SIZE];
#define FAIL 1
#define SUCCEED 0
static void DisplayErrorMessage( DWORD dwError)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
return;
}
int __cdecl main( void )
{
DWORD dwError;
ULONG ulLen;
PMIB_IPNETTABLE pIpTable = (PMIB_IPNETTABLE)ucipTableBuffer;
size_t i, j, k;
ulLen = (ULONG)IP_BUFFER_SIZE;
dwError = GetIpNetTable( pIpTable, &ulLen, TRUE);
if (dwError)
{
DisplayErrorMessage(dwError);
return 1;
}
for (i = 0; i < (size_t)pIpTable->dwNumEntries; i++)
{
char * szMac = new char[pIpTable->table[i].dwPhysAddrLen*3];
unsigned char *paddr = (unsigned char
*)&pIpTable->table[i].dwAddr;
printf("[%d]: %u.%u.%u.%u ", i, *paddr, *(paddr+1), *(paddr+2),
*(paddr+3));
//
// Convert the binary MAC address into human-readable
//
for (j = 0, k = 0; j < pIpTable->table[i].dwPhysAddrLen-1; ++j) {
k += sprintf (szMac + k, "%02X:", pIpTable->table[i].bPhysAddr[j]);
}
sprintf (szMac + k, "%02X", pIpTable->table[i].bPhysAddr[j]);
printf ("MAC address %s\n", szMac);
delete [] szMac;
}
return 0;
}
</code>
I am using GetIpNetTable() to get a list of IP and MAC addresses. The
program runs fine for a few times but then GetIpNetTable() begins returning
ERROR_NO_DATA - The pipe is being closed.
I am sort of new to this but I'm assuming that some resource is not being
released but I'm not sure what that resource may be.
The program is compiled with VC++ 6.0 and is running on Windows 2000 Pro.
Many thanks in advance.
J
<code>
//
// Link with ws2_32.lib and iphlpapi.lib
//
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iphlpapi.h>
#define IP_BUFFER_SIZE 4096
char ucipTableBuffer[IP_BUFFER_SIZE];
#define FAIL 1
#define SUCCEED 0
static void DisplayErrorMessage( DWORD dwError)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
return;
}
int __cdecl main( void )
{
DWORD dwError;
ULONG ulLen;
PMIB_IPNETTABLE pIpTable = (PMIB_IPNETTABLE)ucipTableBuffer;
size_t i, j, k;
ulLen = (ULONG)IP_BUFFER_SIZE;
dwError = GetIpNetTable( pIpTable, &ulLen, TRUE);
if (dwError)
{
DisplayErrorMessage(dwError);
return 1;
}
for (i = 0; i < (size_t)pIpTable->dwNumEntries; i++)
{
char * szMac = new char[pIpTable->table[i].dwPhysAddrLen*3];
unsigned char *paddr = (unsigned char
*)&pIpTable->table[i].dwAddr;
printf("[%d]: %u.%u.%u.%u ", i, *paddr, *(paddr+1), *(paddr+2),
*(paddr+3));
//
// Convert the binary MAC address into human-readable
//
for (j = 0, k = 0; j < pIpTable->table[i].dwPhysAddrLen-1; ++j) {
k += sprintf (szMac + k, "%02X:", pIpTable->table[i].bPhysAddr[j]);
}
sprintf (szMac + k, "%02X", pIpTable->table[i].bPhysAddr[j]);
printf ("MAC address %s\n", szMac);
delete [] szMac;
}
return 0;
}
</code>