The code below will display all the ip addresses on one machine in a list box in a dgl box. If there is only one address the dlg box is not displayed (well is is but very quickly the user doesn't see it). If there is more then one ip address (more NICs) the user double clicks on the ip address he / she want to bind to. This address can be retrieve by calling GetAddress() as show below.
/////////////////////////////
//
// Using CBindAddress
//
CBindAddress* pBindAddress = new CBindAddress;
pBindAddress->DoModal();
CString local = pBindAddress->GetAddress();
delete pBindAddress;
/////////////////////////////
//
// CBindAddress dlg class
//
// BindAddress.cpp : implementation file
//
#include "stdafx.h"
#include "IPDebugTest.h"
#include "BindAddress.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBindAddress dialog
CBindAddress::CBindAddress(CWnd* pParent /*=NULL*/)
: CDialog(CBindAddress::IDD, pParent),
Address("NULL"

{
//{{AFX_DATA_INIT(CBindAddress)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CBindAddress:

oDataExchange(CDataExchange* pDX)
{
CDialog:

oDataExchange(pDX);
//{{AFX_DATA_MAP(CBindAddress)
DDX_Control(pDX, IDC_ADDRESSLIST, m_ctrlAddressList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBindAddress, CDialog)
//{{AFX_MSG_MAP(CBindAddress)
ON_LBN_DBLCLK(IDC_ADDRESSLIST, OnDblclkAddresslist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBindAddress message handlers
BOOL CBindAddress::OnInitDialog()
{
CDialog::OnInitDialog();
// check for number on NIC in machine
struct in_addr addr;
struct hostent *phe;
char ac[80];
int i;
CString csLocalAddress;
// get the name of the local host
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
printf("Error getting local host name: %d\n",WSAGetLastError());
return 1;
}
printf("Host name is %s\n",ac);
phe = gethostbyname(ac);
if (phe == 0) {
AfxMessageBox("Error: Bad host lookup."

;
return 1;
}
for (i=0; phe->h_addr_list
!=0; ++i) {
memcpy(&addr, phe->h_addr_list,sizeof(struct in_addr));
csLocalAddress = inet_ntoa(addr);
m_ctrlAddressList.AddString(csLocalAddress);
}
if(i==1){
Address = "0.0.0.0";
EndDialog(0);
}
m_ctrlAddressList.AddString("0.0.0.0"
;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CBindAddress::OnDblclkAddresslist()
{
m_ctrlAddressList.GetText(m_ctrlAddressList.GetCurSel(),Address);
EndDialog(0);
}
CString CBindAddress::GetAddress()
{
return Address;
}
Cheers,
Brother C