Results 1 to 8 of 8

Thread: NetUserGetInfo Question

  1. #1

    Thread Starter
    Hyperactive Member LeeSalter's Avatar
    Join Date
    Oct 2002
    Location
    Notts, England
    Posts
    307

    NetUserGetInfo Question

    Hello,

    I'm trying to convert my VB6 code to VB .NET.
    I have a call to the NetUserGetInfo API which populates the USER_INFO_3 structure as below:-

    Code:
        Public Structure USER_INFO_3
            Dim usri3_name As Integer
            Dim usri3_password As Integer
            Dim usri3_password_age As Integer
            Dim usri3_priv As Integer
            Dim usri3_home_dir As Integer
            Dim usri3_comment As Integer
            Dim usri3_flags As Integer
            Dim usri3_script_path As Integer
            Dim usri3_auth_flags As Integer
            Dim usri3_full_name As Integer
            Dim usri3_usr_comment As Integer
            Dim usri3_parms As Integer
            Dim usri3_workstations As Integer
            Dim usri3_last_logon As Integer
            Dim usri3_last_logoff As Integer
            Dim usri3_acct_expires As Integer
            Dim usri3_max_storage As Integer
            Dim usri3_units_per_week As Integer
            Dim usri3_logon_hours As Byte
            Dim usri3_bad_pw_count As Integer
            Dim usri3_num_logons As Integer
            Dim usri3_logon_server As Integer
            Dim usri3_country_code As Integer
            Dim usri3_code_page As Integer
            Dim usri3_user_id As Integer
            Dim usri3_primary_group_id As Integer
            Dim usri3_profile As Integer
            Dim usri3_home_dir_drive As Integer
            Dim usri3_password_expired As Integer
        End Structure
    What do these integer values actually hold? Are they pointers in memory? and how do I convert these integer values to the correpsonding string values?

    I've looked at the GetStrFromPtrW function that I've used in the VB6 version, without any success??

    Code:
        Public Function GetStrFromPtrW(ByRef lpszW As Integer) As String
            Dim sRtn As Integer
            Dim WideChar As Integer
            sRtn = lstrlenW(lpszW)  ' 2 bytes/char
            ' WideCharToMultiByte also returns Unicode string length
            WideChar = WideCharToMultiByte(CP_ACP, 0, lpszW, -1, sRtn, _
                vbNullString, vbNullString, 0)
            GetStrFromPtrW = GetStrFromBufferA(sRtn)
        End Function
        ' Returns the string before first null char encountered (if any) from an ANSII string.
        Public Function GetStrFromBufferA(ByRef 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
    The values that I am getting back are totally incorrect at the moment and this is beginning to frustrate.

    Any help would be appreciated.
    "I'm Brian and so is my Wife"

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    It is all quite different in .NET. The integers are indeed pointers (to a unicode string). By declaring the structure correctly, you don't need the GetStrFromPtrW function anymore.
    Have a look at this site

    EDIT: Sorry, I noticed it is C# code, but I think you manage to figure it out anyway
    Last edited by Frans C; Feb 12th, 2003 at 03:18 AM.

  3. #3

    Thread Starter
    Hyperactive Member LeeSalter's Avatar
    Join Date
    Oct 2002
    Location
    Notts, England
    Posts
    307
    Thanks for that Frans. I've now declared the structure using the method you describe:-
    Code:
    Public Structure USER_INFO_3
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_name As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_password As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_password_age As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_priv As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_home_dir As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_comment As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_flags As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_script_path As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_auth_flags As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_full_name As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_usr_comment As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_parms As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_workstations As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_last_logon As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_last_logoff As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_acct_expires As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_max_storage As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_units_per_week As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_logon_hours As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_bad_pw_count As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_num_logons As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_logon_server As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_country_code As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_code_page As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_user_id As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_primary_group_id As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_profile As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_home_dir_drive As String
            <MarshalAs(UnmanagedType.LPWStr)> Dim usri3_password_expired As String
        End Structure
    The call to the API returns successfully, but the strcuture is not populated with anything!!

    Is it anything to do with the MoveMemory function that was used in VB6? Do I need to still use this in .Net?

    Code:
    Public Declare Auto Sub MoveMemory Lib "kernel32" Alias _
            "RtlMoveMemory" (ByVal pDest As USER_INFO_3, ByRef pSource As Integer, _
            ByVal dwLength As Integer)
    TIA
    "I'm Brian and so is my Wife"

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Unfortunately I haven't got VB.NET installed at work, si i can't test it right now, but I noticed you declared every member of the structure as string. This is not correct. Also you haven't set the StructLayout to sequential. I also think you need to declare every member as Public in a public structure. I don't know how you declared the NetUserGetInfo function, but this can be wrong as well. Can you post the declaration, and the code you use to call the function?

    Never use RTLMoveMemory in VB.NET. At least not without special attention. The memory locations are not fixed in .NET. The garbage collector can move it around any time. You wouldn't know what you were copying, and to where. Mostly it isn't needed anymore. In this case it isn't.

    I will try to correct the structure.

    VB Code:
    1. <StructLayout(LayoutKind.Sequential)>Public Structure USER_INFO_3
    2.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_name As String
    3.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_password As String
    4.         Public usri3_password_age As Integer
    5.         Public usri3_priv As Integer
    6.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_home_dir As String
    7.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_comment As String
    8.         Public usri3_flags As Integer
    9.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_script_path As String
    10.         Public usri3_auth_flags As Integer
    11.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_full_name As String
    12.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_usr_comment As String
    13.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_parms As String
    14.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_workstations As String
    15.         Public usri3_last_logon As Integer
    16.         Public usri3_last_logoff As Integer
    17.         Public usri3_acct_expires As Integer
    18.         Public usri3_max_storage As Integer
    19.         Public usri3_units_per_week As Integer
    20.         <MarshalAs(UnmanagedType.U1)> Public usri3_logon_hours As Byte
    21.         Public usri3_bad_pw_count As Integer
    22.         Public usri3_num_logons As Integer
    23.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_logon_server As String
    24.         Public usri3_country_code As Integer
    25.         Public usri3_code_page As Integer
    26.         Public usri3_user_id As Integer
    27.         Public usri3_primary_group_id As Integer
    28.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_profile As String
    29.         <MarshalAs(UnmanagedType.LPWStr)> Public usri3_home_dir_drive As String
    30.         Public usri3_password_expired As Integer
    31.     End Structure

  5. #5

    Thread Starter
    Hyperactive Member LeeSalter's Avatar
    Join Date
    Oct 2002
    Location
    Notts, England
    Posts
    307
    OK Frans. Thanks for your help on this.

    Here's the declaration of the NetUserGetInfo function:-

    Code:
    Public Declare Unicode Function NetUserGetInfo Lib "netapi32" _
            (ByRef lpServer As Byte, _
             ByRef UserName As Byte, _
            ByVal Level As Integer, ByRef lpBuffer As Integer) As Integer
    ..and here's how it is called in the code...

    Code:
    lRet = NetUserGetInfo(ServerName(0), UserName(0), 3, lpBuf)
    I've also tried declaring the lpServer and lpUserName variables in the Function as string and passing it null terminated values.

    Thanks.
    "I'm Brian and so is my Wife"

  6. #6

    Thread Starter
    Hyperactive Member LeeSalter's Avatar
    Join Date
    Oct 2002
    Location
    Notts, England
    Posts
    307
    Can anybody help me with this??
    "I'm Brian and so is my Wife"

  7. #7
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I will have a look at it tonight.

  8. #8

    Thread Starter
    Hyperactive Member LeeSalter's Avatar
    Join Date
    Oct 2002
    Location
    Notts, England
    Posts
    307
    Franz,

    Not to worry.
    I've sorted it!!!!!!

    Thank for your input.
    "I'm Brian and so is my Wife"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width