VC++IP控件赋值

我在使用MFC做一个网络程序时候,要用到IP地址的控件,取值和赋值给一个CString 类型的数据,一时犯难,但还是解决了:
CIPAddressCtrl m_ipTarget; // 假设你的IP控件为m_ipTarget
CString m_szIPAddress;
// 取值到m_szIPAddress
BYTE nField0 = 0, nField1 = 0, nField2 = 0, nField3 = 0;
if(!m_ipTarget.IsBlank())
{
    m_ipTarget.GetAddress(nField0, nField1, nField2, nField3);
}
m_szIPAddress.Format(_T("%d.%d.%d.%d"), nField0, nField1, nField2, nField3);

// 赋值给m_ipTarget
int nField0 = 0, nField1 = 0, nField2 = 0, nField3 = 0;
if(_stscanf(m_szIPAddress, _T("%d.%d.%d.%d"), &nField0, &nField1, &nField2, &nField3) == 4)
{
    m_ipTarget.SetAddress(nField0, nField1, nField2, nField3);
}

同样也可以这样:

// 取值到m_szIPAddress
BYTE b[4];
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS))->GetAddress(b[0],b[1],b[2],b[3]);
m_szIPAddress.Format("%d.%d.%d.%d",b[0],b[1],b[2],b[3]);

// 赋值给m_ipTarget
//地址初始化
char name[20];
gethostname(name,20);
hostent * host=gethostbyname(name);
struct in_addr inAddr;
memmove(&inAddr,host->h_addr_list[0],4);
//或struct in_addr inAddr=(*(struct in_addr *)pHost->h_addr_list[i]);
m_sIP=inet_ntoa(inAddr);
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS))->SetAddress(inAddr.S_un.S_un_b.s_b1,inAddr.S_un.S_un_b.s_b2,inAddr.S_un.S_un_b.s_b3,inAddr.S_un.S_un_b.s_b4);

 

你可能感兴趣的:(struct,list,网络,mfc,vc++,byte)