Changing Elements of User Information

The network management functions provide a variety of information levels to assist in changing user information. Some levels require administrative privileges to execute successfully. For more information about calling functions that require administrator privileges, see Running with Special Privileges.

The sample code in this topic demonstrates how to change several elements of user information by calling the NetUserSetInfo function. The code uses various network management information structures.

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.

Setting some of the more commonly used levels is illustrated in the following code samples:

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


#ifndef UNICODE
#define UNICODE
#endif

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

#pragma comment(lib, "netapi32.lib")

#define SERVER L"test_server_name"
#define USERNAME L"test_user_name"

DWORD netRet = 0;


Setting the User Password, Level 1003

The following code fragment illustrates how to set a user's password to a known value with a call to the NetUserSetInfo function. The USER_INFO_1003 topic contains additional information.


#define PASSWORD L"new_password"

USER_INFO_1003 usriSetPassword;
//
// Set the usri1003_password member to point to a valid Unicode string.
//
// SERVER and USERNAME can be hard-coded strings or pointers to Unicode strings.
//
usriSetPassword.usri1003_password = PASSWORD;
    
netRet = NetUserSetInfo( SERVER, USERNAME, 1003, (LPBYTE)&usriSetPassword, NULL );

if( netRet == NERR_Success ) 
    printf("Success with level 1003!\n");
else 
    printf( "ERROR: %d returned from NetUserSetInfo level 1003\n", netRet);


Setting the User Privilege, Level 1005

The following code fragment illustrates how to specify the level of privilege assigned to a user with a call to the NetUserSetInfo function. The USER_INFO_1005 topic contains additional information. For more information about account privileges, see Privileges and Authorization Constants.


USER_INFO_1005 usriPriv;
//
// Set the usri1005_priv member to the appropriate value.
//
// SERVER and USERNAME can be hard-coded strings or pointers to Unicode strings.
//
usriPriv.usri1005_priv = USER_PRIV_USER;

netRet = NetUserSetInfo( SERVER, USERNAME, 1005, (LPBYTE)&usriPriv, NULL );

if( netRet == NERR_Success ) 
    printf("Success with level 1005!\n");
else 
    printf( "ERROR: %d returned from NetUserSetInfo level 1005\n", netRet);


Setting the User Home Directory, Level 1006

The following code fragment illustrates how to specify the path of a user's home directory with a call to the NetUserSetInfo function. The directory can be a hard-coded path or a valid Unicode path. The USER_INFO_1006 topic contains additional information.


#define HOMEDIR L"C:\\USER\USER_PATH"
USER_INFO_1006 usriHomeDir;
//
// Set the usri1006_home_dir member to point to a valid Unicode string.
//
// SERVER and USERNAME can be hard-coded strings or pointers to Unicode strings.
//
usriHomeDir.usri1006_home_dir = HOMEDIR;

netRet = NetUserSetInfo( SERVER, USERNAME, 1006, (LPBYTE)&usriHomeDir, NULL );

if( netRet == NERR_Success ) 
    printf("Success with level 1006!\n");
else 
    printf( "ERROR: %d returned from NetUserSetInfo level 1006\n", netRet);


Setting the User Comment Field, Level 1007

The following code fragment illustrates how to associate a comment with a user by calling the NetUserSetInfo function. The USER_INFO_1007 topic contains additional information.


#define COMMENT L"This is my Comment Text for the user"
USER_INFO_1007 usriComment;
//
// Set the usri1007_comment member to point to a valid Unicode string.
//
// SERVER and USERNAME can be hard-coded strings or pointers to Unicode strings.
//
usriComment.usri1007_comment = COMMENT;

netRet = NetUserSetInfo( SERVER, USERNAME, 1007, (LPBYTE)&usriComment, NULL );

if( netRet == NERR_Success )
    printf("Success with level 1007!\n");
else
    printf( "ERROR: %d returned from NetUserSetInfo level 1007\n", netRet);


Setting the User Flags, Level 1008

The following code fragment illustrates how to set user flags with a call to the NetUserSetInfo function. The USER_INFO_1008 topic contains a list of valid values for the flags and a description of each flag.

Note that the UF_SCRIPT flag must be set for Windows NT, Windows 2000, Windows XP, and LAN Manager networks. Trying to set other flags without setting UF_SCRIPT on these networks will cause the NetUserSetInfo function to fail.


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


Setting the User Script Path, Level 1009

The following code fragment illustrates how to set the path for the logon script file of a particular user with a call to the NetUserSetInfo function. The script file can be a .CMD file, an .EXE file, or a .BAT file. The string can also be null. The USER_INFO_1009 topic contains additional information.


#define SCRIPT_PATH L"C:\\BIN\\MYSCRIPT.BAT"
USER_INFO_1009 usriScrPath;
//
// Set the usri1009_script_path member to a valid Unicode string.
//
// SERVER and USERNAME can be hard-coded strings or pointers to Unicode strings.
//
usriScrPath.usri1009_script_path = SCRIPT_PATH;
netRet = NetUserSetInfo( SERVER, USERNAME, 1009, (LPBYTE)&usriScrPath, NULL );
if( netRet == NERR_Success ) 
    printf("Success with level 1009!\n");
else 
    printf( "ERROR: %d returned from NetUserSetInfo level 1009\n", netRet);


Setting The User Authority Flags, Level 1010

The following code fragment illustrates how to set the operator privilege flags for a user with a call to the NetUserSetInfo function. The USER_INFO_1010 topic contains a list of valid values for the flags and a description of each flag.


#define AUTHORITY_FLAGS AF_OP_ACCOUNTS
USER_INFO_1010 usriAuthFlags;
//
// Set the usri1010_auth_flags member to the appropriate constant value.
//
// SERVER and USERNAME can be hard-coded strings or pointers to Unicode strings.
//
usriAuthFlags.usri1010_auth_flags = AUTHORITY_FLAGS;
netRet = NetUserSetInfo( SERVER, USERNAME, 1010, (LPBYTE)&usriAuthFlags, NULL);
if( netRet == NERR_Success )
    printf("Success with level 1010!\n");
else
    printf( "ERROR: %d returned from NetUserSetInfo level 1010\n", netRet);


Setting The User Full Name, Level 1011

The following code fragment illustrates how to set a user's full name with a call to the NetUserSetInfo function. The USER_INFO_1011 topic contains additional information.


#define USER_FULL_NAME L"Joe B. User"
USER_INFO_1011 usriFullName;
//
// Set the usri1011_full_name member to a valid Unicode string.
//
// SERVER and USERNAME can be hard-coded strings or pointers to Unicode strings.
//
usriFullName.usri1011_full_name = USER_FULL_NAME;
netRet = NetUserSetInfo( SERVER, USERNAME, 1011, (LPBYTE)&usriFullName, NULL);
if( netRet == NERR_Success ) 
    printf("Success with level 1011!\n");
else 
    printf( "ERROR: %d returned from NetUserSetInfo\n", netRet);


 

 

Community Additions

ADD
Show:
© 2014 Microsoft