Click here to Skip to main content
Click here to Skip to main content

XIPAddressCtrl - an enhanced IP address control based on CIPAddressCtrl

, 5 Jan 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
XIPAddressCtrl adds string handling and profile read/write functions to CIPAddressCtrl.

Introduction

The CIPAddressCtrl control allows the user to enter a standard four-field IP address:

screenshot

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.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // GetAddress()
    //
    // Purpose:     Retrieve the IP address from the control and return it as a
    //              CString.
    //
    // Parameters:  strAddress - [out] dotted IP address; e.g., "127.0.0.1"
    //
    // Returns:     int        - number of non-blank fields in the address
    //
  • SetAddress() - Write the dotted IP string address to the control.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // SetAddress()
    //
    // Purpose:     Write the dotted IP string address to the control.
    //
    // Parameters:  lpszAddress - [in] dotted IP address; e.g., "127.0.0.1"
    //
    // Returns:     None
    //
  • ByteAddressToString() - Convert the four byte fields to a dotted IP address string.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // ByteAddressToString()
    //
    // Purpose:     Convert the four byte fields to a dotted IP address string.
    //
    // Parameters:  nField0 - [in] field 0 from a packed IP address
    //              nField1 - [in] field 1 from a packed IP address
    //              nField2 - [in] field 2 from a packed IP address
    //              nField3 - [in] field 3 from a packed IP address
    //
    // Returns:     CString - dotted IP address string; e.g., "127.0.0.1"
    //
  • StringAddressToByte() - Convert a dotted IP address string to four byte fields.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // StringAddressToByte()
    //
    // Purpose:     Convert a dotted IP address string to four byte fields.
    //
    // Parameters:  lpszAddress - [in]  dotted IP address string
    //              nField0     - [out] reference to field 0 of IP address
    //              nField1     - [out] reference to field 1 of IP address
    //              nField2     - [out] reference to field 2 of IP address
    //              nField3     - [out] reference to field 3 of IP address
    //
    // Returns:     CString     - dotted IP address string; e.g., "127.0.0.1"
    //
  • ReadProfileAddress() - Retrieve the IP address from the profile and write it to the control.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // ReadProfileAddress()
    //
    // Purpose:     Retrieve the IP address from the profile and write it to the
    //              control.
    //
    // Parameters:  lpszSection - [in] pointer to string that specifies the section
    //                            containing the entry
    //              lpszEntry   - [in] pointer to string that contains the entry
    //                            whose value is to be retrieved
    //              lpszDefault - [in] pointer to default string value for the
    //                            given entry if the entry cannot be found
    //
    // Returns:     CString     - string from the application’s profile, or
    //                            lpszDefault if the string cannot be found
    //
  • WriteProfileAddress() - Retrieve the IP address from the control and write it to the profile.
    ///////////////////////////////////////////////////////////////////////////////
    //
    // WriteProfileAddress()
    //
    // Purpose:     Retrieve the IP address from the control and write it to the
    //              profile.
    //
    // Parameters:  lpszSection - [in] pointer to string that specifies the section
    //                            containing the entry
    //              lpszEntry   - [in] pointer to string that contains the entry
    //                            whose value is to be written
    //
    // Returns:     None
    //

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:

screenshot

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):

    // INI file is in exe directory
    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);

    // save our ini file name --
    // first free the string allocated by MFC at CWinApp startup.
    // The string is allocated before InitInstance is called.
    free((void*)m_pszProfileName);
    // Change the name of the .INI file.
    // The CWinApp destructor will free the memory.
    // Note:  must be allocated on heap
    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:

screenshot

Revision History

Version 1.0 - 2005 January 5

  • Initial public release

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.

License

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

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
GeneralDownload link fixed PinmemberHans Dietrich6-Jan-05 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 5 Jan 2005
Article Copyright 2005 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid