HOWTO: Call NetUserGetInfo API from Visual Basic

ID: Q151774


The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 6.0
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0


SUMMARY

The NetUserGetInfo function is a Unicode-only Windows NT API. The last parameter of this function is a pointer to a pointer to a structure whose members contain DWORD data and pointers to Unicode strings. In order to call this function correctly from a Visual Basic application, you need to de-reference the pointer returned by the function and then you need to convert the Visual Basic string to a Unicode string and vice versa. This article illustrates these techniques in an example that calls NetUserGetInfo to retrieve a USER_INFO_3 structure from a Visual Basic application.


MORE INFORMATION

The example below uses the Win32 RtlMoveMemory function to de-reference the pointer returned by the NetUserGetInfo call. For more information on how to declare and use RtlMoveMemory function in Visual Basic, please see the following article in the Microsoft Knowledge Base:

Q129947 INFO: Win32 Replacement for the hmemecpy Function
For more information on how to convert Visual Basic string to Unicode string and vice versa, please see the following article in the Microsoft Knowledge Base:
Q145727 HOWTO: Call the Unicode Version of an API Function with VB
NOTE: In order to run the code in the example, you need to change the NT domain logon name in step 5 to your valid NT domain logon name. An invalid domain logon name will cause the application to GPF.

Step-by-Step Example

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


  2. Add a Command button, Command1, to Form1.


  3. Add the following code to the General Declarations section of Form1:


  4. 
    ' definitions not specifically declared in the article:
    
    ' the servername and username params can also be declared as Longs,
    ' and passed Unicode memory addresses with the StrPtr function.
    Declare Function NetUserGetInfo Lib "netapi32" _
                                  (ByVal servername As String, _
                                  ByVal username As String, _
                                  ByVal level As Long, _
                                  bufptr As Long) As Long
    
    Public Const NERR_Success = 0
    
    Declare Sub MoveMemory Lib "kernel32" Alias _
           "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    
    Declare Function lstrlenW Lib "kernel32" (lpString As Any) As Long
    
    ' Converts a Unicode string to an ANSI string
    ' Specify -1 for cchWideChar and 0 for cchMultiByte to return string length.
    Declare Function WideCharToMultiByte Lib "kernel32" _
                                (ByVal codepage As Long, _
                                ByVal dwFlags As Long, _
                                lpWideCharStr As Any, _
                                ByVal cchWideChar As Long, _
                                lpMultiByteStr As Any, _
                                ByVal cchMultiByte As Long, _
                                ByVal lpDefaultChar As String, _
                                ByVal lpUsedDefaultChar As Long) As Long
    ' CodePage
    Public Const CP_ACP = 0        ' ANSI code page
    
    Private Sub Command1_Click()
      Dim lpBuf As Long
      Dim ui3 As USER_INFO_3
    
      ' replace "Administrator" with a valid NT username
      If (NetUserGetInfo("", StrConv("Administrator", vbUnicode), 3, lpBuf) = NERR_Success) Then
        Call MoveMemory(ui3, ByVal lpBuf, Len(ui3))
    
        MsgBox GetStrFromPtrW(ui3.usri3_name)
    
        Call NetApiBufferFree(ByVal lpBuf)
      End If
    
    End Sub
    
    ' Returns an ANSI string from a pointer to a Unicode string.
    
    Public Function GetStrFromPtrW(lpszW As Long) As String
      Dim sRtn As String
    
      sRtn = String$(lstrlenW(ByVal lpszW) * 2, 0)   ' 2 bytes/char
    
    ' WideCharToMultiByte also returns Unicode string length
    '  sRtn = String$(WideCharToMultiByte(CP_ACP, 0, ByVal lpszW, -1, 0, 0, 0, 0), 0)
    
      Call WideCharToMultiByte(CP_ACP, 0, ByVal lpszW, -1, ByVal sRtn, Len(sRtn), 0, 0)
      GetStrFromPtrW = GetStrFromBufferA(sRtn)
    
    End Function
    
    ' Returns the string before first null char encountered (if any) from an ANSII string.
    
    Public Function GetStrFromBufferA(sz As String) As String
      If InStr(sz, vbNullChar) Then
        GetStrFromBufferA = Left$(sz, InStr(sz, vbNullChar) - 1)
      Else
        ' If sz had no null char, the Left$ function
        ' above would return a zero length string ("").
        GetStrFromBufferA = sz
      End If
    End Function 

Additional query words: Unicode

Keywords : kbnetwork kbAPI kbSDKPlatform kbVBp400 kbVBp500 kbVBp600 kbNetAPI kbGrpNet
Version : WINDOWS:4.0,5.0,6.0
Platform : WINDOWS
Issue type : kbhowto


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