Link to home
Start Free TrialLog in
Avatar of Arachn1d
Arachn1d

asked on

NetUserGetInfo Problems

I have the following code (Along with appropriate API definitions) that should retrieve the real name of a user, given their username. However, when I try it, I get a whole lot of rubbish at the beginning of the name. The rubbish is never the same, and I cant work out what is wrong. What am I doing wrong?

Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Public Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyW" (RetVal As Byte, ByVal Ptr As Long) As Long
Public Declare Function NetAPIBufferFree Lib "netapi32.dll" Alias "NetApiBufferFree" (ByVal Ptr As Long) As Long
Public Declare Function lstrcpyW Lib "kernel32.dll" (bRet As Byte, ByVal lPtr As Long) As Long
Public Declare Function NetUserGetInfo Lib "netapi32.dll" (ServerName As Byte, UserName As Byte, ByVal Level As Long, Buffer As Long) As Long
Public Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long


Public Function getRealName(strServer As String, strUsername As String) As String
    Dim lngReturn As Long
    Dim baServerName() As Byte
    Dim baUserName() As Byte
    Dim lngptrUserInfo As Long
    Dim userInfo As USER_INFO_10_API
    Dim strName As String
    Dim a As Integer
   
    'set variables
    baServerName = strServer & Chr$(0)
    baUserName = strUsername & Chr$(0)
   
    'get user info
    lngReturn = NetUserGetInfo(baServerName(0), baUserName(0), 10, lngptrUserInfo)

    'any errors?
    If lngReturn <> 0 Then
      getRealName = ""
      Exit Function
    End If

    'Turn the pointer into a variable
    CopyMem userInfo, lngptrUserInfo, Len(userInfo)
   
    strName = PointerToStringW(userInfo.Name)
    getRealName = strFix(strName)
    NetAPIBufferFree lngptrUserInfo
End Function

Public Function PointerToStringW(lpStringW As Long) As String
   Dim Buffer() As Byte
   Dim nLen As Long
   
   If lpStringW Then
      nLen = lstrlenW(lpStringW) * 2
      If nLen Then
         ReDim Buffer(0 To (nLen - 1)) As Byte
         CopyMem Buffer(0), ByVal lpStringW, nLen
         PointerToStringW = Buffer
      End If
   End If
End Function

Avatar of AzraSound
AzraSound
Flag of United States of America image

two examples that may aid you:

sample here called netuser.zip
http://www.mvps.org/vb/index2.html?samples.htm

http://www.vbsquare.com/articles/ntvb/

Avatar of Arachn1d
Arachn1d

ASKER

The latter has netUserEnum, not netUserGetInfo. The latter, while it has a useful class, this class returns an error, and I can't get it to work. All I need to know is what is wrong with my code!
Here's the code:

'Use USER_INFO_3_API

Option Explicit

Dim sName As String
Dim sServer As String
Private Type USER_INFO_3_API
   ' Level 0 starts here
   Name As Long
   ' Level 1 starts here
   Password As Long
   PasswordAge As Long
   Privilege As Long
   HomeDir As Long
   Comment As Long
   Flags As Long
   ScriptPath As Long
   ' Level 2 starts here
   AuthFlags As Long
   FullName As Long
   UserComment As Long
   Parms As Long
   Workstations As Long
   LastLogon As Long
   LastLogoff As Long
   AcctExpires As Long
   MaxStorage As Long
   UnitsPerWeek As Long
   LogonHours As Long
   BadPwCount As Long
   NumLogons As Long
   LogonServer As Long
   CountryCode As Long
   CodePage As Long
   ' Level 3 starts here
   UserID As Long
   PrimaryGroupID As Long
   Profile As Long
   HomeDirDrive As Long
   PasswordExpired As Long
End Type



Private Sub Command1_Click()
    getRealName "\\c-936", "test"
End Sub

Public Function getRealName(strServer As String, strUsername As String) As String
    Dim lngReturn As Long
    Dim baServerName() As Byte
    Dim baUserName() As Byte
    Dim lngptrUserInfo As Long
    Dim userInfo As USER_INFO_3_API
    Dim strName As String
    Dim a As Integer
     
    'set variables
    baServerName = strServer & vbNullChar
    baUserName = strUsername & vbNullChar
     
    'get user info
    lngReturn = NetUserGetInfo(baServerName(0), baUserName(0), 3, lngptrUserInfo)

    'any errors?
    If lngReturn <> 0 Then
      getRealName = ""
      Exit Function
    End If

    'Turn the pointer into a variable
    CopyMem userInfo, ByVal lngptrUserInfo, Len(userInfo)
     
    strName = PointerToStringW(userInfo.FullName)
    MsgBox strName
    getRealName = strName
    NetAPIBufferFree lngptrUserInfo
End Function

Public Function PointerToStringW(lpStringW As Long) As String
   Dim Buffer() As Byte
   Dim nLen As Long
     
   If lpStringW Then
      nLen = lstrlenW(lpStringW) * 2
      If nLen Then
         ReDim Buffer(0 To (nLen - 1)) As Byte
         CopyMem Buffer(0), ByVal lpStringW, nLen
         PointerToStringW = Buffer
      End If
   End If
End Function


ASKER CERTIFIED SOLUTION
Avatar of PHalse
PHalse

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I wanted a fix on my code, not new code.
Comment accepted as answer
Thanks, this is what I wanted.