枚举局域网中所有域的所有网络邻居

////////////////////////////////////////////////////////////////////////////////////////
//用途:枚举网络邻居
//参数:FirstEnum=1,开始枚举;如果返回TRUE,应该将这个参数置0,继续枚举直到返回FALSE
//     Reserve=工作组或域名
//返回:成功返回TRUE,属性NeighborName包含一个枚举出的网络邻居名称;
//     没有新的网络邻居或方法失败返回FALSE.
BOOL CCommunication::EnumNeighbor(long FirstEnum, LPCTSTR Reserve)
{
// TODO: Add your dispatch handler code here
strLastError="";
strNeighborName="";

NETRESOURCE netResource;                                //当前枚举的结果内容
HANDLE hEnum;                                           //枚举句柄
DWORD dReturn;                                          //错误的返回码

if(FirstEnum==1)                                        //第1次调用
{
NETRESOURCE nets;

ZeroMemory(&nets,sizeof(NETRESOURCE));

nets.lpRemoteName=(CHAR *)Reserve;
nets.dwDisplayType=RESOURCEDISPLAYTYPE_SERVER;
nets.dwUsage=RESOURCEUSAGE_CONTAINER;
nets.dwScope=RESOURCETYPE_DISK;

dReturn=WNetOpenEnum(RESOURCE_GLOBALNET,
RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER,
&nets,
&hEnum);

        if(dReturn!=NO_ERROR)
{
dReturn=::GetLastError();
switch(dReturn)
{
case ERROR_EXTENDED_ERROR:
strLastError="ERROR_EXTENDED_ERROR";
return FALSE;
case ERROR_NOT_CONTAINER:
strLastError="ERROR_NOT_CONTAINER";
return FALSE;
case ERROR_INVALID_PARAMETER:
strLastError="ERROR_INVALID_PARAMETER";
return FALSE;
case ERROR_NO_NETWORK:
strLastError="ERROR_NO_NETWORK";
return FALSE;
}
}

DWORD dCount=0xfffffff;                            //枚举最多的项数
        DWORD dLen=16384;                                  //分配的枚举结果内存长度16k

lpnetResource=(CHAR *)GlobalAlloc(GPTR,dLen);

dCurNeighbor=0;
dNeighborCount=0;

do
{
dReturn=WNetEnumResource(hEnum,&dCount,lpnetResource,&dLen);
if(dReturn==NO_ERROR)
{
dCurNeighbor=0;
dNeighborCount=dCount;
}
else if(dReturn!=ERROR_NO_MORE_ITEMS)
{
WNetCloseEnum(hEnum);
GlobalFree((HGLOBAL)lpnetResource);
        lpnetResource=NULL;
strLastError="枚举网络邻居失败";
return FALSE;
}
}while(dReturn!=ERROR_NO_MORE_ITEMS);
WNetCloseEnum(hEnum);

if(dNeighborCount==0)
{
GlobalFree((HGLOBAL)lpnetResource);
     lpnetResource=NULL;
strLastError="没有一个主机";
return FALSE;
}

CopyMemory(&netResource,lpnetResource+sizeof(NETRESOURCE)*dCurNeighbor,sizeof(NETRESOURCE));
dCurNeighbor++;
strNeighborName=netResource.lpRemoteName+2;  //获得连接在本机上主机的名称,+2是去掉主机名前的'\\'
return TRUE;
}
else
{
if(lpnetResource==NULL)                                      
{
strLastError="第1次调用这个方法应该将第1个参数置1";
return FALSE;
}

if(dCurNeighbor==dNeighborCount)
{
GlobalFree((HGLOBAL)lpnetResource);
     lpnetResource=NULL;
return FALSE;
}

    CopyMemory(&netResource,lpnetResource+sizeof(NETRESOURCE)*dCurNeighbor,sizeof(NETRESOURCE));
dCurNeighbor++;
strNeighborName=netResource.lpRemoteName+2;     //获得连接在本机上主机的名称
return TRUE;
}
}

