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

Last reviewed: August 13, 1997
Article ID: Q159423
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 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, 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

  1. 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
    
    

  2. 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:

   ARTICLE-ID: Q151774
   TITLE     : How to Call NetUserGetInfo from Visual Basic 4.0

(c) Microsoft Corporation 1996, All Rights Reserved. Contributions by Wei Hua, Microsoft Corporation
Keywords          : APrgNet vb416 VB4WIN kbhowto
Version           : 4.0
Platform          : NT WINDOWS


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 13, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.