📄 neighbordlg.cpp
字号:
// NeighborDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Neighbor.h"
#include "NeighborDlg.h"
#include "DirectDriver.h"
#include "LocalNameDlg.h"
#include "HostNameDlg.h"
#include "afxsock.h"
//#include "Ws2tcpip.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#include <winnetwk.h>
#pragma comment(lib, "mpr.lib")
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()
/////////////////////////////////////////////////////////////////////////////
// CNeighborDlg dialog
CNeighborDlg::CNeighborDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNeighborDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNeighborDlg)
m_NetListBox = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CNeighborDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNeighborDlg)
DDX_LBString(pDX, IDC_LIST, m_NetListBox);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNeighborDlg, CDialog)
//{{AFX_MSG_MAP(CNeighborDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_LISTBUTTON, OnListbutton)
ON_BN_CLICKED(IDC_IPBUTTON, OnIpbutton)
ON_BN_CLICKED(IDC_NAVIGATOR, OnNavigator)
ON_BN_CLICKED(IDC_REDIRECT, OnRedirect)
ON_BN_CLICKED(IDC_DISCONNECTDIRECT, OnDisconnectdirect)
ON_BN_CLICKED(IDC_IPTOHOSTNAME, OnIptohostname)
ON_BN_CLICKED(IDC_NETSEND, OnNetsend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNeighborDlg message handlers
BOOL CNeighborDlg::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
if(AfxSocketInit(NULL)==FALSE)
{
AfxMessageBox("Socket Error");
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CNeighborDlg::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 CNeighborDlg::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 CNeighborDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//-----------------------------------------------------------------------
//列取网上邻居
BOOL CNeighborDlg::Enumerate(LPNETRESOURCE lpNetRC_p,int depth)
{
HANDLE hEnum = 0;
DWORD dwScope = RESOURCE_GLOBALNET ;
DWORD dwType = RESOURCETYPE_ANY ;
DWORD dwResult = WNetOpenEnum(
dwScope, // scope of enumeration
dwType, // resource types to list
0, // enumerate all resources
lpNetRC_p, // pointer to resource structure (NULL at first time)
&hEnum // handle to resource
) ;
if( dwResult != NO_ERROR )
return FALSE;
DWORD dwBuffer = 16384 ; // 16K is reasonable size
DWORD dwEntries = -1 ; //-1, enumerate all possible entries
LPNETRESOURCE lpnrLocal = 0;
BOOL bRet = TRUE;
do
{
// first allocate buffer for NETRESOURCE structures ...
lpnrLocal = (LPNETRESOURCE) GlobalAlloc( GPTR, dwBuffer ) ;
dwResult = WNetEnumResource(
hEnum, // resource-handle
&dwEntries,
lpnrLocal,
&dwBuffer
) ;
if( dwResult == NO_ERROR )
{
for(DWORD i = 0 ; i<dwEntries;i++)
{
CString nsname;
nsname = lpnrLocal[i].lpRemoteName;
nsname += " ";
nsname +=lpnrLocal[i].lpLocalName;
nsname += " ";
nsname +=lpnrLocal[i].lpComment;
CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST);
if(depth==1) //workgroup
{
listbox->AddString(nsname);
}
if(depth==2) //user
{
listbox->AddString(" "+nsname);
m_NeighborList.AddTail(nsname);
}
m_List.AddTail(nsname+CString(depth));
if( RESOURCEUSAGE_CONTAINER ==
(lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER) )
if( !Enumerate( &lpnrLocal[i],depth+1) )
{
//TRACE0( "CNetwork::Enumerate(): recursiv call failed\n" );
//throw CNetworkBreak(FALSE);
// return FALSE;
}
}
}
else
if( dwResult != ERROR_NO_MORE_ITEMS )
{
return FALSE;
}
} while( dwResult != ERROR_NO_MORE_ITEMS );
if( lpnrLocal )
GlobalFree((HGLOBAL) lpnrLocal) ;
WNetCloseEnum(hEnum) ;
return bRet;
}
//列取网上邻居的按钮消息
void CNeighborDlg::OnListbutton()
{
CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST);
listbox->ResetContent();
BeginWaitCursor();
EnableWindow(FALSE);
Enumerate(0,0);
EndWaitCursor();
EnableWindow(TRUE);
m_List.RemoveAll();
m_NeighborList.RemoveAll();
}
BOOL CNeighborDlg::Enumerate2(LPNETRESOURCE lpnr,int depth)
{
DWORD dwResult, dwResultEnum;
HANDLE hEnum;
DWORD cbBuffer = 16384; // 16K is a good size
DWORD cEntries = -1; // enumerate all possible entries
LPNETRESOURCE lpnrLocal; // pointer to enumerated structures
DWORD i;
//
// Call the WNetOpenEnum function to begin the enumeration.
//
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
RESOURCETYPE_ANY, // all resources
0, // enumerate all resources
lpnr, // NULL first time the function is called
&hEnum); // handle to the resource
if (dwResult != NO_ERROR)
{
//
// Process errors with an application-defined error handler.
//
return FALSE;
}
//
// Call the GlobalAlloc function to allocate resources.
//
lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
do
{
//
// Initialize the buffer.
//
ZeroMemory(lpnrLocal, cbBuffer);
//
// Call the WNetEnumResource function to continue
// the enumeration.
//
dwResultEnum = WNetEnumResource(hEnum, // resource handle
&cEntries, // defined locally as -1
lpnrLocal, // LPNETRESOURCE
&cbBuffer); // buffer size
//
// If the call succeeds, loop through the structures.
//
if (dwResultEnum == NO_ERROR)
{
for(i = 0; i < cEntries; i++)
{
// Call an application-defined function to
// display the contents of the NETRESOURCE structures.
//
CString nsname;
nsname = lpnrLocal[i].lpRemoteName;
nsname += " ";
nsname +=lpnrLocal[i].lpLocalName;
nsname += " ";
nsname +=lpnrLocal[i].lpComment;
CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST);
listbox->AddString(nsname);
m_List.AddTail(nsname+CString(depth));
// If the NETRESOURCE structure represents a container resource,
// call the EnumerateFunc function recursively.
if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage
& RESOURCEUSAGE_CONTAINER))
if(!Enumerate2(&lpnrLocal[i],depth+1))
{
MessageBox("EnumerateFunc returned FALSE.");
}
}
}
// Process errors.
//
else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -