PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Finding Computers on a Network (DirectoryServices and API)-VBForums
Results 1 to 9 of 9

Thread: Finding Computers on a Network (DirectoryServices and API)

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Finding Computers on a Network (DirectoryServices and API)

    I had to do a ton of searching on how to do this, so I figured I would post them for others to use. These will return all the computers on your network. I am including 2 methods. One using DirectoryServices and the other using API.

    The DirectoryServices Method - This uses DirectoryServices to return the computers on your network. Just specify your network name. You may also need to add reference to System.DirectoryServices in your project as I don't believe that it is referenced by default.

    vb.net Code:
    1. Imports System.DirectoryServices
    2. Namespace SearchNetwork
    3.     Class NetworkItems
    4.         Public Function GetComputers() As String()
    5.             Dim compList As New List(Of String)
    6.             Dim dEntry As DirectoryEntry = New DirectoryEntry("LDAP://YOUR_NETWORK_NAME")
    7.             Dim dSearcher As DirectorySearcher = New DirectorySearcher(dEntry)
    8.             dSearcher.Filter = ("(objectClass=computer)")
    9.  
    10.             Dim sResult As SearchResult
    11.             For Each sResult In dSearcher.FindAll()
    12.                 compList.Add(sResult.GetDirectoryEntry().Name.ToString())
    13.             Next
    14.  
    15.             Return compList.ToArray
    16.         End Function
    17.     End Class
    18. End Namespace

    The API Method - This class is from this thread http://www.eggheadcafe.com/forumpost...umpostid=11135 and props goes to Gigemboy for finding this method and posting the link in this thread http://vbforums.com/showthread.php?t=371327

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2. Namespace Win32VB
    3.     Public Class NetApi32
    4.  
    5.         Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal BufPtr As IntPtr) As Integer
    6.  
    7.         Private Declare Unicode Function NetServerEnum Lib "netapi32" _
    8.              (ByVal Servername As IntPtr, _
    9.              ByVal Level As Integer, _
    10.              ByRef bufptr As IntPtr, _
    11.              ByVal PrefMaxLen As Integer, _
    12.              ByRef entriesread As Integer, _
    13.              ByRef TotalEntries As Integer, _
    14.              ByVal serverType As Integer, _
    15.              ByVal Domain As IntPtr, _
    16.              ByVal ResumeHandle As Integer) As Integer
    17.  
    18.         <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    19.         Public Structure SERVER_INFO_101
    20.             Dim dwPlatformID As Integer
    21.             Dim lpszServerName As String
    22.             Dim dwVersionMajor As Integer
    23.             Dim dwVersionMinor As Integer
    24.             Dim dwType As Integer
    25.             Dim lpszComment As String
    26.         End Structure
    27.  
    28.         Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Integer) As Integer
    29.  
    30.         'Windows type used to call the Net API
    31.         Private Const MAX_PREFERRED_LENGTH As Integer = -1
    32.         Private Const NERR_SUCCESS As Integer = 0
    33.         Private Const ERROR_MORE_DATA As Integer = 234
    34.         Private Const SV_TYPE_WORKSTATION As Integer = &H1S
    35.         Private Const SV_TYPE_SERVER As Integer = &H2S
    36.         Private Const SV_TYPE_SQLSERVER As Integer = &H4S
    37.         Private Const SV_TYPE_DOMAIN_CTRL As Integer = &H8S
    38.         Private Const SV_TYPE_DOMAIN_BAKCTRL As Integer = &H10S
    39.         Private Const SV_TYPE_TIME_SOURCE As Integer = &H20S
    40.         Private Const SV_TYPE_AFP As Integer = &H40S
    41.         Private Const SV_TYPE_NOVELL As Integer = &H80S
    42.         Private Const SV_TYPE_DOMAIN_MEMBER As Integer = &H100S
    43.         Private Const SV_TYPE_PRINTQ_SERVER As Integer = &H200S
    44.         Private Const SV_TYPE_DIALIN_SERVER As Integer = &H400S
    45.         Private Const SV_TYPE_XENIX_SERVER As Integer = &H800S
    46.         Private Const SV_TYPE_SERVER_UNIX As Integer = SV_TYPE_XENIX_SERVER
    47.         Private Const SV_TYPE_NT As Integer = &H1000S
    48.         Private Const SV_TYPE_WFW As Integer = &H2000S
    49.         Private Const SV_TYPE_SERVER_MFPN As Integer = &H4000S
    50.         Private Const SV_TYPE_SERVER_NT As Integer = &H8000S
    51.         Private Const SV_TYPE_POTENTIAL_BROWSER As Integer = &H10000
    52.         Private Const SV_TYPE_BACKUP_BROWSER As Integer = &H20000
    53.         Private Const SV_TYPE_MASTER_BROWSER As Integer = &H40000
    54.         Private Const SV_TYPE_DOMAIN_MASTER As Integer = &H80000
    55.         Private Const SV_TYPE_SERVER_OSF As Integer = &H100000
    56.         Private Const SV_TYPE_SERVER_VMS As Integer = &H200000
    57.         Private Const SV_TYPE_WINDOWS As Integer = &H400000  'Windows95 and above
    58.         Private Const SV_TYPE_DFS As Integer = &H800000  'Root of a DFS tree
    59.         Private Const SV_TYPE_CLUSTER_NT As Integer = &H1000000  'NT Cluster
    60.         Private Const SV_TYPE_TERMINALSERVER As Integer = &H2000000  'TerminalServer
    61.         Private Const SV_TYPE_DCE As Integer = &H10000000  'IBM DSS
    62.         Private Const SV_TYPE_ALTERNATE_XPORT As Integer = &H20000000  'rtnalternate transport
    63.         Private Const SV_TYPE_LOCAL_LIST_ONLY As Integer = &H40000000  'rtn localonly
    64.         Private Const SV_TYPE_DOMAIN_ENUM As Integer = &H80000000
    65.         Private Const SV_TYPE_ALL As Integer = &HFFFFFFFF
    66.         Private Const SV_PLATFORM_ID_OS2 As Integer = 400
    67.         Private Const SV_PLATFORM_ID_NT As Integer = 500
    68.  
    69.         'Mask applied to svX_version_major in
    70.         'order to obtain the major version number.
    71.         Private Const MAJOR_VERSION_MASK As Integer = &HFS
    72.  
    73.         <StructLayout(LayoutKind.Sequential)> Public Structure SERVER_INFO_100
    74.             Dim sv100_platform_id As Integer
    75.             Dim sv100_name As Integer
    76.         End Structure
    77.  
    78.         Public Shared Function GetAllComputersInDomain() As ArrayList
    79.             Dim bufptr As IntPtr
    80.             Dim dwEntriesread As Integer
    81.             Dim dwTotalentries As Integer
    82.             Dim dwResumehandle As Integer
    83.             Dim se101 As SERVER_INFO_101 = New SERVER_INFO_101
    84.             Dim success As Integer
    85.             Dim nStructSize As Integer
    86.             Dim cnt As Integer
    87.             nStructSize = Marshal.SizeOf(se101)
    88.  
    89.             success = NetServerEnum(IntPtr.Zero, 101, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, SV_TYPE_NT, IntPtr.Zero, dwResumehandle)
    90.             'if all goes well
    91.             Dim resSC As New ArrayList
    92.             If success = NERR_SUCCESS And success <> ERROR_MORE_DATA Then
    93.                 'loop through the returned data, adding each
    94.                 'machine to the list
    95.                 For cnt = 0 To dwEntriesread - 1
    96.                     'get one chunk of data and cast
    97.                     'into an SERVER_INFO_100 struct
    98.                     'in order to add the name to a list
    99.                     se101 = DirectCast(Marshal.PtrToStructure(New IntPtr(bufptr.ToInt32 + (nStructSize * cnt)), GetType(SERVER_INFO_101)), SERVER_INFO_101)
    100.                     resSC.Add(se101)
    101.                 Next
    102.             End If
    103.             'clean up regardless of success
    104.             Call NetApiBufferFree(bufptr)
    105.             'return entries as sign of success
    106.             Return resSC
    107.         End Function
    108.     End Class
    109. End Namespace
    Attached Files Attached Files
    Last edited by bmahler; Jun 29th, 2007 at 08:02 AM. Reason: Added Project showing usage of both examples
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0 PHP Image Gallery v2.0
    VB 2005
    Find Computers on a network Simple License Encryption SQL Server Database Access dll Use Reflection to Return Crystal ReportDocument Silently Print PDF Generic Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost) MSDN Design Guidelines API Reference Inno Setup Compiler Inno Setup Preprocessor ISTool - Fairly easy to use GUI for creating inno setup projects Connection Strings NAnt -Automated Builds Cruise Control .NET - Frontend for automated builds

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: Finding Computers on a Network (DirectoryServices and API)

    Please show some project zip file if possible. So it is easy to understand.

  3. #3

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Finding Computers on a Network (DirectoryServices and API)

    ok, I added a sample project that uses both examples.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0 PHP Image Gallery v2.0
    VB 2005
    Find Computers on a network Simple License Encryption SQL Server Database Access dll Use Reflection to Return Crystal ReportDocument Silently Print PDF Generic Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost) MSDN Design Guidelines API Reference Inno Setup Compiler Inno Setup Preprocessor ISTool - Fairly easy to use GUI for creating inno setup projects Connection Strings NAnt -Automated Builds Cruise Control .NET - Frontend for automated builds

  4. #4
    New Member
    Join Date
    Mar 2008
    Posts
    7

    Re: Finding Computers on a Network (DirectoryServices and API)

    Thank you,I have download it and will be back with the openion

  5. #5
    New Member
    Join Date
    Mar 2008
    Posts
    7

    Re: Finding Computers on a Network (DirectoryServices and API)

    I want to know,why this message appears?
    should I import any tools befor I open this Project?
    please advise

    I am using Vista

  6. #6

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Finding Computers on a Network (DirectoryServices and API)

    What version of visual studio are you trying to open the example project in. It was written with VS 2005
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0 PHP Image Gallery v2.0
    VB 2005
    Find Computers on a network Simple License Encryption SQL Server Database Access dll Use Reflection to Return Crystal ReportDocument Silently Print PDF Generic Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost) MSDN Design Guidelines API Reference Inno Setup Compiler Inno Setup Preprocessor ISTool - Fairly easy to use GUI for creating inno setup projects Connection Strings NAnt -Automated Builds Cruise Control .NET - Frontend for automated builds

  7. #7
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: Finding Computers on a Network (DirectoryServices and API)

    Hi,ur sample is very helpfull,but i have something to ask,how can i include the Gateway Address and the IPAddresses in the list too?

  8. #8
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: Finding Computers on a Network (DirectoryServices and API)

    No Answer !!!!!!!!!

  9. #9
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    Re: Finding Computers on a Network (DirectoryServices and API)

    Is it possible to get a list of computers on a wireless network (router) ?
    FOX Designs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width