Solved

VB - Detecting Network Name

Posted on 2004-04-21
6
862 Views
Last Modified: 2008-01-09
Hi, I need a way to find a networks domain name using visual basic. I found a way to find the local computer name but cant find the code for network name.

thanks in advance
0
Comment
Question by:Josh_Hall
6 Comments
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Have you tried
MSGBOX Environ("UserDomain")
??
0
 

Author Comment

by:Josh_Hall
Comment Utility
That brings up the domain your logged onto. I need to find the network name somehow.
0
 
LVL 19

Expert Comment

by:Shauli
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Josh_Hall
Comment Utility
hmm, thanks i had been searching for ages and didnt find that! I was hoping to get the netbios name, probably should of said that earlier.

e.g. that code gets. home.hall.com, the netbios name is HALL...
0
 
LVL 6

Accepted Solution

by:
LunaSkye earned 100 total points
Comment Utility
I have kind of a painful way of doing it..

You need to use API's WNetOpenEnum, WNetEnumResource, WNetCloseEnum.

Start out enumerating ANY resource, you will get global containers for more.. resources..
Enumerate thru these containers, looking for more containers.. if you find more, enumerate them. If you find real values, record them.

This is how you get your net name, inaddition to all computers on network etc... It all depens what you send to the API's.


*** PASTE THIS ALL INTO A MODULE ****



Option Explicit

Private Type NETRESOURCE
   dwScope As Long
   dwType As Long
   dwDisplayType As Long
   dwUsage As Long
   lpLocalName As Long
   lpRemoteName As Long
   lpComment As Long
   lpProvider As Long
End Type

Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, ByVal lpBuffer As Long, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long

Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCE_REMEMBERED = &H3

Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_UNKNOWN = &HFFFF

Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2
Private Const RESOURCEUSAGE_RESERVED = &H80000000

Private Const GMEM_FIXED = &H0
Private Const GMEM_ZEROINIT = &H40
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)

Private Declare Function GlobalAlloc Lib "KERNEL32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "KERNEL32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function CopyPointer2String Lib "KERNEL32" Alias "lstrcpyA" (ByVal NewString As String, ByVal OldString As Long) As Long

Public Function DoNetEnum(list As Object) As Boolean
   
    Dim hEnum As Long, lpBuff As Long, NR As NETRESOURCE
    Dim cbBuff As Long, cCount As Long
    Dim p As Long, res As Long, i As Long
   
    On Error Resume Next
    'test to see if list is a
    'list box type control
    list.AddItem " "
    list.Clear
    If Err.Number > 0 Then Exit Function
   
    On Error GoTo ErrorHandler
   
    'Setup the NETRESOURCE input structure.
    NR.lpRemoteName = 0
    cbBuff = 10000
    cCount = &HFFFFFFFF
   
    'Open a Net enumeration operation handle: hEnum.
    res = WNetOpenEnum(RESOURCE_GLOBALNET, _
      RESOURCETYPE_ANY, 0, NR, hEnum)
   
    If res = 0 Then
   
       'Create a buffer large enough for the results.
       '10000 bytes should be sufficient.
       lpBuff = GlobalAlloc(GPTR, cbBuff)
       'Call the enumeration function.
       res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
       If res = 0 Then
          p = lpBuff
          'WNetEnumResource fills the buffer with an array of
          'NETRESOURCE structures. Walk through the list and print
          'each local and remote name.
          For i = 1 To cCount
             ' All we get back are the Global Network Containers --- Enumerate each of these
             CopyMemory NR, ByVal p, LenB(NR)
             list.AddItem "Network Name " & _
                PointerToString(NR.lpRemoteName)
   
             DoNetEnum2 NR, list
             p = p + LenB(NR)
          Next i
          End If
    DoNetEnum = True
   
ErrorHandler:
    On Error Resume Next
       If lpBuff <> 0 Then GlobalFree (lpBuff)
       WNetCloseEnum (hEnum) 'Close the enumeration
   
    End If

End Function

Private Function PointerToString(p As Long) As String

   'The values returned in the NETRESOURCE structures are pointers to
   'ANSI strings so they need to be converted to Visual Basic Strings.

   Dim s As String
   s = String(255, Chr$(0))
   CopyPointer2String s, p
   PointerToString = Left(s, InStr(s, Chr$(0)) - 1)

End Function

Private Sub DoNetEnum2(NR As NETRESOURCE, list As Object)

   Dim hEnum As Long, lpBuff As Long
   Dim cbBuff As Long, cCount As Long
   Dim p As Long, res As Long, i As Long

   'Setup the NETRESOURCE input structure.
   cbBuff = 10000
   cCount = &HFFFFFFFF

   'Open a Net enumeration operation handle: hEnum.
   res = WNetOpenEnum(RESOURCE_GLOBALNET, _
     RESOURCETYPE_ANY, 0, NR, hEnum)

   If res = 0 Then

      'Create a buffer large enough for the results.
      '10000 bytes should be sufficient.
      lpBuff = GlobalAlloc(GPTR, cbBuff)
      'Call the enumeration function.
      res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)

      If res = 0 Then
         p = lpBuff
         'WNetEnumResource fills the buffer with an array of
         'NETRESOURCE structures. Walk through the list and print
         'each remote name.
         For i = 1 To cCount
            CopyMemory NR, ByVal p, LenB(NR)
            list.AddItem "Network Computer #" & i & " " & PointerToString(NR.lpRemoteName)
            p = p + LenB(NR)
         Next i
      End If

      If lpBuff <> 0 Then GlobalFree (lpBuff)
      WNetCloseEnum (hEnum) 'Close the enumeration

   End If

End Sub


********* PASTE THIS ALL INTO A FORM's LOAD EVENT ***************

Private Sub Form_Load()
    Call DoNetEnum(List1)
End Sub

The form should have a list box on it called "List1"
-Andrew
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
In your project set a reference to 'Windows Script Host Object Model".  Then use this code:

Private Sub Command1_Click()
    Dim objNetwork As New WshNetwork
    x = objNetwork.ComputerName
End Sub

This'll return the NetBIOS name of the machine.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now