////////////////////////////////////////////////////////////////////////////////////////
//用途:EnumNeighbor()枚举网络邻居成功后,返回枚举到的邻居名称
BSTR CCommunication::GetNeighborName()
{
CString strResult;
// TODO: Add your property handler here
strResult=strNeighborName;

return strResult.AllocSysString();
}

//废弃
void CCommunication::SetNeighborName(LPCTSTR lpszNewValue)
{
// TODO: Add your property handler here
}

////////////////////////////////////////////////////////////////////////////////////////
//用途:EnumDomain()枚举工作组或域成功后,返回枚举到的工作组或域名称
BSTR CCommunication::GetDomainName()
{
CString strResult;
// TODO: Add your property handler here
strResult=strDomain;

return strResult.AllocSysString();
}

//废弃
void CCommunication::SetDomainName(LPCTSTR lpszNewValue)
{
// TODO: Add your property handler here
}

////////////////////////////////////////////////////////////////////////////////////////
//用途:实际的枚举工作组或域
//参数:netResource=网络类型
//返回:成功返回TRUE,失败返回FALSE.
BOOL CCommunication::EnumTheDomain(NETRESOURCE netResource)
{
HANDLE hEnum;                                           //枚举句柄
DWORD dReturn;                                          //错误的返回码
NETRESOURCE nets;                                       //当前枚举的结果内容

//将要返回内容的结构清零
ZeroMemory(&nets,sizeof(NETRESOURCE));

//如果第一次枚举域和工作组dCurNeigborDm=0,dNeighborCountDm=0
//或者是上一个网络类型中的域和工作组枚举结束,则枚举下一个网络类型中的域和工作组;
//此时,netResource已经包含了下一个网络类型的信息
    if(dCurNeighborDm==dNeighborCountDm)
{
dReturn=WNetOpenEnum(RESOURCE_GLOBALNET,
RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER,
&netResource,
&hEnum);

        if(dReturn!=NO_ERROR)
{
dReturn=::GetLastError();
switch(dReturn)
{
case ERROR_EXTENDED_ERROR:
strLastError="ERROR_EXTENDED_ERROR";
return FALSE;
case ERROR_NOT_CONTAINER:
strLastError="ERROR_NOT_CONTAINER";
return FALSE;
case ERROR_INVALID_PARAMETER:
strLastError="ERROR_INVALID_PARAMETER";
return FALSE;
case ERROR_NO_NETWORK:
strLastError="ERROR_NO_NETWORK";
return FALSE;
}
}

DWORD dCount=0xfffffff;                            //枚举最多的项数
        DWORD dLen=16384;                                  //分配的枚举结果内存长度16k

lpnetResourceDm=(CHAR *)GlobalAlloc(GPTR,dLen);

//必要的初始化
dCurNeighborDm=0;
dNeighborCountDm=0;

do
{
dReturn=WNetEnumResource(hEnum,&dCount,lpnetResourceDm,&dLen);
if(dReturn==NO_ERROR)
{
dCurNeighborDm=0;
dNeighborCountDm=dCount;
}
else if(dReturn!=ERROR_NO_MORE_ITEMS)
{
WNetCloseEnum(hEnum);
GlobalFree((HGLOBAL)lpnetResourceDm);
lpnetResourceDm=NULL;
strLastError="枚举工作组或域失败";
return FALSE;
}
}while(dReturn!=ERROR_NO_MORE_ITEMS);
WNetCloseEnum(hEnum);
}

if(dNeighborCountDm==0)
{
GlobalFree((HGLOBAL)lpnetResourceDm);
   lpnetResourceDm=NULL;
return FALSE;
}

CopyMemory(&nets,lpnetResourceDm+sizeof(NETRESOURCE)*dCurNeighborDm,sizeof(NETRESOURCE));
dCurNeighborDm++;
strDomain=nets.lpRemoteName;    

if(dCurNeighborDm==dNeighborCountDm)
{
GlobalFree((HGLOBAL)lpnetResourceDm);
   lpnetResourceDm=NULL;
}

return TRUE;
}

