Introduction
The CIPAddressCtrl control allows the user to enter a standard
four-field IP address:
However, the API for this control is quite limited, and only deals with four
BYTE values or a DWORD value. I decided to extend this API with functions to
allow for strings, and also a pair of functions to allow for reading/writing the
IP address to the profile (see below for more info on profile functions).
XIPAddressCtrl Functions
Here are the functions available with XIPAddressCtrl:
- GetAddress() - Retrieve the IP address
from the control and return it as a CString.
- SetAddress() - Write the dotted IP string
address to the control.
- ByteAddressToString() - Convert the four
byte fields to a dotted IP address string.
- StringAddressToByte() - Convert a dotted
IP address string to four byte fields.
- ReadProfileAddress() - Retrieve the IP
address from the profile and write it to the control.
- WriteProfileAddress() - Retrieve the IP
address from the control and write it to the profile.
Using the Profile Functions
MFC provides two mechanisms for automatically dealing with profiles. The
first mechanism is the CWinApp::SetRegistryKey() API. When
SetRegistryKey()
is used, the registry is the target of the
GetProfileInt()
, GetProfileString()
,
WriteProfileInt()
, and WriteProfileString()
member
functions of CWinApp
. The registry location will be of the form:
HKCU\Software\company name\application name\section name\value name
For
example, in the
XIPAddressCtrlTest demo,
SetRegistryKey("CodeProject")
is used, and so
WriteProfileAddress("Server", "IP")
will write the IP address to
HKCU\Software\CodeProject\XIPAddressCtrlTest\Server\IP, as shown here:
The second mechanism provided by MFC is to set up a profile
file (.ini file) as the target of the GetProfileInt()
,
GetProfileString()
, WriteProfileInt()
, and
WriteProfileString()
member functions of CWinApp
. For
example, the following code, inserted in the
CWinApp::InitInstance()
function, will set up the profile file
XIPAddressCtrlTest.ini (in the same directory as the exe):
TCHAR szProfileFilePath[MAX_PATH*2] = { 0 };
GetModuleFileName(NULL, szProfileFilePath,
sizeof(szProfileFilePath)/sizeof(TCHAR)-2);
TCHAR *cp = _tcsrchr(szProfileFilePath, _T('\\'));
if (cp)
*(cp+1) = _T('\0');
CString strProfileFilePath = szProfileFilePath;
strProfileFilePath += _T("XIPAddressCtrlTest.ini");
TRACE(_T("strProfileFilePath=<%s>\n"), strProfileFilePath);
free((void*)m_pszProfileName);
m_pszProfileName = _strdup(strProfileFilePath);
This second mechanism will produce the following
XIPAddressCtrlTest.ini file:
[Server]
IP=127.0.0.1
Since XIPAddressCtrl uses CWinApp::GetProfileString()
and
CWinApp::WriteProfileString()
, the target location of the
ReadProfileAddress()
and WriteProfileAddress()
functions will depend on which MFC mechanism is used by the app.
The Demo App
The demo app allows you to enter an IP address and stores
it in the app's profile:
Revision History
Version 1.0 - 2005 January 5
Usage
This software is released into the public domain. You are free to use it in
any way you like, except that you may not sell this source code. If you modify
it or extend it, please to consider posting new code here for everyone to share.
This software is provided "as is" with no expressed or implied warranty. I
accept no liability for any damage or loss of business that this software may
cause.