1. 使用
int DimCount
void main()
{
LPNETRESOURCE lpNetRC_p=NULL
DimCount=0
Enumerate(lpNetRC_p)
}
2.回调函数
bool Enumerate(LPNETRESOURCE lpNetRC_p)
{
int UserCount=0
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 or RESOURCEUSAGE_CONTAINER
lpNetRC_p, // pointer to resource structure (NULL at first time)
&hEnum // handle to resource
)
if( dwResult != NO_ERROR )
{
int re=GetLastError()
return FALSE
}
DWORD dwBuffer = 16384 // 16K is reasonable size
DWORD dwEntries = 0xFFFFFFFF // enumerate all possible entries
LPNETRESOURCE lpBuffer
// first allocate buffer for NETRESOURCE structures ...
lpBuffer=(LPNETRESOURCE)GlobalAlloc(GPTR,dwBuffer )
dwResult = WNetEnumResource(hEnum,&dwEntries,lpBuffer,&dwBuffer)
if( dwResult == NO_ERROR )
{
for( register DWORD i = 0 i <dwEntriesi++)
{
// memcpy(&lpBuffer[i],&lpBuffer[i],sizeof(NETRESOURCE))
if(lpNetRC_p)
{
if(lpBuffer[i].dwDisplayType==RESOURCEDISPLAYTYPE_DOMAIN)
{
//这里就是得到获取本机 域名 或 工作组名并把它放到数组中
strcpy((char )DimInfo[DimCount++].dimName,lpBuffer[i].lpRemoteName)
}
else
{ //这里就是得到的用户名并把它放到数组中
strcpy((char )DimInfo[DimCount1].User[UserCount++].UserName,lpBuffer[i].lpRemoteName+2)
if(UserCount=20)UserCount=0
}
}
DWORD a=lpBuffer[i].dwDisplayType,c=lpBuffer[i].dwUsage
//RESOURCEUSAGE_CONTAINER == (lpBuffer[i].dwUsage & RESOURCEUSAGE_CONTAINER)
//上面的条件枚举所有的网络资源
if(a==6||a&RESOURCEDISPLAYTYPE_DOMAIN==RESOURCEDISPLAYTYPE_DOMAIN||c&RESOURCEUSAGE_CONTAINER==RESOURCEUSAGE_CONTAINER)
{
if(!Enumerate(&lpBuffer[i]))
{
return FALSE
}
//strcpy((char )User[UserCount++].UserName,lpBuffer[i].lpRemoteName+2)
//if(UserCount=20)UserCount=0
}
}
}
else if(dwResult != ERROR_NO_MORE_ITEMS)
{
return FALSE
}
if(lpBuffer) GlobalFree((HGLOBAL) lpBuffer)
WNetCloseEnum(hEnum)
return TRUE
}