www.pudn.com > remoteControl.zip > ipDlg.cpp


// ipDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "ip.h" 
#include "ipDlg.h" 
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
typedef struct _ASTAT_ 
{ 
    ADAPTER_STATUS adapt; 
    NAME_BUFFER    NameBuff [30]; 
}ASTAT, * PASTAT; 
 
ASTAT Adapter;  
 
///////////////////////////////////////////////////////////////////////////// 
// CIpDlg dialog 
 
CIpDlg::CIpDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CIpDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CIpDlg) 
	m_UserName = _T(""); 
	m_LocalIp = _T(""); 
	m_Mac = _T(""); 
	m_Nab = _T(""); 
	m_NetIp = _T(""); 
	m_ComName = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CIpDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CIpDlg) 
	DDX_Text(pDX, IDC_USERNAME, m_UserName); 
	DDX_Text(pDX, IDC_LOCALIP, m_LocalIp); 
 
	DDX_Text(pDX, IDC_NAB, m_Nab); 
	DDX_Text(pDX, IDC_NETIP, m_NetIp); 
	DDX_Text(pDX, IDC_COMNAME, m_ComName); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CIpDlg, CDialog) 
	//{{AFX_MSG_MAP(CIpDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_IPBTN, OnIpbtn) 
	ON_BN_CLICKED(IDC_NETIPBTN, OnNetipbtn) 
	ON_BN_CLICKED(IDC_COMNAMEBTN, OnComnamebtn) 
	ON_BN_CLICKED(IDC_USERNAMEBTN, OnUsernamebtn) 
	ON_BN_CLICKED(IDC_NABBTN, OnNabbtn) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CIpDlg message handlers 
 
BOOL CIpDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CIpDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CIpDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
/////////////////////////////////////////////////////////// 
/*/获得MAC地址 
// 输入参数:lana_num为网卡编号,一般地,从0开始, 
//但在Windows 2000中并不一定是连续分配的 
void CIpDlg::getmac_one(int lana_num) 
{ 
/*    NCB ncb; 
    UCHAR uRetCode; 
 
    memset( &ncb, 0, sizeof(ncb) ); 
    ncb.ncb_command = NCBRESET; 
    ncb.ncb_lana_num = lana_num; 
	// 指定网卡号 
 
 // 首先对选定的网卡发送一个NCBRESET命令,以便进行初始化 
    uRetCode = Netbios( &ncb ); 
    CString tempstr; 
 
    memset( &ncb, 0, sizeof(ncb) ); 
    ncb.ncb_command = NCBASTAT; 
    ncb.ncb_lana_num = lana_num; 	// 指定网卡号 
 
    strcpy( (char *)ncb.ncb_callname,"*               " ); 
    ncb.ncb_buffer = (unsigned char *) &Adapter; 
 
    // 指定返回的信息存放的变量 
    ncb.ncb_length = sizeof(Adapter); 
 
    // 接着,可以发送NCBASTAT命令以获取网卡的信息 
    uRetCode = Netbios( &ncb ); 
 
    if ( uRetCode == 0 ) 
    { 
 
        // 把网卡MAC地址格式化成常用的16进制形式,如0010-A4E4-5802 
        tempstr.Format( "第 %d 块网络适配器的地址是: %02X-%02X-%02X-%02X-%02X-%02X\n", 
                        lana_num,Adapter.adapt.adapter_address[0], 
                        Adapter.adapt.adapter_address[1],Adapter.adapt.adapter_address[2], 
                        Adapter.adapt.adapter_address[3],Adapter.adapt.adapter_address[4], 
                        Adapter.adapt.adapter_address[5]); 
        m_Mac = m_Mac + tempstr; 
 
    } 
} 
 
 
void CIpDlg::OnMacbtn()  
{ 
	// TODO: Add your control notification handler code here 
	 
	// 定义一个存放返回网卡信息的变量 
	NCB ncb; 
    UCHAR uRetCode; 
    LANA_ENUM lana_enum; 
 
    memset( &ncb, 0, sizeof(ncb) ); 
    ncb.ncb_command = NCBENUM; 
 
    ncb.ncb_buffer = (unsigned char *) &lana_enum; 
    ncb.ncb_length = sizeof(lana_enum); 
 
	// 向网卡发送NCBENUM命令,以获取当前机器的网卡信息,如有多少个网卡、每张网卡的编号等 
    uRetCode = Netbios( &ncb ); 
    CString tempstr; 
    if ( uRetCode == 0 ) 
    { 
        tempstr.Format("网络适配器的数量是: %d\n\n", lana_enum.length); 
        m_Mac = tempstr; 
        // 对每一张网卡,以其网卡编号为输入编号,获取其MAC地址 
        for ( int i=0; i< lana_enum.length; ++i) 
          getmac_one( lana_enum.lana[i]); 
     } 
} 
*/ 
 
