The Win32 Network Management APIs 14

 

 

 

 

 

Changing Elements of User Information Code Snippet Example

 

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 following code example 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:

 

  1. Setting the User Password, Level 1003
  2. Setting the User Privilege, Level 1005
  3. Setting the User Home Directory, Level 1006
  4. Setting the User Comment Field, Level 1007
  5. Setting the User Flags, Level 1008
  6. Setting the User Script Path, Level 1009
  7. Setting The User Authority Flags, Level 1010
  8. Setting The User Full Name, Level 1011

 

All code fragments assume that the user has defined the UNICODE compile directive (or the set through the Visual Studio project’s setting as shown in the following Figure),

 

Setting the Unicode Visual C++ project option

 

And includes the appropriate SDK header files, as follows:

 

#include <windows.h>

// INCL_NET includes all LAN Manager headers if necessary

// #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 Code Snippet Example

 

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)

    wprintf(L"Success with level 1003!\n");

else

    wprintf(L"ERROR: %d returned from NetUserSetInfo level 1003\n", netRet);

 

Setting the User Privilege, Level 1005 Code Snippet Example

 

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;

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

    wprintf(L"Success with level 1005!\n");

else

    wprintf(L"ERROR: %d returned from NetUserSetInfo level 1005\n", netRet);

 

Setting the User Home Directory, Level 1006 Code Snippet Example

 

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 )

    wprintf(L"Success with level 1006!\n");

else

    wprintf(L"ERROR: %d returned from NetUserSetInfo level 1006\n", netRet);

 

 

 

 

< Win32 Network Management APIs 13 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 15 >