Using NetAddUsers

Using NetAddUsers

Question:
I created a program to Add User Account to my NT Server. It reads from a text file which provides the user info. Everything works fine, except that the user’s full name takes the value from the user id. I used USER_INFO_2 and I specified the flag usri2_full_name to take the values from the text file (the value has been changed to unicode). This really puzzles me as the rest works fine. Please advice.

Answer:
It’s really hard to say without seeing both the code and the declares, but there are lots of way to go wrong with the unicode functions, and only a couple of ways to do it correctly. The following VB code will allow you to add users, including full names, to either the local machine or the server.

' ***********************************************' IN A MODULE (*.BAS) FILE' ***********************************************Option ExplicitPrivate Declare Function lstrlenW _   Lib "kernel32" (ByVal lpString As Long) As LongPrivate Declare Sub CopyMem _ Lib "kernel32" Alias "RtlMoveMemory" _ (pTo As Any, _  uFrom As Any, _  ByVal lSize As Long)' **************************************************' Inputs       : ByVal xi_lngStrPtr:Long  -- Pointer to a string' Outputs      : String: Translated string' Description  : When passed a pointer to a string,'              :     return that string' **************************************************Public Function PointerToStringW(xi_lngPtrString As Long) As StringOn Error Resume Next                 ' Don't accept error here Dim p_abytBuffer()                As Byte Dim p_lngLength                   As Long If xi_lngPtrString <> 0 Then    p_lngLength = lstrlenW(xi_lngPtrString) * 2    If p_lngLength Then       ReDim p_abytBuffer(0 To (p_lngLength - 1)) As Byte       CopyMem p_abytBuffer(0), ByVal xi_lngPtrString, p_lngLength       PointerToStringW = p_abytBuffer    End If End If   On Error GoTo 0End Function' **************************************************' Inputs       : ByVal xi_lngDWord:Long  -- Pointer to a DWord' Outputs      : String: Pointer' Description  : When passed a DWord, return a pointer to'              :   that DWord' **************************************************Public Function PointerToDWord(xi_lngDWord As Long) As LongOn Error Resume Next               ' Don't accept error here Dim p_lngRtn                    As Long    If xi_lngDWord <> 0 Then    CopyMem p_lngRtn, ByVal xi_lngDWord, 4    PointerToDWord = p_lngRtn End If   On Error GoTo 0End Function' ***********************************************' IN A FORM (*.FRM) FILE' ***********************************************Option Explicit' ---------------------------------------------' API calls' ---------------------------------------------Private Declare Function NetUserAdd _   Lib "netapi32.dll" _   (ServerName As Byte, _    ByVal Level As Long, _    Buffer As USER_INFO_3, _    parm_err As Long) As Long' ---------------------------------------------' Possible errors with API call' ---------------------------------------------Private Const ERROR_ACCESS_DENIED    As Long = 5Private Const NERR_BASE              As Long = 2100Private Const NERR_GroupExists       As Long = NERR_BASE + 123Private Const NERR_NotPrimary        As Long = NERR_BASE + 126Private Const NERR_UserExists        As Long = NERR_BASE + 124Private Const NERR_PasswordTooShort  As Long = NERR_BASE + 145Private Const NERR_InvalidComputer   As Long = NERR_BASE + 251' ---------------------------------------------' General constants used' ---------------------------------------------Private Const constUserInfoLevel3    As Long = 3Private Const TIMEQ_FOREVER          As Long = -1&Private Const MAX_PATH               As Long = 260&Private Const DOMAIN_GROUP_RID_USERS As Long = &H201&Private Const USER_MAXSTORAGE_UNLIMITED As Long = -1&' ---------------------------------------------' Used by usri3_flags element of data structure' ---------------------------------------------Private Const UF_SCRIPT              As Long = &H1&Private Const UF_ACCOUNTDISABLE      As Long = &H2&Private Const UF_HOMEDIR_REQUIRED    As Long = &H8&Private Const UF_LOCKOUT             As Long = &H10&Private Const UF_PASSWD_NOTREQD      As Long = &H20&Private Const UF_PASSWD_CANT_CHANGE  As Long = &H40&Private Const UF_DONT_EXPIRE_PASSWD  As Long = &H10000Private Const STILL_ACTIVE           As Long = &H103&Private Const UF_NORMAL_ACCOUNT      As Long = &H200&Private Const UF_SERVER_TRUST_ACCOUNT  As Long = &H2000&Private Const PROCESS_QUERY_INFORMATION  As Long = &H400&Private Const UF_TEMP_DUPLICATE_ACCOUNT   As Long = &H100&Private Const UF_INTERDOMAIN_TRUST_ACCOUNT   As Long = &H800&Private Const UF_WORKSTATION_TRUST_ACCOUNT   As Long = &H1000&' ---------------------------------------------' The USER_INFO_3 data structure' ---------------------------------------------Private Type USER_INFO_3 usri3_name            As Long usri3_password        As Long usri3_password_age    As Long usri3_priv            As Long usri3_home_dir        As Long usri3_comment         As Long usri3_flags           As Long usri3_script_path     As Long usri3_auth_flags      As Long usri3_full_name       As Long usri3_usr_comment     As Long usri3_parms           As Long usri3_workstations    As Long usri3_last_logon      As Long usri3_last_logoff     As Long usri3_acct_expires    As Long usri3_max_storage     As Long usri3_units_per_week  As Long usri3_logon_hours     As Long usri3_bad_pw_count    As Long usri3_num_logons      As Long usri3_logon_server    As Long usri3_country_code    As Long usri3_code_page       As Long usri3_user_id         As Long usri3_primary_group_id As Long usri3_profile          As Long usri3_home_dir_drive   As Long usri3_password_expired As LongEnd TypePrivate Sub Command2_Click()  Dim p_blnRtn                      As Boolean    On Error Resume Next p_blnRtn = AddUser(xi_strServerName:="PDCNEW", _                    xi_strUserName:="MyUser1", _                    xi_strPassword:="ABCDEF", _                    xi_strUserFullName:="Long Name for MyUser", _                    xi_strUserComment:="This is a comment") If Err.Number <> 0 Then    MsgBox "Error: " & Err.Description & " in the function " & Err.Source End If   End Sub' **********************************************' Add a user either to NT -- you *MUST* have admin or'     account operator priviledges to successfully run'     this function' **********************************************Public Function AddUser(ByVal xi_strServerName As String, _                      ByVal xi_strUserName As String, _                      ByVal xi_strPassword As String, _                      Optional ByVal xi_strUserFullName As String = vbNullString, _                      Optional ByVal xi_strUserComment As String = vbNullString) As Boolean Dim p_strErr                      As String Dim p_lngRtn                      As Long Dim p_lngPtrUserName              As Long Dim p_lngPtrPassword              As Long Dim p_lngPtrUserFullName          As Long Dim p_lngPtrUserComment           As Long Dim p_lngParameterErr             As Long Dim p_lngFlags                    As Long Dim p_abytServerName()            As Byte Dim p_abytUserName()              As Byte Dim p_abytPassword()              As Byte Dim p_abytUserFullName()          As Byte Dim p_abytUserComment()           As Byte Dim p_strServerName               As String Dim p_typUserInfo3                As USER_INFO_3     If xi_strUserFullName = vbNullString Then    xi_strUserName = xi_strUserName End If    If Len(xi_strServerName) < 1 Then    p_strServerName = "" Else    p_strServerName = UCase$(xi_strServerName)    If Left$(p_strServerName, 2) <> "\" Then       p_strServerName = "\" & p_strServerName    End If End If         ' ------------------------------------------   ' Create byte arrays to avoid Unicode hassles   ' ------------------------------------------ p_abytServerName = p_strServerName & vbNullChar p_abytUserName = xi_strUserName & vbNullChar p_abytUserFullName = xi_strUserFullName & vbNullChar p_abytPassword = xi_strPassword & vbNullChar p_abytUserComment = xi_strUserComment & vbNullChar    ' ------------------------------------------ ' Get pointers to the byte arrays ' ------------------------------------------ p_lngPtrUserName = VarPtr(p_abytUserName(0)) p_lngPtrUserFullName = VarPtr(p_abytUserFullName(0)) p_lngPtrPassword = VarPtr(p_abytPassword(0)) p_lngPtrUserComment = VarPtr(p_abytUserComment(0))  ' ------------------------------------------ ' Fill the VB structure ' ------------------------------------------ p_lngFlags = UF_NORMAL_ACCOUNT Or _              UF_SCRIPT Or _              UF_DONT_EXPIRE_PASSWD With p_typUserInfo3    .usri3_acct_expires = TIMEQ_FOREVER            ' Never expires    .usri3_comment = p_lngPtrUserComment           ' Comment    .usri3_flags = p_lngFlags                      ' There are a number of variations    .usri3_full_name = p_lngPtrUserFullName        ' User's full name    .usri3_max_storage = USER_MAXSTORAGE_UNLIMITED   ' Can use any amount of disk space    .usri3_name = p_lngPtrUserName                   ' Name of user account    .usri3_password = p_lngPtrPassword               ' Password for user account    .usri3_primary_group_id = DOMAIN_GROUP_RID_USERS ' You MUST use this constant for NetUserAdd    .usri3_script_path = 0&     ' Path of user's logon script    .usri3_auth_flags = 0&      ' Ignored by NetUserAdd    .usri3_bad_pw_count = 0&    ' Ignored by NetUserAdd    .usri3_code_page = 0&       ' Code page for user's language    .usri3_country_code = 0&    ' Country code for user's language    .usri3_home_dir = 0&        ' Can specify path of home directory of this user    .usri3_home_dir_drive = 0&  ' Drive letter assign to user's profile    .usri3_last_logoff = 0&     ' Not needed when adding a user    .usri3_last_logon = 0&      ' Ignored by NetUserAdd    .usri3_logon_hours = 0&     ' Null means no restrictions    .usri3_logon_server = 0&    ' Null means logon to domain server    .usri3_num_logons = 0&      ' Ignored by NetUserAdd    .usri3_parms = 0&           ' Used by specific applications    .usri3_password_age = 0&    ' Ignored by NetUserAdd    .usri3_password_expired = 0& ' None-zero means user must change password at next logon    .usri3_priv = 0&            ' Ignored by NetUserAdd    .usri3_profile = 0&         ' Path to a user's profile    .usri3_units_per_week = 0&  ' Ignored by NetUserAdd    .usri3_user_id = 0&         ' Ignored by NetUserAdd    .usri3_usr_comment = 0&     ' User comment    .usri3_workstations = 0&    ' Workstations a user can log onto (null = all stations)   End With   ' ------------------------------------------' Attempt to add the user' ------------------------------------------ p_lngRtn = NetUserAdd(p_abytServerName(0), _                       constUserInfoLevel3, _                       p_typUserInfo3, _                       p_lngParameterErr)   ' ------------------------------------------' Check for error' ------------------------------------------ If p_lngRtn <> 0 Then    AddUser = False    Select Case p_lngRtn       Case ERROR_ACCESS_DENIED          p_strErr = "User doesn't have sufficient access rights."       Case NERR_GroupExists          p_strErr = "The group already exists."       Case NERR_NotPrimary          p_strErr = "Can only do this operation on the PDC of the domain."       Case NERR_UserExists          p_strErr = "The user account already exists."       Case NERR_PasswordTooShort          p_strErr = "The password is shorter than required."       Case NERR_InvalidComputer          p_strErr = "The computer name is invalid."       Case Else          p_strErr = "Unknown error #" & CStr(p_lngRtn)    End Select          On Error GoTo 0    Err.Raise Number:=p_lngRtn, _              Description:=p_strErr & vbCrLf & _                           "Error in parameter " & p_lngParameterErr & _                           " when attempting to add the user, " & xi_strUserName, _              Source:="Form1.AddUser" Else    AddUser = True End IfEnd Function
devx-admin

