Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi All,

 

I'll apologise in advance for the length of this post.

 

I am trying to get information about any given user on our NT 4 Domain/Network.

 

I have defined the USER_INFO_3 structure like this:-

  <StructLayout(LayoutKind.Sequential)> _
   Public Structure USER_INFO_3
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_name As String
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_password As String
       Public usri3_password_age As Integer
       Public usri3_priv As Integer
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_home_dir As String
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_comment As String
       Public usri3_flags As Integer
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_script_path As String
       Public usri3_auth_flags As Integer
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_full_name As String
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_usr_comment As String
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_parms As String
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_workstations As String
       Public usri3_last_logon As Integer
       Public usri3_last_logoff As Integer
       Public usri3_acct_expires As Integer
       Public usri3_max_storage As Integer
       Public usri3_units_per_week As Integer
       <MarshalAs(UnmanagedType.U1)> Public usri3_logon_hours As Byte
       Public usri3_bad_pw_count As Integer
       Public usri3_num_logons As Integer
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_logon_server As String
       Public usri3_country_code As Integer
       Public usri3_code_page As Integer
       Public usri3_user_id As Integer
       Public usri3_primary_group_id As Integer
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_profile As String
       <MarshalAs(UnmanagedType.LPWStr)> Public usri3_home_dir_drive As String
       Public usri3_password_expired As Integer
   End Structure

 

..and am declaring the API like this:-

Public Declare Unicode Function NetUserGetInfo Lib "netapi32" _
   (ByRef lpServer As Byte, _
   ByRef UserName As Byte, _
    ByVal Level As Integer, ByVal lpBuffer As IntPtr) As Integer

 

..I am calling the API like this from my code:-

lRet = NetUserGetInfo(ServerName(0), UserName(0), Level, lpBuf)

 

The API returns with code zero, indicating that it has completed successfully. This is confirmed by the fact that it fails when I pass it a non-existent user.

 

However, the structure is not populating with any data whatosever.

 

Does anybody know what I am doing wrong? Do I need to use the marshal.ptrToStructure or marshal.StructureToPtr methods anywhere??

 

Any help would be appreciated.

 

:confused:

Posted

Basically, I changed the API declaration to:-

Public Declare Unicode Function NetUserGetInfo Lib "netapi32" ( _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal servername As String, _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal username As String, _
      ByVal level As Integer, _
      ByRef bufptr As IntPtr) As Integer

 

And then marshalled the buffer into a new structure by doing:-

UI3 = CType(Marshal.PtrToStructure(lpBuf, _
                    GetType(USER_INFO_3)), USER_INFO_3)

 

Here's the full code:-

 


   Public Function GetUserDetails(ByVal User As LANUser) As Boolean

       Dim UI3 As New USER_INFO_3()
       Dim Server As String
       Dim loopCount As Integer
       Dim lRet As Integer = -1
       Dim size As Integer = Marshal.SizeOf(UI3)
       Dim lpBuf As IntPtr = Marshal.AllocHGlobal(size)
       Const Level As Integer = 3

       Marshal.StructureToPtr(UI3, lpBuf, True)

       Server = GetPDC() & vbNullChar
       User.UserID = User.UserID & vbNullChar

       lRet = NetUserGetInfo(Server, User.UserID, Level, lpBuf)

       UI3 = CType(Marshal.PtrToStructure(lpBuf, _
                    GetType(USER_INFO_3)), USER_INFO_3)


       If lRet = NERR_SUCCESS Then
           User.UserID = UI3.usri3_name
           User.UserName = UI3.usri3_full_name
           User.HomeServer = Microsoft.VisualBasic.Left(UI3.usri3_home_dir, 10)
           User.HomeDirectory = UI3.usri3_home_dir
           User.ProfilePath = UI3.usri3_profile
           User.Description = UI3.usri3_comment
           User.LogonScript = UI3.usri3_script_path
           User.HomeDirectoryDrive = UI3.usri3_home_dir_drive

           Marshal.FreeHGlobal(lpBuf)

           Return True
       Else
           MessageBox.Show("User is not known to the PDC.")
           Marshal.FreeHGlobal(lpBuf)
           Return False
       End If

   End Function

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...