I am not getting correct ip address and ip mask.
other values are correct for index, broadcast, Re assembly size.
How to get correct ip address and ip mass using GetIpAddrTable?
#pragma comment(lib,"IPHlpApi.Lib")
#pragma comment(lib, "ws2_32.lib")
#include "windows.h"
#include "iphlpapi.h" //IP Helper api
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Ip Address") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Ip Address"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
TCHAR szName[] = TEXT ("IP ADDRESS") ;
MIB_IPADDRTABLE *pIPAddrTable;
DWORD dwSize = 0;
DWORD dwRetVal;
struct in_addr inAddr;
int iLength;
TCHAR szBuffer[40],szBuffer1[40],szBuffer2[40],szBuffer3[40],szBuffer4[40],szBuffer5[40];
switch (message)
{
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
TextOut(hdc,0,1,szName,lstrlen(szName));
pIPAddrTable = (MIB_IPADDRTABLE*) malloc( sizeof(MIB_IPADDRTABLE) );
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
free( pIPAddrTable );
pIPAddrTable = (MIB_IPADDRTABLE *) malloc ( dwSize );
TextOut(hdc,0,120,szBuffer,iLength = wsprintf(szBuffer, TEXT("memory %d"),dwRetVal));
}
if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR )
{
TextOut(hdc,0,20,szBuffer,iLength = wsprintf(szBuffer, TEXT("GetIpAddrTable call failed with %d"),dwRetVal));
}
if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) == NO_ERROR )
{
TextOut(hdc,0,140,szBuffer,iLength = wsprintf(szBuffer, TEXT("no error %d"),dwRetVal));
}
memmove (&inAddr, &(pIPAddrTable->table[0].dwAddr), 4);
TextOut(hdc,0,20,szBuffer1,iLength = wsprintf(szBuffer1, TEXT("IP Address : %s "), inet_ntoa(inAddr)));
TextOut(hdc,0,40,szBuffer2,iLength = wsprintf(szBuffer2, TEXT("IP Mask : %i "), pIPAddrTable->table[0].dwMask));
TextOut(hdc,0,60,szBuffer3,iLength = wsprintf(szBuffer3, TEXT("IF Index : %i "), pIPAddrTable->table[0].dwIndex));
TextOut(hdc,0,80,szBuffer4,iLength = wsprintf(szBuffer4, TEXT("Broadcast Addr : %i "), pIPAddrTable->table[0].dwBCastAddr));
TextOut(hdc,0,100,szBuffer5,iLength = wsprintf(szBuffer5, TEXT("Re-assembly size : %i "), pIPAddrTable->table[0].dwReasmSize));
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}