HOWTO: Call LAN Manager Functions from 16-bit Visual Basic 4.0

ID: Q159423


The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions, 16-bit only, for Windows, version 4.0


SUMMARY

The Win32 network service APIs ported from LAN Manager are Unicode and used by Windows NT only. For Windows 95 and Windows 98, you must use the 16-bit LAN Manager functions. This article illustrates how to call the 16-bit LAN Manager functions from a Visual Basic 4.0 16-bit application.


MORE INFORMATION

The following sample application retrieves network information for a specified user. It uses two TextBoxes to collect the domain name and the user name. It then calls NetGetDCName to get the name of the Primary Domain Controller (PDC) for the specified domain. Once the application retrieves the PDC, it calls NetUserGetGroups to retrieve the list of global groups and NetUserGetInfo to retrieve the USER_INFO_10 structure. See the REFERENCES section of this article for more information.

Step-by-Step Example

  1. Start Visual Basic 4.0. If you are already running Visual Basic, choose New Project on the File menu. Form1 is created by default.


  2. Add a CommandButton, Command1, to Form1.


  3. Add two TextBoxes, Text1 and Text2, to Form1. The domain name is entered in Text1 and the user name is entered in Text2.


  4. Add a ListBox, List1, to Form1. The ListBox contains user information including the groups that the user belongs to.


  5. Add the following code to the General Declarations section of Form1:
    
          Option Explicit
          Private Declare Function LStrCpy Lib "kernel" (ByVal Dest As _
              String, ByVal Source As Any) As Integer
          Private Declare Function NetGetDCName Lib "NETAPI.DLL" ( _
              ByVal server As String, ByVal domain As String, ByVal buffer As _
              String, ByVal cbBuffer As Integer) As Integer
          Private Declare Function NetUserGetInfo Lib "NETAPI.DLL" (ByVal _
              server As String, ByVal UserName As String, ByVal level As _
              Integer, buffer As Any, ByVal cbBuffer As Integer, pcbTotal As _
              Integer) As Integer
          Private Declare Function NetUserGetGroups Lib "NETAPI.DLL" (ByVal _
              server As String, ByVal UserName As String, ByVal level As _
              Integer, ByVal buffer As String, ByVal cbBuffer As Integer, _
              cEntriesRead As Integer, cTotalAvail As Integer) As Integer
          
          Private Type USER_INFO_10
              usri10_name As String * 22
              usri10_comment As Long
              usri10_usr_comment As Long
              usri10_full_name As Long
              usri10_extraspace As String * 400
          End Type
          
          Private Type group_users_info_0
              grui0_name As String * 22
          End Type
          
          Private UserI As USER_INFO_10
          Private Group As group_users_info_0
          
          Public Function PointerToString(Pointer As Long) As String
              Dim res As Integer
              Dim buffer As String * 80
              buffer = String(80, 0)
          
              If Pointer > 0 Then
                res = LStrCpy(buffer, Pointer)
              End If
          
              PointerToString = Left(buffer, InStr(buffer, Chr(0)) - 1)
          End Function 


  6. Add the following code in the Command1_Click event procedure:
    
              Private Sub Command1_Click()
              List1.Clear
              Screen.MousePointer = vbHourglass
          
              Dim domain As String
              domain = Text1.Text
              Dim user_name As String
              user_name = Text2.Text
          
              Dim dc_name As String
              dc_name = String(22, 0)
          
          
              Dim status As Long
             'Get the name of the Domain Server
              status = NetGetDCName("", domain, dc_name, Len(dc_name))
          
              If status <> 0 Then
                  MsgBox "Domain not found."
              Else
                  'Strip off the extra characters from dc_name
                  dc_name = Left(dc_name, InStr(dc_name, Chr(0)))
          
                  Dim cRead As Integer
                  Dim total As Integer
                  Dim allGroups As String * 255
                  allGroups = String(255, 0)
                  status = NetUserGetGroups(dc_name, user_name, 0, _
                          allGroups, 255, cRead, total)
                  If (status <> 0) Then
                      MsgBox "NetUserGetGroups fails."
                      Exit Sub
                  Else
                      Dim i As Integer
                      For i = 1 To total
                          Group.grui0_name = Trim(Left(allGroups, 21))
                          List1.AddItem Group.grui0_name
                          allGroups = Mid(allGroups, 22, Len(allGroups) - 22)
                          allGroups = Trim(allGroups)
                      Next i
                  End If
          
                  status = NetUserGetInfo(dc_name, user_name, 10, _
                          UserI, Len(UserI), total)
          
                  If (status <> 0) Then
                      MsgBox "User name not found."
                      Exit Sub
                  Else
                      List1.AddItem "User Name: " & Left(UserI.usri10_name, _
                          InStr(UserI.usri10_name, Chr(0)) - 1)
                      List1.AddItem "Comment: " & _
                          PointerToString(UserI.usri10_comment)
                      List1.AddItem "User Comment: " & _
                          PointerToString(UserI.usri10_usr_comment)
                      List1.AddItem "Full Name: " & _
                          PointerToString(UserI.usri10_full_name)
                  End If
              End If
          
              Screen.MousePointer = vbNormal
          End Sub 


  7. Start the program. Enter a valid domain name and user name in the TextBoxes and click the CommandButton. The ListBox fills with the user information.



REFERENCES

The following article in the Microsoft Knowledge Base demonstrates techniques that are useful in calling most 32-bit network service APIs on Windows NT from a Visual Basic 4.0 32-bit application:

Q151774 How to Call NetUserGetInfo from Visual Basic 4.0
(c) Microsoft Corporation 1996, All Rights Reserved. Contributions by Wei Hua, Microsoft Corporation.

Additional query words: kb16bitonly kbVBp400 kbVBp kbWinOS98

Keywords : kbnetwork kbAPI kbSDKPlatform kbNetAPI kbGrpNet
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.