Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how can i get the computer name&ip in local network?

how can i get the computer name&ip in local network?

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadminhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hellin
    wrote on last edited by
    #1

    hi , how can i get all of the computer name&ip in local net work .I want to get other computer names&ip (All) ? how can i do ? do you understand me ? Can you help me ? thank you !

    T 1 Reply Last reply
    0
    • H Hellin

      hi , how can i get all of the computer name&ip in local net work .I want to get other computer names&ip (All) ? how can i do ? do you understand me ? Can you help me ? thank you !

      T Offline
      T Offline
      twing
      wrote on last edited by
      #2

      my little suggestions: char szhostname[128]; //gethostname if( gethostname(szhostname, 128) == 0 ) { // get ipaddr. struct hostent * phost; phost = gethostbyname(szhostname); } //and refer to msdn Hello World!

      H 1 Reply Last reply
      0
      • T twing

        my little suggestions: char szhostname[128]; //gethostname if( gethostname(szhostname, 128) == 0 ) { // get ipaddr. struct hostent * phost; phost = gethostbyname(szhostname); } //and refer to msdn Hello World!

        H Offline
        H Offline
        Hellin
        wrote on last edited by
        #3

        not only my computer name & ip ! all in then local network .(some groups in the network)

        T 1 Reply Last reply
        0
        • H Hellin

          not only my computer name & ip ! all in then local network .(some groups in the network)

          T Offline
          T Offline
          twing
          wrote on last edited by
          #4

          i guess it's what about WNetOpenEnum. See msdn. Hello World!

          H 1 Reply Last reply
          0
          • T twing

            i guess it's what about WNetOpenEnum. See msdn. Hello World!

            H Offline
            H Offline
            Hellin
            wrote on last edited by
            #5

            see: // TODO: Add extra validation here CString strTemp; struct hostent *host; struct in_addr *ptr; // To retrieve the IP Address DWORD dwScope = RESOURCE_CONTEXT; NETRESOURCE *NetResource = NULL; HANDLE hEnum; WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum ); WSADATA wsaData; WSAStartup(MAKEWORD(1,1),&wsaData); if ( hEnum ) { DWORD Count = 0xFFFFFFFF; DWORD BufferSize = 2048; LPVOID Buffer = new char[2048]; WNetEnumResource( hEnum, &Count, Buffer, &BufferSize ); NetResource = (NETRESOURCE*)Buffer; char szHostName[200]; unsigned int i; for ( i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ ) { if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY ) { if ( NetResource->lpRemoteName ) { CString strFullName = NetResource->lpRemoteName; if ( 0 == strFullName.Left(2).Compare("\\\\") ) strFullName = strFullName.Right( strFullName.GetLength()-2); gethostname( szHostName, strlen( szHostName ) ); host = gethostbyname(strFullName); if(host == NULL) continue; ptr = (struct in_addr *) host->h_addr_list[0]; // Eg. 211.40.35.76 split up like this. int a = ptr->S_un.S_un_b.s_b1; // 211 int b = ptr->S_un.S_un_b.s_b2; // 40 int c = ptr->S_un.S_un_b.s_b3; // 35 int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%s --> %d.%d.%d.%d", strFullName,a,b,c,d); AfxMessageBox(strTemp); } } } delete Buffer; WNetCloseEnum( hEnum ); } WSACleanup(); //******************************************* I try ,but fail ! thanks all the same !

            D 1 Reply Last reply
            0
            • H Hellin

              see: // TODO: Add extra validation here CString strTemp; struct hostent *host; struct in_addr *ptr; // To retrieve the IP Address DWORD dwScope = RESOURCE_CONTEXT; NETRESOURCE *NetResource = NULL; HANDLE hEnum; WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum ); WSADATA wsaData; WSAStartup(MAKEWORD(1,1),&wsaData); if ( hEnum ) { DWORD Count = 0xFFFFFFFF; DWORD BufferSize = 2048; LPVOID Buffer = new char[2048]; WNetEnumResource( hEnum, &Count, Buffer, &BufferSize ); NetResource = (NETRESOURCE*)Buffer; char szHostName[200]; unsigned int i; for ( i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ ) { if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY ) { if ( NetResource->lpRemoteName ) { CString strFullName = NetResource->lpRemoteName; if ( 0 == strFullName.Left(2).Compare("\\\\") ) strFullName = strFullName.Right( strFullName.GetLength()-2); gethostname( szHostName, strlen( szHostName ) ); host = gethostbyname(strFullName); if(host == NULL) continue; ptr = (struct in_addr *) host->h_addr_list[0]; // Eg. 211.40.35.76 split up like this. int a = ptr->S_un.S_un_b.s_b1; // 211 int b = ptr->S_un.S_un_b.s_b2; // 40 int c = ptr->S_un.S_un_b.s_b3; // 35 int d = ptr->S_un.S_un_b.s_b4; // 76 strTemp.Format("%s --> %d.%d.%d.%d", strFullName,a,b,c,d); AfxMessageBox(strTemp); } } } delete Buffer; WNetCloseEnum( hEnum ); } WSACleanup(); //******************************************* I try ,but fail ! thanks all the same !

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Hellin wrote: I try ,but fail ! Which tells us nothing. What exactly is failing? Are you getting a compiler/linker error. Is an assertion firing? Have you stepped through the code to see exactly what is (not) happening?


              Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

              H 1 Reply Last reply
              0
              • D David Crow

                Hellin wrote: I try ,but fail ! Which tells us nothing. What exactly is failing? Are you getting a compiler/linker error. Is an assertion firing? Have you stepped through the code to see exactly what is (not) happening?


                Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                H Offline
                H Offline
                Hellin
                wrote on last edited by
                #7

                not code Error ,I say that ,only can search my own computer name &ip ,so i say I fail to !

                D 1 Reply Last reply
                0
                • H Hellin

                  not code Error ,I say that ,only can search my own computer name &ip ,so i say I fail to !

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  What about my last question?


                  Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups