dilaogdlg.cpp

来自「显示局域网内所有电脑IP,用户名,用VC6开发,」· C++ 代码 · 共 300 行

CPP
300
字号
// dilaogDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dilaog.h"
#include "dilaogDlg.h"


//相关头文件
#include	"Winnetwk.h"
#pragma		comment(lib, "Mpr.lib ")
#include	"afxsock.h"			//同时要在link内加入ws2_32.lib



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDilaogDlg dialog

CDilaogDlg::CDilaogDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDilaogDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDilaogDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDilaogDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDilaogDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDilaogDlg, CDialog)
	//{{AFX_MSG_MAP(CDilaogDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDilaogDlg message handlers

BOOL CDilaogDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	m_list.SetExtendedStyle(LVS_EX_FLATSB
			|LVS_EX_FULLROWSELECT
			|LVS_EX_HEADERDRAGDROP
			|LVS_EX_ONECLICKACTIVATE
			|LVS_EX_GRIDLINES);
	//向表格中插入列
	m_list.InsertColumn(0,"计算机名",LVCFMT_LEFT,200,0);
	m_list.InsertColumn(1,"IP地址",LVCFMT_LEFT,200,0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDilaogDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CDilaogDlg::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 CDilaogDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


/*=============================================================
功能说明:遍历局域网内所有的电脑IP,过程等于手动访问网上邻居

原理
	从根节点向子节点层层搜索
	第一层节点
		micorsoft windows network		//重点是这个
		micorsoft terminal services
		web client network
=============================================================*/
void CDilaogDlg::OnButton1()
{
	DWORD	i,k;
	DWORD Count=0xFFFFFFFF,Bufsize=4096,Res;
 
	HANDLE  hEnum   =   0;   
	LPVOID Buf  = new char[4096*4];
	LPVOID Buf1 = new char[4096*4];
	LPVOID Buf2 = new char[4096*4];

	NETRESOURCE* nRes;
	NETRESOURCE* nRes1;
	NETRESOURCE* nRes2;

	k = 0;

	//初始化winsock,加载相关DLL
	WORD   wVersionRequested;   
	WSADATA   wsaData;   
	int   err; 
	wVersionRequested = MAKEWORD(2,2); 
	err = WSAStartup(wVersionRequested, &wsaData);   
	if(err != 0 )   
	{   
		AfxMessageBox("socket_error"); 
	}

	//初成NULL则通过,表示访问根节点
	i = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, (LPNETRESOURCE)NULL, &hEnum);
	if(i != NO_ERROR)
	{
		MessageBox("网络资源访问失败1!");
		int re = GetLastError();   
		return;
	}

	i = WNetEnumResource(hEnum,&Count,Buf,&Bufsize);

	nRes = (NETRESOURCE*)Buf;
	for(DWORD n=0; n<Count; n++,nRes++)
	{
		DWORD Count1=0xFFFFFFFF;
		Res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONTAINER,nRes,&hEnum);
		Res = WNetEnumResource(hEnum,&Count1,Buf1,&Bufsize);

		nRes1=(NETRESOURCE*)Buf1;
		for(DWORD i=0; i<Count1; i++,nRes1++)
		{
			DWORD Count2=0xFFFFFFFF;
			Res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONTAINER,nRes1,&hEnum);
			Res=WNetEnumResource(hEnum,&Count2,Buf2,&Bufsize);
			nRes2=(NETRESOURCE*)Buf2;
			for(DWORD j=0; j<Count2; j++,nRes2++,k++)
			{
				m_list.InsertItem(k, 0);		//新加一项

				CString sName = nRes2->lpRemoteName;
				sName = sName.Right(sName.GetLength()-2);

				m_list.SetItemText(k, 0, sName);

				CString str="";
				struct hostent * pHost;
				pHost = gethostbyname(sName);

				if(pHost == NULL)
					str.Insert(0, "无法得到IP地址!");
				else
				{
					//将IP变成可视的地址
					for(int n=0; n<4; n++)
					{
						CString addr;
						if(n > 0)
						{
							str += ".";
						}
						addr.Format("%u",(unsigned int)((unsigned char*)pHost->h_addr_list[0])[n]);
						str += addr;
					}
				}

				m_list.SetItemText(k, 1, str);
			}
		}
	}

	//释放相关资源
	delete Buf;
	delete Buf1;
	delete Buf2;
	WNetCloseEnum(hEnum);
}

void CDilaogDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?