Platform SDK: Network Management |
The network management functions provide a variety of information levels to assist in changing user information. Some levels require administrative privileges to execute successfully.
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:
#define UNICODE #include <windows.h> #define INCL_NET #include <lm.h>
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 TEXT("new_password")
. . . USER_INFO_1003 usriSetPassword; DWORD netRet = 0; // // Set the usri1003_password member 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);
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.
USER_INFO_1005 usriPriv; DWORD netRet = 0; // // Set the usri1005_priv member 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);
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 TEXT("C:\\USER\USER_PATH") USER_INFO_1006 usriHomeDir; // // Set the usri1006_home_dir member 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);
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 TEXT("This is my Comment Text for the user") USER_INFO_1007 usriComment; // // Set the usri1007_comment member to point to // a valid Unicode comment string. // // 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);
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 and LAN Manager networks. Trying to set other flags without setting UF_SCRIPT on a Windows NT/Windows 2000 or LAN Manager network 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. // // 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);
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 "C:\\BIN\\MYSCRIPT.BAT" USER_INFO_1009 usriScrPath; // // Set the usri1009_script_path member 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);
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 a constant // containing the appropriate 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);
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 TEXT("Joe B. User") USER_INFO_1011 usriFullName; // // Set the usri1011_full_name member 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);