| 
 | 
								
									
										|  |  
										|  |  
										| | This article will show you use of NetUserAdd and NetUserDel API to add/remove useraccount. 
 Windows NT and Windows 2000 exposes LanMan 32-bit Application Programming Interfaces (APIs) to provide network services. These APIs are called only from 32-bit programs running on a Windows NT or Windows 2000 and are used to control both the local and remote Windows NT (or Windows 2000) machines that you have permission to access.
 
 Note:
 | 
 WhileWindows2000supportsLanManAPIsforbackwardscompatibilityitisrecommendedthatADSIbeusedinsteadformostofthefunctionsbelow.FormoreinformationaboutADSIsearchforActiveDirectoryServicesInterfaceorADSIintheMicrosoftKnowledgeBaseorMSDN | This article contains code examples of using the following LanMan APIs: | 
 NetUserAddusingLevel1structureNetUserDelNetAPIBufferFreeNetAPIBufferAllocat | Note: LAN Manager APIs use various level of structures. You can use any supported level depending on information required. For example in this article i have used NetUserAdd API with level-1 structure USER_INFO_1 but you can also use USER_INFO_2, USER_INFO_3 or USER_INFO_4 depending on information you want to specify when you add the user account. 
 Although, Visual Basic stores strings internally as UNICODE, it converts them to ANSI when it calls APIs. Normally this isn't a problem because most 32-bit APIs have both an ANSI and a UNICODE version. However, LanMan APIs only support UNICODE. The code snippet below demonstrates how to use Byte arrays to pass UNICODE parameters.
 
 Instead of using strings, for example:
 | 
 | use Byte arrays, such the one below, because they aren't converted to ANSI:
 | 
 | Now lets check the actual code which will first add sample user TestUser1 on local machine and then it will delete that account. 
 Step-By-Step Example
 
 - Create a standard exe project
 - Add the following code in form1
 | 
 |  Click here to copy the following block |  | Const NERR_BASE = 2100 Const ERROR_ACCESS_DENIED = 5&
 Const NERR_GroupExists = (NERR_BASE + 123)
 Const NERR_InvalidComputer = (NERR_BASE + 251)
 Const NERR_NotPrimary = (NERR_BASE + 126)
 Const NERR_PasswordTooShort = (NERR_BASE + 145)
 Const NERR_UserExists = (NERR_BASE + 124)
 Const NERR_UserNotFound = (NERR_BASE + 121)
 
 
 Const USER_PRIV_MASK = &H3
 Const USER_PRIV_GUEST = &H0
 Const USER_PRIV_USER = &H1
 Const USER_PRIV_ADMIN = &H2
 
 
 Const UF_SCRIPT = &H1
 Const UF_ACCOUNTDISABLE = &H2
 Const UF_HOMEDIR_REQUIRED = &H8
 Const UF_LOCKOUT = &H10
 Const UF_PASSWD_NOTREQD = &H20
 Const UF_PASSWD_CANT_CHANGE = &H40
 Const UF_NORMAL_ACCOUNT = &H200
 
 
 
 
 Private Type USER_INFO_0
 ptrname As Long
 End Type
 
 Private Type USER_INFO_1
 ptrname As Long
 ptrPassword As Long
 dwPasswordAge As Long
 dwPriv As Long
 ptrHomeDir As Long
 ptrcomment As Long
 dwFlags As Long
 ptrScriptPath As Long
 End Type
 
 Private Declare Function NetAPIBufferFree Lib "NETAPI32.dll" Alias _
 "NetApiBufferFree" (ByVal Ptr As Long) As Long
 
 Private Declare Function NetAPIBufferAllocate Lib "NETAPI32.dll" Alias _
 "NetApiBufferAllocate" (ByVal ByteCount As Long, Ptr As Long) As Long
 
 Private Declare Function StrToPtr Lib "Kernel32" Alias "lstrcpyW" _
 (ByVal Ptr As Long, Source As Byte) As Long
 
 
 Private Declare Function NetUserAdd1 Lib "NETAPI32.dll" Alias "NetUserAdd" _
 (servername As Byte, ByVal level As Long, Buffer As USER_INFO_1, ParmError As Long) As Long
 
 Private Declare Function NetUserDel Lib "NETAPI32.dll" (servername As Byte, _
 UserName As Byte) As Long
 
 Function DelUser(ByVal servername As String, ByVal UserName As String) As Long
 Dim UNArray() As Byte, SNArray() As Byte
 UNArray = UserName & vbNullChar
 SNArray = servername & vbNullChar
 DelUser = NetUserDel(SNArray(0), UNArray(0))
 End Function
 
 Function AddUser(ByVal servername As String, ByVal UserName As String, _
 ByVal PWD As String) As Long
 
 Dim result As Long, UNPtr As Long, PWDPtr As Long, ParmError As Long
 Dim SNArray() As Byte, UNArray() As Byte, PWDArray() As Byte
 Dim UserStruct As USER_INFO_1
 
 
 
 SNArray = servername & vbNullChar
 UNArray = UserName & vbNullChar
 PWDArray = PWD & vbNullChar
 
 
 
 result = NetAPIBufferAllocate(UBound(UNArray) + 1, UNPtr)
 result = NetAPIBufferAllocate(UBound(PWDArray) + 1, PWDPtr)
 
 
 
 result = StrToPtr(UNPtr, UNArray(0))
 result = StrToPtr(PWDPtr, PWDArray(0))
 
 
 
 With UserStruct
 .ptrname = UNPtr
 .ptrPassword = PWDPtr
 .dwPasswordAge = 3
 .dwPriv = USER_PRIV_USER
 .ptrHomeDir = 0
 .ptrcomment = 0
 .dwFlags = UF_NORMAL_ACCOUNT Or UF_SCRIPT
 .ptrScriptPath = 0
 End With
 
 
 
 result = NetUserAdd1(SNArray(0), 1, UserStruct, ParmError)
 AddUser = result
 If result <> 0 Then
 Debug.Print "Error " & result & " in parameter " & ParmError _
 & " when adding user " & UserName
 End If
 
 
 
 result = NetAPIBufferFree(UNPtr)
 result = NetAPIBufferFree(PWDPtr)
 End Function
 
 Function GetError(errCode As Long) As String
 Select Case errCode
 Case NERR_InvalidComputer
 GetError = "The computer name is invalid."
 Case NERR_NotPrimary
 GetError = "The operation is allowed only on the primary domain controller of the domain."
 Case NERR_PasswordTooShort
 GetError = "The password is shorter than required according to policy."
 Case NERR_UserExists
 GetError = "The user name already exists."
 Case NERR_GroupExists
 GetError = "The group name already exists."
 Case NERR_UserNotFound
 GetError = "The user name could not be found."
 Case ERROR_ACCESS_DENIED
 GetError = "Access is denied"
 Case Else
 GetError = "Error#" & Err.LastDllError
 End Select
 End Function
 
 Private Sub Form_Load()
 Dim ret As Long
 
 
 
 ret = AddUser(".", "TestUser1", "pwd123")
 If ret = 0 Then
 MsgBox "User added", vbInformation
 Else
 MsgBox GetError(ret)
 Exit Sub
 End If
 
 ret = MsgBox("Do you want to delete the user which you just added?", vbQuestion + vbYesNo)
 If ret = vbYes Then
 ret = DelUser(".", "TestUser1")
 If ret = 0 Then
 MsgBox "User Deleted", vbInformation
 Else
 MsgBox GetError(ret)
 Exit Sub
 End If
 End If
 End Sub
 | 
 |  
										|  |  
										|  |  
 
	
		| Submitted By :
					Nayan Patel 
					 (Member Since : 5/26/2004 12:23:06 PM) |  
		|  |  
		| 
				 | Job Description :   
			He is the moderator of this site and  currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |  
		| View all (893) submissions by this author 
			(Birth Date : 7/14/1981 ) |  |  |