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[DimCount-1].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
}
今天阿驹电脑安全就给大家粗浅地说一下如何用VC++编程获取IP以及主机名。首先告诉大家,可以通过gethostname()和gethostbyname()函数来获取计算机的主机名和IP地址。两个函数的语法格式如下:gethostname()int gethostname(char FAR * name, int namelen)name:表示返回主机名。namelen:表示字符串name的大小。获取IP地址的代码char computername[128]CString strif(gethsotname(computername,128) == 0) //获得主机名{ struct hostent * phostint i=0, j, h_len=4phost = gethostbyname(computername)m_hostname = computernamefor( j=0, j0) str+=”.”m_addr.Format(“%u”, (unsigned int)((unsigned char*)->h_addr_list[i][j] ))str+=m_addr}}m_ipaddress = strUpdataData(FALSE)欢迎分享,转载请注明来源:夏雨云
评论列表(0条)