用VC列表显示所有网上邻居
2010-06-27 20:41:38 来源:WEB开发网网上邻居的查找在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 failedn" );
//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);
}
更多精彩
赞助商链接