////////////////////////////////////////////////////////////////////////////////////////
//用途:枚举工作组或域
//参数:FirstEnum=1,开始枚举;如果返回TRUE,应该将这个参数置0,继续枚举直到返回FALSE
//     Reserve=保留
//返回:成功返回TRUE,属性DomainName包含一个枚举出的工作组或域名称;
//     没有新的工作组或域或方法失败返回FALSE.
BOOL CCommunication::EnumDomain(long FirstEnum, LPCTSTR Reserve)
{
// TODO: Add your dispatch handler code here
strLastError="";
strDomain="";

    NETRESOURCE netResource;                                //当前枚举的网络类型的结果内容
HANDLE hEnum;                                           //枚举句柄
DWORD dReturn;                                          //错误的返回码

if(FirstEnum==1)                                        //第1次调用
{
dReturn=WNetOpenEnum(RESOURCE_GLOBALNET,
RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER,
NULL,
&hEnum);

        if(dReturn!=NO_ERROR)
{
dReturn=::GetLastError();
switch(dReturn)
{
case ERROR_EXTENDED_ERROR:
strLastError="ERROR_EXTENDED_ERROR";
return FALSE;
case ERROR_NOT_CONTAINER:
strLastError="ERROR_NOT_CONTAINER";
return FALSE;
case ERROR_INVALID_PARAMETER:
strLastError="ERROR_INVALID_PARAMETER";
return FALSE;
case ERROR_NO_NETWORK:
strLastError="ERROR_NO_NETWORK";
return FALSE;
}
}

DWORD dCount=0xfffffff;                            //枚举最多的项数
        DWORD dLen=16384;                                  //分配的枚举结果内存长度16k

lpnetResourceNk=(CHAR *)GlobalAlloc(GPTR,dLen);

dCurNeighborNk=0;
dNeighborCountNk=0;

do
{
dReturn=WNetEnumResource(hEnum,&dCount,lpnetResourceNk,&dLen);
if(dReturn==NO_ERROR)
{
dCurNeighborNk=0;
dNeighborCountNk=dCount;
}
else if(dReturn!=ERROR_NO_MORE_ITEMS)
{
WNetCloseEnum(hEnum);
GlobalFree((HGLOBAL)lpnetResourceNk);
        lpnetResourceNk=NULL;
strLastError="枚举网络类型失败";
return FALSE;
}
}while(dReturn!=ERROR_NO_MORE_ITEMS);
WNetCloseEnum(hEnum);

if(dNeighborCountNk==0)
{
GlobalFree((HGLOBAL)lpnetResourceNk);
     lpnetResourceNk=NULL;
return FALSE;
}

CopyMemory(&netResource,lpnetResourceNk+sizeof(NETRESOURCE)*dCurNeighborNk,sizeof(NETRESOURCE));
dCurNeighborNk++;

if(EnumTheDomain(netResource)==FALSE)
goto GOONENUM;
else
return TRUE;
}
else
{
GOONENUM:

if(lpnetResourceNk==NULL)                                      
{
strLastError="第1次调用这个方法应该将第1个参数置1";
return FALSE;
}

//如果网络类型当前索引和总数相同并且域或工作组当前索引和总数相同
if(dCurNeighborNk==dNeighborCountNk&&dCurNeighborDm==dNeighborCountDm)
{
GlobalFree((HGLOBAL)lpnetResourceNk);
     lpnetResourceNk=NULL;
return FALSE;
}

        //如果域或工作组当前索引和总数相同,则获取下一个网络类型的信息
if(dCurNeighborDm==dNeighborCountDm)
{
CopyMemory(&netResource,lpnetResourceNk+sizeof(NETRESOURCE)*dCurNeighborNk,sizeof(NETRESOURCE));
    dCurNeighborNk++;
}

if(EnumTheDomain(netResource)==FALSE)
goto GOONENUM;
else
return TRUE;
}
}

你可能感兴趣的:(枚举局域网中所有域的所有网络邻居)