更改用户信息的元素

网络管理功能提供各种信息级别来帮助更改用户信息。 某些级别需要管理权限才能成功执行。 有关调用需要管理员权限的函数的详细信息,请参阅 使用特殊特权运行

本主题中的示例代码演示如何通过调用 NetUserSetInfo 函数更改用户信息的多个元素。 该代码使用各种网络管理信息结构。

更改用户信息时,最好使用该信息的特定级别。 这可以防止在使用较低级别的值时意外重置不相关的信息。

以下代码示例演示了如何设置一些更常用的级别:

所有代码片段都假定用户已定义 UNICODE 编译指令并包含相应的 SDK 头文件,如下所示:

#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;

设置用户密码,级别 1003

下面的代码片段演示如何通过调用 NetUserSetInfo 函数将用户的密码设置为已知值。 USER_INFO_1003主题包含其他信息。

#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);

设置用户权限,级别 1005

以下代码片段演示如何通过调用 NetUserSetInfo 函数指定分配给用户的权限级别。 USER_INFO_1005主题包含其他信息。 有关帐户特权的详细信息,请参阅 特权授权常量

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);

设置用户主目录,级别 1006

以下代码片段演示如何通过调用 NetUserSetInfo 函数来指定用户主目录的路径。 目录可以是硬编码路径或有效的 Unicode 路径。 USER_INFO_1006主题包含其他信息。

#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);

设置用户注释字段,级别 1007

以下代码片段演示如何通过调用 NetUserSetInfo 函数将注释与用户相关联。 USER_INFO_1007主题包含其他信息。

#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);

设置用户标志,级别 1008

以下代码片段演示如何通过调用 NetUserSetInfo 函数来设置用户标志。 USER_INFO_1008主题包含标志的有效值列表和每个标志的说明。

请注意,必须为 Windows NT、Windows 2000、Windows XP 和 LAN Manager 网络设置UF_SCRIPT标志。 尝试在这些网络上设置其他标志而不设置UF_SCRIPT将导致 NetUserSetInfo 函数失败。

#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);

设置用户脚本路径,级别 1009

以下代码片段演示如何通过调用 NetUserSetInfo 函数来设置特定用户的登录脚本文件的路径。 脚本文件可以是 。CMD 文件、.EXE文件或.BAT文件。 字符串也可以为 null。 USER_INFO_1009主题包含其他信息。

#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);

设置用户颁发机构标志,级别 1010

以下代码片段演示如何通过调用 NetUserSetInfo 函数为用户设置操作员特权标志。 USER_INFO_1010主题包含标志的有效值列表和每个标志的说明。

#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);

设置用户全名,级别 1011

以下代码片段演示如何通过调用 NetUserSetInfo 函数来设置用户的全名。 USER_INFO_1011主题包含其他信息。

#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);