(50分)MFC如何获取同一局域网中主机的IP地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(50分)MFC如何获取同一局域网中主机的IP地址相关的知识,希望对你有一定的参考价值。
请告诉我方法,具体一点。使用了什么函数也说明一下。我刚学,谢谢~
说的好的追加~
如果使用VC就可以知道本局域网上所有主机的ip 方法如下:
BOOL CIPWnd::GetLanComputers(LPNETRESOURCE lpnr,HTREEITEM hNode)
DWORD dwResult,dwResultEnum,dwDsptype,dwIP;
DWORD cbBuffer = 16384; /* 16K is reasonable size */
DWORD cEntries = 0xFFFFFFFF; /* enumerate all possible entries */
LPNETRESOURCE lpnrLocal; /* pointer to enumerated structures */
char * ptr;
pcinfo * pPcinfo;
struct hostent FAR * pHostent;
HANDLE hEnum;
dwResult=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,0,lpnr,&hEnum);
if (dwResult ==67) //domain下没有可访问的主机
return TRUE;
if (dwResult != NO_ERROR )
return FALSE;
lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
dwResultEnum = WNetEnumResource(hEnum, /* resource handle */
&cEntries, /* defined locally as 0xFFFFFFFF */
lpnrLocal, /* LPNETRESOURCE */
&cbBuffer); /* buffer size */
if (dwResultEnum == NO_ERROR)
if (!g_pThread) return FALSE;
for(DWORD i = 0; i < cEntries; i++)
if (!g_pThread) return FALSE;
dwDsptype=(lpnrLocal+i)->dwDisplayType;
// RESOURCEDISPLAYTYPE_DOMAIN 0x00000001
// RESOURCEDISPLAYTYPE_SERVER 0x00000002
// RESOURCEDISPLAYTYPE_GROUP 0x00000005
// RESOURCEDISPLAYTYPE_NETWORK 0x00000006
if (dwDsptype==1||dwDsptype==2||dwDsptype==5||dwDsptype==6)
m_treeview.SendMessage(TVM_EXPAND,TVE_EXPAND,(long)hNode);
if (dwDsptype==2)
ptr=(lpnrLocal+i)->lpRemoteName+2;
pHostent = gethostbyname(ptr);
if (!g_pThread) return FALSE;
if (pHostent)
memcpy(&dwIP, pHostent->h_addr_list[0], pHostent->h_length);
pPcinfo=new pcinfo;
pPcinfo->uIP=dwIP;
pPcinfo->iflag=0;
pPcinfo->totalBytes=0;
pPcinfo->totalPacks=0;
ZeroMemory(pPcinfo->szName,MAX_HOSTNAME_LAN);
memcpy(pPcinfo->szName,ptr,strlen(ptr));
m_pcArr.Add(pPcinfo);
else
dwIP=0;
else
ptr=(lpnrLocal+i)->lpRemoteName;
dwIP=0;
HTREEITEM hNode1=m_treeview.InsertItem(ptr,hNode);
m_treeview.SetItemData(hNode1,dwIP);
if(RESOURCEUSAGE_CONTAINER == ((lpnrLocal+i)->dwUsage & RESOURCEUSAGE_CONTAINER) && (dwDsptype==1||dwDsptype==5||dwDsptype==6))
m_treeview.SendMessage(TVM_EXPAND,TVE_EXPAND,(long)hNode1);
GetLanComputers((lpnrLocal+i),hNode1);
GlobalFree((HGLOBAL) lpnrLocal);
WNetCloseEnum(hEnum);
return TRUE;
或者你去这个网查
http://www.vchelp.net/vchelp/archive.asp?type_id=39&class_id=1&cata_id=5&article_id=653 参考技术A WNetEnumResource可以用来枚举。
BOOL WINAPI EnumerateFunc(HWND hwnd,
HDC hdc,
LPNETRESOURCE lpnr)
DWORD dwResult, dwResultEnum;
HANDLE hEnum;
DWORD cbBuffer = 16384; // 16K is a good size
DWORD cEntries = -1; // enumerate all possible entries
LPNETRESOURCE lpnrLocal; // pointer to enumerated structures
DWORD i;
//
// Call the WNetOpenEnum function to begin the enumeration.
//
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
RESOURCETYPE_ANY, // all resources
0, // enumerate all resources
lpnr, // NULL first time the function is called
&hEnum); // handle to the resource
if (dwResult != NO_ERROR)
//
// Process errors with an application-defined error handler.
//
NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetOpenEnum");
return FALSE;
//
// Call the GlobalAlloc function to allocate resources.
//
lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
if (lpnrLocal == NULL)
return FALSE;
do
//
// Initialize the buffer.
//
ZeroMemory(lpnrLocal, cbBuffer);
//
// Call the WNetEnumResource function to continue
// the enumeration.
//
dwResultEnum = WNetEnumResource(hEnum, // resource handle
&cEntries, // defined locally as -1
lpnrLocal, // LPNETRESOURCE
&cbBuffer); // buffer size
//
// If the call succeeds, loop through the structures.
//
if (dwResultEnum == NO_ERROR)
for(i = 0; i < cEntries; i++)
// Call an application-defined function to
// display the contents of the NETRESOURCE structures.
//
DisplayStruct(hdc, &lpnrLocal[i]);
// If the NETRESOURCE structure represents a container resource,
// call the EnumerateFunc function recursively.
if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage
& RESOURCEUSAGE_CONTAINER))
if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i]))
TextOut(hdc, 10, 10, "EnumerateFunc returned FALSE.", 29);
// Process errors.
//
else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource");
break;
//
// End do.
//
while(dwResultEnum != ERROR_NO_MORE_ITEMS);
//
// Call the GlobalFree function to free the memory.
//
GlobalFree((HGLOBAL)lpnrLocal);
//
// Call WNetCloseEnum to end the enumeration.
//
dwResult = WNetCloseEnum(hEnum);
if(dwResult != NO_ERROR)
//
// Process errors.
//
NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum");
return FALSE;
return TRUE;
当然也可以进行IP扫描,然后将IP地址转换为主机名
参考http://topic.csdn.net/t/20061007/13/5065543.html
以上是关于(50分)MFC如何获取同一局域网中主机的IP地址的主要内容,如果未能解决你的问题,请参考以下文章