|
-
Feb 11th, 2003, 11:19 AM
#1
Thread Starter
Hyperactive Member
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"
-
Feb 12th, 2003, 03:06 AM
#2
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.
-
Feb 12th, 2003, 06:40 AM
#3
Thread Starter
Hyperactive Member
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"
-
Feb 12th, 2003, 07:31 AM
#4
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:
<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
-
Feb 12th, 2003, 08:21 AM
#5
Thread Starter
Hyperactive Member
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"
-
Feb 13th, 2003, 05:55 AM
#6
Thread Starter
Hyperactive Member
Can anybody help me with this??
"I'm Brian and so is my Wife"
-
Feb 13th, 2003, 07:10 AM
#7
I will have a look at it tonight.
-
Feb 13th, 2003, 10:46 AM
#8
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|