Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
#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, // window class name
TEXT ("Ip Address"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

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 ;
DWORD i;
TCHAR szName[] = TEXT ("IP ADDRESS") ;

MIB_IPADDRTABLE *pIPAddrTable;
DWORD dwSize = 0;
DWORD dwRetVal;

//For error : GetIpAddrTable call failed
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) );


/* Get size required by GetIpAddrTable() */
//The GetIpAddrTable function retrieves the interface–to–IPv4 address mapping table.
if (GetIpAddrTable(pIPAddrTable,&dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
free( pIPAddrTable );
pIPAddrTable = (MIB_IPADDRTABLE *) malloc ( dwSize );
}

if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR )
{
TextOut(hdc,0,20,szBuffer,iLength = wsprintf(szBuffer, TEXT("GetIpAddrTable call failed with %d"),dwRetVal));
}

TextOut(hdc,0,20,szBuffer1,iLength = wsprintf(szBuffer1, TEXT("IP Address: %ld\n"), pIPAddrTable->table[0].dwAddr));
TextOut(hdc,0,40,szBuffer2,iLength = wsprintf(szBuffer2, TEXT("IP Mask: %ld\n"), pIPAddrTable->table[0].dwMask));
TextOut(hdc,0,60,szBuffer3,iLength = wsprintf(szBuffer3, TEXT("IF Index: %ld\n"), pIPAddrTable->table[0].dwIndex));
TextOut(hdc,0,80,szBuffer4,iLength = wsprintf(szBuffer4, TEXT("Broadcast Addr: %ld\n"), pIPAddrTable->table[0].dwBCastAddr));
TextOut(hdc,0,100,szBuffer5,iLength = wsprintf(szBuffer5, TEXT("Re-assembly size: %ld\n"), pIPAddrTable->table[0].dwReasmSize));

EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}


"I am getting this error "

1>------ Build started: Project: iptrace, Configuration: Debug Win32 ------
1>Compiling...
1>iptrace.cpp
1>.\iptrace.cpp(67) : warning C4101: 'i' : unreferenced local variable
1>Linking...
1>iptrace.obj : error LNK2019: unresolved external symbol _GetIpAddrTable@12 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)
1>C:\Documents and Settings\Amit\My Documents\Visual Studio 2008\Projects\iptrace\Debug\iptrace.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\Amit\My Documents\Visual Studio 2008\Projects\iptrace\iptrace\Debug\BuildLog.htm"
1>iptrace - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Posted
Updated 27-Feb-12 10:32am
v2
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 16:25pm    
This is not a question, just a code dump.
C, C++, Windows? Tag what's relevant, format the code (in "pre" tags).
--SA
amityadav4a 27-Feb-12 16:27pm    
I am getting this error "

1>------ Build started: Project: iptrace, Configuration: Debug Win32 ------
1>Compiling...
1>iptrace.cpp
1>.\iptrace.cpp(67) : warning C4101: 'i' : unreferenced local variable
1>Linking...
1>iptrace.obj : error LNK2019: unresolved external symbol _GetIpAddrTable@12 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)
1>C:\Documents and Settings\Amit\My Documents\Visual Studio 2008\Projects\iptrace\Debug\iptrace.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\Amit\My Documents\Visual Studio 2008\Projects\iptrace\iptrace\Debug\BuildLog.htm"
1>iptrace - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I am getting this error. i have posted error in the last

The linker error message is straightforward. Just look at the MSDN help page to find out which link library could be missing.

You probably did not link the library "Iphlpapi.lib". Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365949%28v=vs.85%29.aspx[^].

See also the code sample on this page.

—SA
 
Share this answer
 
Problem solved
Added #pragma comment(lib,"IPHlpApi.Lib")

in the code and now [problem is solved.

But i am not getting the correct ip address by GetIpAddrTable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900