We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Scanning Network to get computer names

strongd
strongd asked
on
Medium Priority
329 Views
Last Modified: 2010-04-30
How do I scan a network to get computer names.  Not the users logged on.  Just computer names.  
Comment
Watch Question

Commented:
use MAPI/ISAPI books.
You can use the "NET VIEW" command, redirect the output to a file, and parse through the file to get a list of names.  If I remember right (I don't have Microsoft client installed on my computer at home, so I can't verify these commands right now):

"NET VIEW \\"  - list of domains
"NET VIEW /D:domain" - list of computers in a domain
"NET VIEW" - list of computers in your computer's domain

Not the best or most efficient way, but it should work.

Commented:
I had sources to do that, but I dont' find them. Will try to find.

Commented:
NET VIEW is the MSN / NT approach. Novell networks would require NDIR or some variant.
I know you can map drives for any network (so long as the client conforms to Microsoft's requirements) using the NET command (ie., you can map Banyan drives with "NET USE K: \\StreetTalk\fs_blahblah@blah@blah").  I'd imagine the VIEW parameter would work as well...  I'll try when I go to work and see if it does.

Commented:
Use the EnumResources library function call

Download the file:

www.frogpond.com/~markg/examples/browse.zip

Implement the 32bit version. It is C code, but you should be able to follow it. Hope this helps.

Regards,
  jdyer

Commented:
I have found my project implementing in VB the retrieval of users, computers and user groups through the network.

As it is a zip file, give me your e-mail and I will send it to you or go to my web site, to download it.

waty.thierry@usa.net
http://www.geocities.com/ResearchTriangle/6311/

Author

Commented:
The link is bad.  He or she doesn't have a web page anymore.

Commented:
whose link?? Not mine, I just tried it the day I posted this to make sure!

Regards,
  jdyer

Author

Commented:
This link www.frogpond.com/~markg/examples/browse.zip does not work here.  I just tried it again.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
This is heavily commented! Hopefully that will make it easier to understand. I have not actually tested this code (That's my job for tomorrow). I have used similar code, so this SHOULD work. If you would like a copy of my test program then I will send it to you to verify that the code works... HERE IT IS:


// --THIS SHOULD BE DECLARED IN winnetwk.h (but isn't)--
#ifdef __cplusplus
      extern "C" {
      #endif

      DWORD APIENTRY WNetGetResourceParentA(
        LPNETRESOURCE lpNetResource,
        LPVOID lpBuffer,
        LPDWORD lpBufferSize);

      DWORD APIENTRY WNetGetResourceParentW(
        LPNETRESOURCE lpNetResource,
        LPVOID lpBuffer,
        LPDWORD lpBufferSize);

      #ifdef UNICODE
      #define WNetGetResourceParent  WNetGetResourceParentW
      #else
      #define WNetGetResourceParent  WNetGetResourceParentA
      #endif // !UNICODE

      #ifdef __cplusplus
      }
      #endif
// ----------------------------------------------------


// Takes a listbox as an argument and fills the listbox
//  with all of the computers in the network, also sets
//  a global static (Label) control to the workgroup name
void CListNetCompsDlg::EnumerateComputers( CListBox& lst )
{
      LPNETRESOURCE lnpLocalComp;
      LPNETRESOURCE lnpWrkGrp;
      CString strCompName;
      char compName[500];
      char workGrp[500];
      DWORD compLen;
      HANDLE hEnum;

      DWORD dwEntries;
      DWORD dwResult;
      DWORD dwSize;

      dwEntries = 0xFFFFFFFF;                  // enumerate all possible
                                            //  entries

      dwSize = 16384;                        // should be plenty of
                                    //  room to fill array of
                                    //  resources

      compLen = sizeof( compName );


      // Try to allocate some resources, post a msg on error
      lnpLocalComp = (LPNETRESOURCE)GlobalAlloc( GPTR,                                    sizeof( LPNETRESOURCE ) );

      lnpWrkGrp = (LPNETRESOURCE)GlobalAlloc( GPTR,
                  sizeof( LPNETRESOURCE ) );

      if( (lnpLocalComp == NULL ) || (lnpWrkGrp == NULL ) )
      {
            MessageBox( "Global Memory Allocation failed!" );
            SendMessage( WM_CLOSE );
            return;
      }


      // get the computer name and prepend a "\\" to it
      //  For example if the computer name is MYPC it will now
      //  be \\MYPC (this is what a universal network name
      //  looks like)
      
              GetComputerName( compName, &compLen );
      strCompName.Format( "\\\\%s", compName );

      // fill the lpRemoteName variable of the NETRESOURCE
      //  structure with our computer name, we will use it to
      //  search up in the hierarchy for the workgroup name
      lnpLocalComp->lpRemoteName = (char*)
            ((LPCTSTR)strCompName);


      // Do the up-hierarchy search for the workgroup name
      compLen = sizeof( LPNETRESOURCE );
      dwResult = WNetGetResourceParent( lnpLocalComp,
            lnpWrkGrp,
            &compLen );

      if( dwResult != WN_SUCCESS )
      {
            MessageBox( "Error getting workgroup!" );
            SendMessage( WM_CLOSE );
            return;
      }

      // Remove the leading "\\" for display
      strcpy( workGrp, lnpWrkGrp->lpRemoteName + 2 );

      sWorkGroup.SetWindowText( workGrp );

      // Delete all current entries in listbox, otherwise
      //  if this is called onRefresh then it will display
      //  doubles
      lst.ResetContent();

      // Try to open a network resource enumeration and post
      //  a message on error
      dwResult = WNetOpenEnum( RESOURCE_GLOBALNET,
                        RESOURCETYPE_ANY,
                        0,
                        lnpWrkGrp,
                        &hEnum );
      
      if( dwResult != NO_ERROR )
      {
            MessageBox( "Could not open network enumeration!" );
            SendMessage( WM_CLOSE );
            return;
      }

      GlobalFree( lnpLocalComp );

      // make sure dwResult doesn't start out as
      //   ERROR_NO_MORE_ITEMS
      dwResult = ERROR_NO_MORE_ITEMS + 1;

      while( dwResult != ERROR_NO_MORE_ITEMS )
      {
            // Allocate resources
            lnpLocalComp = (LPNETRESOURCE)GlobalAlloc( GPTR,
                                          dwSize );


            // Try enumeration, fill list box with returned
            //  values, show message on error
            dwResult = WNetEnumResource( hEnum, &dwEntries,
                  lnpLocalComp, &dwSize );

            if( dwResult == NO_ERROR )
            {
                  CString strTrimmed;      // comp names w/o leading
                                    //  "\\"
                  
                  CString strNormal;            // CString representation
                                    //  of lpRemoteName

                  for( unsigned int i = 0; i < dwEntries; i++ )
                  {
                        strNormal = lnpLocalComp[i].lpRemoteName;

                                // Trim leading "\\" for display
                        strTrimmed = strNormal.Right(
                              strNormal.GetLength() - 2 );
                        
                        lst.AddString( strTrimmed );
                  }
            }
            else if( dwResult != ERROR_NO_MORE_ITEMS )
            {
                  MessageBox( "Enumeration Error!" );
                  SendMessage( WM_CLOSE );
                  return;
            }
      }

}      

Author

Commented:
Thanks for all your help.  I will go a head and compile it as a DLL and call it will VB.  This will help a whole lot!!!!   Thanks again jdyer.  

Author

Commented:
Thanks for all your help.  I will go a head and compile it as a DLL and call it will VB.  This will help a whole lot!!!!   Thanks again jdyer.  

Commented:
make sure to #include <winnetwk.h>

I'm not sure what your experience with C++ is...

you'll need to remove the

// Remove the leading "\\" for display
      strcpy( workGrp, lnpWrkGrp->lpRemoteName + 2 );

      sWorkGroup.SetWindowText( workGrp );

otherwise the dll call will fail (most probably) since it can't find sWorkGroup. If you need more help, let me know.

Regards,
  jdyer
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.