//获得局域网IP地址 
void CIpDlg::OnIpbtn()  
{ 
	// TODO: Add your control notification handler code here 
	 WORD wVersionRequested; 
       WSADATA wsaData; 
 
      //Start up WinSock 
       wVersionRequested = MAKEWORD(1, 1); 
       WSAStartup(wVersionRequested, &wsaData); 
 
 
        hostent *p; 
        char s[128]; 
        char *p2; 
 
        //获得计算机名字 
        gethostname(s, 128); 
        p = gethostbyname(s); 
      
        //获得IP地址 
        p2 = inet_ntoa(*((in_addr *)p->h_addr)); 
        m_LocalIp =  CString(p2); 
 
        WSACleanup(); 
		UpdateData(FALSE); 
} 
 
//获得拨号网络IP地址 
void CIpDlg::OnNetipbtn()  
{ 
	// TODO: Add your control notification handler code here 
	WSAData wsaData; 
 
         
        if (WSAStartup(MAKEWORD(1,1),&wsaData)!=0) 
        { //初始化WINSOCK调用 
                m_NetIp = "WinSock版本错误。"; 
                return ; 
        } 
         
        char HostName[80]; 
        LPHOSTENT lpHostEnt; 
        struct in_addr addr[2]; 
 
        //本程序假设主机不是多宿主机,即最多只有一块网卡和一个动态IP 
        for (int I=0; I< 2; I++) 
        { 
                memset(&addr[I],0,sizeof(in_addr)); 
                //对in_addr结构清0,以利后面填写 
        } 
 
        if (gethostname(HostName,sizeof(HostName))==SOCKET_ERROR) 
        { // 得到本主机名 
                m_NetIp = "不能取得本地机器名字。"; 
                return ; 
        } 
       // m_NetIp = "您的计算机的名字是: " + CString(HostName); 
        lpHostEnt=gethostbyname(HostName);//利用得到的主机名去获得主机结构 
        if (!lpHostEnt) 
        { 
                m_NetIp = "找不到主机。"; 
                return ; 
        } 
 
        for (int i=0; lpHostEnt->h_addr_list[i]!=0; i++) 
         //从主机地址表中得到IP地址 
               memcpy(&addr[i],lpHostEnt->h_addr_list[i],sizeof(in_addr)); 
 
        if ( CString(inet_ntoa(addr[1])) == "0.0.0.0") 
       { 
                
           //     m_NetIp = "您的计算机的本地地址是: " + CString(inet_ntoa(addr[0])); 
               
                m_NetIp =  "计算机没有连接到互联网。"; 
       } 
        else 
        { 
               
          //      m_NetIp = "您的计算机的本地地址是: " + CString(inet_ntoa(addr[1])); 
            
                m_NetIp = "互联网络IP地址是: " + CString(inet_ntoa(addr[0])); 
        } 
        WSACleanup(); //释放WINSOCK调用 
		UpdateData(FALSE); 
} 
 
 
//获得计算机名称 
void CIpDlg::OnComnamebtn()  
{ 
	// TODO: Add your control notification handler code here 
	  WORD wVersionRequested; 
      WSADATA wsaData; 
      char name[255]; 
   //   CString ip; 
     // PHOSTENT hostinfo; 
      wVersionRequested = MAKEWORD( 2, 0 ); 
 
      if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) 
      { 
 
            gethostname(name, sizeof(name)); 
            WSACleanup( ); 
      }  
	  m_ComName = CString(name); 
	  UpdateData(FALSE); 
} 
 
 
//获取网络用户名称 
void CIpDlg::OnUsernamebtn()  
{ 
	// TODO: Add your control notification handler code here 
	 char username[20]; 
     unsigned long size1 = 20; 
     GetUserName(username,&size1); 
 
	 m_UserName = username; 
	 UpdateData(FALSE); 
 
} 
 
//获得网络邻居 
void CIpDlg::OnNabbtn()  
{ 
	// TODO: Add your control notification handler code here 
	LPITEMIDLIST pidlRoot = NULL; 
	SHGetSpecialFolderLocation(GetSafeHwnd(), CSIDL_NETWORK, &pidlRoot); 
 
	CString strDisplayName; 
	BROWSEINFO bi; 
	memset(&bi, 0, sizeof(BROWSEINFO)); 
	bi.hwndOwner = GetSafeHwnd(); 
	bi.pidlRoot = pidlRoot; 
	bi.pszDisplayName = strDisplayName.GetBuffer(MAX_PATH + 1); 
	bi.lpszTitle = "查找计算机"; 
	bi.ulFlags = BIF_BROWSEFORCOMPUTER; 
 
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi); 
	strDisplayName.ReleaseBuffer(); 
 
	if(pidl) 
	{ 
		m_Nab = strDisplayName; 
		UpdateData(FALSE); 
	} 
 
	IMalloc *pMalloc = NULL; 
	SHGetMalloc(&pMalloc); 
	pMalloc->Free(pidlRoot); 
 
}