Advertisement
2002C Windows System Services #8619

Create/Destroy User on Domain ( Administrating NT)

Create a new user and destroy an existing user on a Windows NT domain.. When a user is created, I set him to be a member of Domain Users while you can specify that he goes into Domain User, Domain Guests, Domain Admins Hong YAN

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Function DomainCreateUser( _
  ByVal sSName As String, _
  ByVal sUName As String, _
  ByVal sPWD As String, _
  ByVal sHomeDir As String, _
  ByVal sComment As String, _
  ByVal sScriptFile As String) As Long
'Create a new user to be a member of group Domain Users
  Dim lResult As Long
  Dim lParmError As Long
  
  Dim lUNPtr As Long
  Dim lPWDPtr As Long
  Dim lHomeDirPtr As Long
  Dim lCommentPtr As Long
  Dim lScriptFilePtr As Long
  
  Dim bSNArray() As Byte
  Dim bUNArray() As Byte
  Dim bPWDArray() As Byte
  Dim bHomeDirArray() As Byte
  Dim bCommentArray() As Byte
  Dim bScriptFileArray() As Byte
  
  Dim UserStruct As TUser1
   
  ' Move to byte arrays
  bSNArray = sSName & vbNullChar
  bUNArray = sUName & vbNullChar
  bPWDArray = sPWD & vbNullChar
  bHomeDirArray = sHomeDir & vbNullChar
  bCommentArray = sComment & vbNullChar
  bScriptFileArray = sScriptFile & vbNullChar
  
  ' Allocate buffer space
  lResult = NetAPIBufferAllocate(UBound(bUNArray) + 1, lUNPtr)
  lResult = NetAPIBufferAllocate(UBound(bPWDArray) + 1, lPWDPtr)
  lResult = NetAPIBufferAllocate(UBound(bHomeDirArray) + 1, lHomeDirPtr)
  lResult = NetAPIBufferAllocate(UBound(bCommentArray) + 1, lCommentPtr)
  lResult = NetAPIBufferAllocate(UBound(bScriptFileArray) + 1, lScriptFilePtr)
  
  ' Copy arrays to the buffer
  lResult = StrToPtr(lUNPtr, bUNArray(0))
  lResult = StrToPtr(lPWDPtr, bPWDArray(0))
  lResult = StrToPtr(lHomeDirPtr, bHomeDirArray(0))
  lResult = StrToPtr(lCommentPtr, bCommentArray(0))
  lResult = StrToPtr(lScriptFilePtr, bScriptFileArray(0))
  
  With UserStruct
   .ptrName = lUNPtr
   .ptrPassword = lPWDPtr
   .dwPasswordAge = 3
   .dwPriv = USER_PRIV_USER
   .ptrHomeDir = lHomeDirPtr
   .ptrComment = lCommentPtr
   .dwFlags = UF_NORMAL_ACCOUNT Or UF_SCRIPT
   .ptrScriptHomeDir = lScriptFilePtr
  End With
  
  ' Create the new user
  lResult = NetUserAdd1(bSNArray(0), 1, UserStruct, lParmError)
  DomainCreateUser = lResult
  If lResult <> 0 Then
    Call NetErrorHandler(lResult, " when creating new user " & sUName)
  End If
  
  ' Release buffers from memory
  lResult = NetAPIBufferFree(lUNPtr)
  lResult = NetAPIBufferFree(lPWDPtr)
  lResult = NetAPIBufferFree(lHomeDirPtr)
  lResult = NetAPIBufferFree(lCommentPtr)
  lResult = NetAPIBufferFree(lScriptFilePtr)
End Function
Public Function DomainDestroyUser(ByVal sSName As String, ByVal sUName As String)
'Destroy an existing user with user id sUName
'from current PDC with sSName
  Dim lResult As Long
  Dim lParmError As Long
  
  Dim bSNArray() As Byte
  Dim bUNArray() As Byte
   
  ' Move to byte arrays
  bSNArray = sSName & vbNullChar
  bUNArray = sUName & vbNullChar
  
  lResult = NetUserDel(bSNArray(0), bUNArray(0))
  If lResult = 0 Then
    DomainDestroyUser = True
  Else
    Call NetErrorHandler(lResult, "delete user '" & sUName & "' from server '" & 
sSName & "'.")
    DomainDestroyUser = False
  End If
  
End Function
Original Comments (3)
Recovered from Wayback Machine