首页 | 社区 | 博客 | 招聘 | 文章 | 新闻 | 下载 | 读书 | 代码
亲,您未登录哦! 登录 | 注册

用VC列表显示所有网上邻居

打印文章

分享到:
网上邻居的查找在www.codeguru.com上有例子,只是它
把它做成了类,本是好事,但对大家理解它的原理增加了难度,
所以现在把它实现在一对话框中,以便大家能一目了然。

#include <winnetwk.h>

#pragma comment(lib, “mpr.lib”)
BOOL CNetEnum2Dlg::Enumerate(LPNETRESOURCE lpNetRC_p)
{
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 = 0xFFFFFFFF ; // enumerate all possible entries
LPNETRESOURCE lpnrLocal = 0;

BOOL bRet = TRUE;

//try
{
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( register DWORD i = 0 ; i
{
CString nsname;
nsname = lpnrLocal[i].lpRemoteName;
nsname += " ";
nsname +=lpnrLocal[i].lpLocalName;
nsname += " ";
nsname +=lpnrLocal[i].lpComment;
m_netList.AddString(nsname); // m_netList defined in NetEnumDlg2.h : CListBox m_netList;
if( RESOURCEUSAGE_CONTAINER ==
(lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER) &&
lpnrLocal[i].dwDisplayType != RESOURCEDISPLAYTYPE_SERVER )
if( !Enumerate( &lpnrLocal[i]) )
{
//TRACE0( "CNetwork::Enumerate(): recursiv call failed\n" );
//throw CNetworkBreak(FALSE);
return FALSE;
}

}
} else if( dwResult != ERROR_NO_MORE_ITEMS )
{
//AfxMessageBox("WNetEnumResource");
return FALSE;
}
} while( dwResult != ERROR_NO_MORE_ITEMS );
}

if( lpnrLocal )
GlobalFree((HGLOBAL) lpnrLocal) ;

WNetCloseEnum(hEnum) ;

return bRet;
}

void CNetEnum2Dlg::OnListWNet()
{
Enumerate(0);

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )

编程爱好者论坛

本栏最新文章