Changing Elements of User Information

Windows NT

The code sample in this topic demonstrates how to change several elements of user information using the NetUserGetInfo and NetUserSetInfo functions and various levels of several structures.

The Net functions provide a variety of levels to assist in changing user information. Some of these levels require administrative privileges to execute successfully. When changing user information, it is best to use the specific level for that piece of information. This prevents the accidental resetting of unrelated information when using the lower level values. Some of the more commonly used levels are illustrated in the following sample code.

All code fragments assume that the user has defined the UNICODE compile directive and included the appropriate Net function header files, as follows:

#define UNICODE
#include <windows.h>
#define INCL_NET
#include <lm.h>
 

Setting the User Password, Level 1003

This level sets the password to a known value. The USER_INFO_1003 structure topic contains a description of each flag. The following code fragment illustrates how to use this level.

#define PASSWORD TEXT("new_password")

.
.
.
USER_INFO_1003 usriSetPassword;
DWORD netRet = 0;
//
// Set the usri1003_password element to point to a Unicode string
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriSetPassword.usri1003_password  = PASSWORD;
netRet = NetUserSetInfo( SERVER, USERNAME,  1003, (LPBYTE)&usriSetPassword);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);
 

Setting the User Privilege, Level 1005

This level sets the privilege level of the user. The USER_INFO_1005 structure topic contains a description of each flag. The following code fragment illustrates how to use this level.

USER_INFO_1005 usriPriv;
DWORD netRet = 0;
//
// Set the usri1005_priv to the appropriate value, then call NetUserSetInfo.
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriPriv.usri1005_priv  = USER_PRIV_USER;
netRet = NetUserSetInfo( SERVER, USERNAME,  1005, (LPBYTE)&usriPriv);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);
 

Setting the User Home Directory, Level 1006

This level sets the user home directory. The USER_INFO_1006 structure topic contains a description of each flag. This directory can be a hard coded path or a valid Unicode path.

#define HOMEDIR TEXT("C:\\USER\USER_PATH")
USER_INFO_1006 usriHomeDir;
//
// Set the usri1006_home_dir to point to a valid Unicode string for the new home directory.
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriHomeDir.usri1006_home_dir  = HOMEDIR;
netRet = NetUserSetInfo( SERVER, USERNAME,  1006, (LPBYTE)&usriHomeDir);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);
 

Setting the User Comment Field, Level 1007

This level is used to set the user comment field. The USER_INFO_1007 structure topic contains a description of each flag. The following code fragment illustrates how to use this level.

#define COMMENT TEXT("This is my Comment Text for the user")
USER_INFO_1007 usriComment;
//
// Set the usri1007_comment  to point to a valid Unicode string for the new comment.
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriComment.usri1007_comment  = COMMENT;
netRet = NetUserSetInfo( SERVER, USERNAME,  1006, (LPBYTE)&usriComment);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);
 

Setting the User Flags, Level 1008

This level sets the user flags. The USER_INFO_1008 structure topic contains the valid values for the flags field. The UF_SCRIPT flag must be set for Windows NT and LAN Manager networks. Trying to set the flags without setting this bit on a Windows NT or LAN Manager network will cause the NetUserSetInfo function to fail. The following code fragment illustrates how to use this level.

#define USR_FLAGS UF_SCRIPT | UF_NORMAL_ACCOUNT
USER_INFO_1008 usriFlags;
//
// Set the usri1008_flags  to the appropriate constant.
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriFlags.usri1008_flags  = USR_FLAGS;
netRet = NetUserSetInfo( SERVER, USERNAME,  1006, (LPBYTE)&usriFlags);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);
 

Setting the User Script Path, Level 1009

This level is used to set a logon script path for a particular user. Points to a Unicode string specifying the path of the user's logon script, .CMD, .EXE, or .BAT file. The string can be null. The USER_INFO_1009 structure topic contains a description of each flag. The following code fragment illustrates how to use this level.

#define SCRIPT_PATH "C:\\BIN\\MYSCRIPT.BAT"
USER_INFO_1009 usriScrPath;
//
// Set the usri1009_script_path  to a Unicode string constant or a pointer to a Unicode string.
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriScrPath.usri1009_script_path  = SCRIPT_PATH;
netRet = NetUserSetInfo( SERVER, USERNAME,  1006, (LPBYTE)&usriScrPath);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);
 

Setting The User Authority Flags, Level 1010

This level is used to set the authority flags for a user. The USER_INFO_1010 structure topic contains a description of each flag. The following code fragment illustrates how to use this level.

#define AUTHORITY_FLAGS AF_OP_ACCOUNTS
USER_INFO_1010 usriAuthFlags;
//
// Set the usri1010_auth_flags  a constant containing the appropriated flag values.
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriAuthFlags.usri1010_auth_flags  = SCRIPT_PATH;
netRet = NetUserSetInfo( SERVER, USERNAME,  1006, (LPBYTE)&usriAuthFlags);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);
 

Setting The User Full Name, Level 1011

This level sets the user's full name. The USER_INFO_1011 structure topic contains a description of each flag. The following code fragment illustrates how to use this level.

#define USER_FULL_NAME TEXT("Joe B. User")
USER_INFO_1011 usriFullName;
//
// Set the usri1011_full_name  to a Unicode string constant or a pointer to a Unicode string.
//
// SERVER and USERNAME can be hard coded TEXT("") strings or pointers to
// Unicode strings.
//
usriFullName.usri1011_full_name  = USER_FULL_NAME;
netRet = NetUserSetInfo( SERVER, USERNAME,  1006, (LPBYTE)&usriFullName);
if( netRet == NERR_Success ) printf("Success!\n");
else printf( "ERROR: %d Returned from NetUserSetInfo\n", netRet);