How To Use NetUserAdd (140165)



The information in this article applies to:

  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Professional
  • Microsoft Win32 Software Development Kit (SDK) 3.1
  • Microsoft Win32 Software Development Kit (SDK) 3.5
  • Microsoft Win32 Software Development Kit (SDK) 3.51
  • Microsoft Win32 Software Development Kit (SDK) 4.0

This article was previously published under Q140165

SUMMARY

NetUserAdd allows a Windows NT application to add a user to a Windows NT workstation or server programmatically. Adding a user with this function seems easy enough, but there are a few issues that are not obvious.

MORE INFORMATION

If you would like to add users to a Windows NT workstation or server in your application, you must use NetUserAdd. Given a server name, level, and appropriate structure, a new user will be created. The following pointers may help you avoid problems:
  1. As required of all Net* functions, submit all strings in UNICODE (wide characters). Use MultiByteToWideChar to convert standard ASCII strings into UNICODE strings. Don't forget to include the terminating NULL in string conversions.
  2. Specify the server name as \\server. Be sure to include double backslashes, four backslashes if inside a C literal string.
  3. Use the UF_SCRIPT required flag. Without it, you will get ERROR_INVALID_PARAMETER.
  4. When using level 2, make sure your strings do not exceed the maximum lengths. Constants are defined for the maximum lengths. For example, the maximum lengths for params, comment, user comment, and full name is MAXCOMMENTSZ. Paths must fit within PATHLEN characters. UNC names for servers must fit in UNCLEN characters.
  5. Use NetUserAdd only to add user accounts, not guest or administrator accounts. After the account is established, use NetUserSetGroups to add Administrator, Guest, or other groups to the new user account.

REFERENCES

Microsoft LAN Manager, A Programmer's Guide by Ralph Ryan.
Microsoft LAN Manager Programmer's Reference.
Win32 SDK Documentation.

Modification Type:MinorLast Reviewed:3/21/2005
Keywords:kbhowto kbnetwork KB140165