Obtain the computer name and IP address of the LAN

Source: Internet
Author: User

(1) create a dialog box-based application

 

(2) Add the code in the oninitdialog method of the dialog box.

WSADATA wsd;WSAStartup(MAKEWORD(2,2),&wsd);m_grid.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_ONECLICKACTIVATE|LVS_EX_GRIDLINES);m_grid.InsertColumn(0,"computer name",LVCFMT_LEFT,200,0);m_grid.InsertColumn(1,"IP Adress",LVCFMT_LEFT,200,0);DWORD Count=0xFFFFFFFF,Bufsize=4096,Res;NETRESOURCE* nRes;NETRESOURCE* nRes1;NETRESOURCE* nRes2;HANDLE lphEnum;LPVOID Buf=new char[4096];LPVOID Buf1=new char[4096];LPVOID Buf2=new char[4096];Res=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,NULL,&lphEnum);Res=WNetEnumResource(lphEnum,&Count,Buf,&Bufsize);nRes=(NETRESOURCE*)Buf;for (DWORD n=0;n<Count;n++,nRes++){DWORD Count1=0xFFFFFFFF;Res=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,nRes,&lphEnum);Res=WNetEnumResource(lphEnum,&Count1,Buf1,&Bufsize);nRes1=(NETRESOURCE*)Buf1;for (DWORD i=0;i<Count;i++,nRes1++){DWORD Count2=0xFFFFFFFF;Res=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,nRes1,&lphEnum);Res=WNetEnumResource(lphEnum,&Count2,Buf2,&Bufsize);nRes2=(NETRESOURCE*)Buf2;for (DWORD j=0;j<Count2;j++,nRes2++){m_grid.InsertItem(j,0);CString sName=nRes2->lpRemoteName;sName=sName.Right(sName.GetLength()-2);m_grid.SetItemText(j,0,sName);CString str="";struct hostent *pHost;pHost=gethostbyname(sName);if (pHost==NULL){m_grid.SetItemText(j,1,"fail to get IP Address");}else{for (int n=0;n<4;n++){CString addr;if (n>0){str+="";}addr.Format("%u",(unsigned int)((unsigned char*)pHost->h_addr_list[0])[n]);str+=addr;}m_grid.SetItemText(j,1,str);}}}}delete Buf;delete Buf1;delete Buf2;WNetCloseEnum(lphEnum);

(3) run the program,

Note: import the flat. Lib library and ws2_32.lib library, including afxsock. h and winnetwk. h.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.