Parse IP addresses | CodeGuru

Parse IP addresses

Download Source Code and Example Very often, if you write Internet applications, you need to verify IP address user entered and make sure that it’s correct. So lets see how we can do that without a lot of troubles. First of all we need to add declarations of the following function to header file of […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 7, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Download Source Code and Example

Very often, if you write Internet applications, you need to verify IP address
user entered and make sure that it’s correct. So lets see how we can do that without
a lot of troubles.
First of all we need to add declarations of the following function to header file of the
dialog.

#include <winsock.h>

void WINAPI DDX_IPAddress(CDataExchange* pDX, int nIDC, UINT & value);

To implement data exchange you need to call DDX_IPAddress function from
within your dialog DoDataExchange function

Now add following code to the implementation file of the dialog, and do not forget
to include ws2_32.lib into the project or you’ll get a lot of linking errors.

// DDX routine fo IP address translation
void WINAPI DDX_IPAddress(CDataExchange* pDX, int nIDC, UINT & value)
{
	// from dialog to class ?
	if( pDX->m_bSaveAndValidate)
	{
		CString Val;
		BOOL bValid = true;

pDX->m_pDlgWnd->GetDlgItem(nIDC)->GetWindowText(Val);

for( int i = 0; i < Val.GetLength(); i++) { // let's check if all entered char in entered // IP address are digits if(Val[i] == '.') continue; if(isdigit(Val[i]) == 0) { bValid = false; break; } } if(bValid) { value = inet_addr(Val); if(value == INADDR_NONE) { pDX->m_pDlgWnd->MessageBox(“The entered IP address is invalid.”);
pDX->PrepareEditCtrl(nIDC);
pDX->Fail();
}
}
else
{
pDX->m_pDlgWnd->MessageBox(“IP address can only have digits and dots.”);
pDX->PrepareEditCtrl(nIDC);
pDX->Fail();
}
}
else
{
// if the value is a valid IP address store it in the child control
in_addr IPaddress;
memcpy(&IPaddress, &value, 4);
CString Address = inet_ntoa(IPaddress);
if(!Address.IsEmpty())
{
pDX->m_pDlgWnd->GetDlgItem(nIDC)->SetWindowText(Address);
}
}
}

Last updated: 14 May 1998

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.