欢迎访问 水平网    今天是:2018年02月21日 注册 | 登录 | 订阅 | 收藏
>> C/S程序开发 >> C/C++ >> 用VC列表显示所有网上邻居
推荐文章
热点文章
专题
JQuery框架
Prototype.js
HTML5

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

作者:未知,  来源:网络,  阅读:601,  发布时间:2014-09-15 【放入收藏夹
网上邻居的查找在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.lpRemoteName;
nsname += " ";
nsname +=lpnrLocal.lpLocalName;
nsname += " ";
nsname +=lpnrLocal.lpComment;
m_netList.AddString(nsname); // m_netList defined in NetEnumDlg2.h : CListBox m_netList;
if( RESOURCEUSAGE_CONTAINER ==
(lpnrLocal.dwUsage & RESOURCEUSAGE_CONTAINER) &&
lpnrLocal.dwDisplayType != RESOURCEDISPLAYTYPE_SERVER )
if( !Enumerate( &lpnrLocal) )
{
//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);
}
TGAS:VC网上邻居
评论【共有0条评论】查看所有评论
称呼:(*)   邮箱:   QQ:   验证码: 看不清楚?点击刷新验证码