devx-admin

Share the Post:
Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023,

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023, more than one-fifth of automobiles

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed are at the forefront because

Sunsets' Technique

Inside the Climate Battle: Make Sunsets’ Technique

On February 12, 2023, Luke Iseman and Andrew Song from the solar geoengineering firm Make Sunsets showcased their technique for injecting sulfur dioxide (SO₂) into the stratosphere as a means

AI Adherence Prediction

AI Algorithm Predicts Treatment Adherence

Swoop, a prominent consumer health data company, has unveiled a cutting-edge algorithm capable of predicting adherence to treatment in people with Multiple Sclerosis (MS) and other health conditions. Utilizing artificial

Personalized UX

Here’s Why You Need to Use JavaScript and Cookies

In today’s increasingly digital world, websites often rely on JavaScript and cookies to provide users with a more seamless and personalized browsing experience. These key components allow websites to display

Geoengineering Methods

Scientists Dimming the Sun: It’s a Good Thing

Scientists at the University of Bern have been exploring geoengineering methods that could potentially slow down the melting of the West Antarctic ice sheet by reducing sunlight exposure. Among these

why startups succeed

The Top Reasons Why Startups Succeed

Everyone hears the stories. Apple was started in a garage. Musk slept in a rented office space while he was creating PayPal with his brother. Facebook was coded by a

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists