IP Control for MFC (Conversion of IP Address to String)

Keywords: MFC

(2) Fa 2 In VS, once the new project is completed, click on the toolbox ---"IP Address Control, you can drag out an IP-specific control

1. Get the IP address:

(1) Law I

Get the control's pointer first:

CIPAddressCtrl * pIP=(CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS);
BYTE nf1,nf2,nf3,nf4;
pIP->GetAddress(nf1,nf2,nf3,nf4);
CString str;
str.Format("%d.%d.%d.%d",nf1,nf2,nf3,nf4);//Here nf gets the IP value
MessageBox(str);

(2) Law II

 DWORD dw;
 pIP->GetAddress(dw);
 CString str;
 WORD hiWord=HIWORD(dw);
 WORD loWord=LOWORD(dw);
 BYTE nf1=HIBYTE(hiWord);//If not, your final output IP is reverse
 BYTE nf2=LOBYTE(hiWord);
 BYTE nf3=HIBYTE(loWord);
 BYTE nf4=LOBYTE(loWord);
 str.Format("%d.%d.%d.%d",nf1,nf2,nf3,nf4);
 MessageBox(str);

eg://Get and convert IP addresses from IPAddressCtrl to CString

    unsigned  char   *pIP; 
    CString  strIP; 
    DWORD  dwIP; 
    m_ipAddr.GetAddress(dwIP); 
    pIP  =   (unsigned   char*)&dwIP; 
    strIP.Format("%u.%u.%u.%u",*(pIP+3),   *(pIP+2),  *(pIP+1),   *pIP);

2. Set IP Address

(1) Law I

void SetAddress(BYTE nField0, BYTE nField1,BYTE nField2, BYTE nField3);
 
void SetAddress(DWORD dwAddress);

 eg1:

    pIP->SetAddress(127,0,0,1);
    pIP->SetAddress((DWORD)0x7F000001);//Both lines work the same way

eg2://Display CString IP address in IPAddressCtrl

    CString  strIP="192.168.0.10"; 
    DWORD  dwIP; 
    dwIP  =   inet_addr(strIP); 
    unsigned  char   *pIP   =  (unsigned   char*)&dwIP; 
    m_ipAddr.SetAddress(*pIP,   *(pIP+1),  *(pIP+2),   *(pIP+3));

(2) Law 2

void SetFieldRange(int nField, BYTE nLower,BYTE nUpper);

Set the range for each field: nField is a value from 0 to 3 indicating the subscript of the field; nLower and nUpper range from 0 to 255, which can be exceeded, but the control is also set back to 0 to 255 by default.

eg:

    pIP->SetFieldRange(0,5,127);//Set the range of each field, the first parameter is the subscript of the field, the second and third are the range
    pIP->SetFieldRange(1,5,127);//When the input is less than 5, such as 0, it resets to 5 when the input focus is lost
    pIP->SetFieldRange(2,5,127);//When the input is larger than 127, such as 128, it will automatically change back to 127
    pIP->SetFieldRange(3,5,127);

Or simply use the SetWindowText, GetWindowText methods, but generally what your interface does is the user enters the IP and gets it and converts it in the program, or you read the native IP and so on and display it in the form, so these two methods are rarely used, it is better to master the above usage.

//CIPAddressCtrl value converted to CString

    CString strIP="192.168.1.184";
    m_IPAddr.SetWindowText(strIP);
    CString strIP;
    m_IPAddr.GetWindowText(strIP);

//CString to CIPAddressCtrl

    CString strIP="192.168.1.184";
    DWORD dwAddress= ntohl( inet_addr(strIP));
    m_IPAddr.SetAddress(dwAddress);

//ntohl(): Convert TCP/IP network byte order format IP address of u_short type to host byte order

//inet_addr(): Converts the dotted IP address string to the IP address format in the in_addr structure

The first time I used this method, I converted IP addresses such as "127.0.0.1" through ntohl () and inet_addr () into variables of type DWORD of the IP control.

Plus: If you use an IP address, you'd better use its unique CIP Address Ctrl control. Of course, you can also use the edit box, but in this case, you will inevitably have to decide if the IP you enter is legitimate.

The criteria for determining whether an IP address is legal are as follows:

  • The string must contain three symbols'.';
  • The length of the four strings separated by the symbol'. 'must be less than or equal to 3;
  • The four strings separated by the symbol'. 'must be convertible to integers;
  • Four strings separated by the symbol'. 'must not be converted to integers greater than 255
bool IsValidIP(char *ip)
{
         std::string sip=ip;
         int pos=(int)sip.find_first_of(".");//Find the location of the first symbol'..
         if(pos==0)//If not found, return false
                   return false;
         std::string s1=sip.substr(0,pos);//s1 is the first number in the IP address
         sip=sip.substr(pos+1,sip.length()-pos);
         if(s1.length()>3)
                   return false;
         for(int i=0;i<(int)s1.length();i++)//Determine if each character is a number
         {
                   intc=s1.c_str()[i];
                   if(!isdigit(c))
                            return false;
         }
         inta=atoi(s1.c_str());//Determine whether it is between 1 and 255
         if(a<1|| a>255)
                   return false;
         pos=(int)sip.find_first_of(".");
         std::strings2=sip.substr(0,pos); //s2 is the second number in the IP address
         sip=sip.substr(pos+1,sip.length()-pos);
         if(s2.length()>3)
                   return false;
         for(int i=0;i<(int)s2.length();i++)//Determine if each character is a number
         {
                   int c=s2.c_str()[i];
                   if(!isdigit(c))
                            return false;
         }
         a=atoi(s2.c_str());
         if(a>255)//Determine whether or not it is between 0 and 255
                   return false;
         pos=(int)sip.find_first_of(".");
         std::string s3=sip.substr(0,pos); //s3 is the third number in the IP address
         sip=sip.substr(pos+1,sip.length()-pos);
         if(s3.length()>3)
                   return false;
         for(int i=0;i<(int)s3.length();i++)//Determine if each character is a number
         {
                   int c=s3.c_str()[i];
                   if(!isdigit(c))
                            return false;
         }
         a=atoi(s3.c_str());
         if(a>255)//Determine whether or not it is between 0 and 255
                   return false;
         pos=(int)sip.find_first_of(".");
         std::strings4=sip.substr(0,pos); //s4 is the fourth number in the IP address
         sip=sip.substr(pos+1,sip.length()-pos);
         if(s4.length()>3)
                   return false;
         for(int i=0;i<(int)s4.length();i++)//Determine if each character is a number
         {
                   int c=s4.c_str()[i];
                   if(!isdigit(c))
                            return false;
         }
         a=atoi(s4.c_str());
         if(a>254)//Determine whether it is between 0 and 254
                   return false;
         //After all the above checks, determine that the string is a legitimate IP address
         return true;
}

Posted by MadRhino on Mon, 13 Sep 2021 09:32:13 -0700