The Win32 Network Management APIs 20

 

 

 

 

 

NetUserGetInfo() Function Program Example

 

The NetUserGetInfo() function retrieves information about a particular user account on a server. If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management user functions. If you call this function on a domain controller that is running Active Directory, access is allowed or denied based on the access control list (ACL) for the securable object. The default ACL permits all authenticated users and members of the "Pre-Windows2000 compatible access" group to view the information. If you call this function on a member server or workstation, all authenticated users can view the information.

The security descriptor of the User object is used to perform the access check for this function.

User account names are limited to 20 characters and group names are limited to 256 characters. In addition, account names cannot be terminated by a period and they cannot include commas or any of the following printable characters: ", /, \, [, ], :, |, <, >, +, =, ;, ?, *. Names also cannot include characters in the range 1-31, which are nonprintable.

The following code sample demonstrates how to retrieve information about a particular user account with a call to the NetUserGetInfo() function. The sample calls NetUserGetInfo(), specifying various information levels. If the call succeeds, the code prints information about the user account. Finally, the sample frees the memory allocated for the information buffer.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.

 

NetUserGetInfo() Function Program Example: Creating new Win32 console mode application project

 

Then, add the source file and give it a suitable name.

 

NetUserGetInfo() Function Program Example: Adding new C++ source file

 

Then, add the following source code.

 

/* For Unicode if needed

#ifndef UNICODE

#define UNICODE

#endif  */

 

// Include the following libraries during the link time

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

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

 

#include <windows.h>

#include <stdio.h>

#include <assert.h>

#include <lm.h>

// for ConvertSidToStringSid() function

#include <sddl.h>

 

int wmain(int argc, wchar_t * argv[])

{

    DWORD dwLevel = 0;

 

    LPUSER_INFO_0 pBuf = NULL;

    LPUSER_INFO_1 pBuf1 = NULL;

    LPUSER_INFO_2 pBuf2 = NULL;

    LPUSER_INFO_3 pBuf3 = NULL;

    LPUSER_INFO_4 pBuf4 = NULL;

    LPUSER_INFO_10 pBuf10 = NULL;

    LPUSER_INFO_11 pBuf11 = NULL;

    LPUSER_INFO_20 pBuf20 = NULL;

    LPUSER_INFO_23 pBuf23 = NULL;

 

    NET_API_STATUS nStatus;

    LPTSTR sStringSid = NULL;

 

    int i = 0;

    int j = 0;

 

    if (argc != 3)

    {

        fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);

        wprintf(L"Example: %s \\\\Kambinggedik Mike\n", argv[0]);

        exit(1);

    }

 

    while (i < 24)

      {

        // Call the NetUserGetInfo() function.

        dwLevel = i;

        wprintf(L"\nCalling NetUserGetinfo() with Servername = %s Username = %s Level = %d\n", argv[1], argv[2], dwLevel);

        nStatus = NetUserGetInfo(argv[1], argv[2], dwLevel, (LPBYTE *) & pBuf);

 

        // If the call succeeds, print the user information.

        if (nStatus == NERR_Success)

            {

            if (pBuf != NULL)

            {

                wprintf(L"NetUserGetinfo() looks pretty fine!\n");

                switch (i)

                {

                case 0:

                    wprintf(L"\tUser account name: %s\n", pBuf->usri0_name);

                    break;

                case 1:

                    pBuf1 = (LPUSER_INFO_1) pBuf;

                    wprintf(L"\tUser account name: %s\n", pBuf1->usri1_name);

                    wprintf(L"\tPassword: %s\n", pBuf1->usri1_password);

                    wprintf(L"\tPassword age (seconds): %d\n",pBuf1->usri1_password_age);

                    wprintf(L"\tPrivilege level: %d\n", pBuf1->usri1_priv);

                    wprintf(L"\tHome directory: %s\n", pBuf1->usri1_home_dir);

                    wprintf(L"\tUser comment: %s\n", pBuf1->usri1_comment);

                    wprintf(L"\tFlags (in hex): %x\n", pBuf1->usri1_flags);

                    wprintf(L"\tScript path: %s\n", pBuf1->usri1_script_path);

                    break;

                case 2:

                    pBuf2 = (LPUSER_INFO_2) pBuf;

                    wprintf(L"\tUser account name: %s\n", pBuf2->usri2_name);

                    wprintf(L"\tPassword: %s\n", pBuf2->usri2_password);

                    wprintf(L"\tPassword age (seconds): %d\n",pBuf2->usri2_password_age);

                    wprintf(L"\tPrivilege level: %d\n", pBuf2->usri2_priv);

                    wprintf(L"\tHome directory: %s\n", pBuf2->usri2_home_dir);

                    wprintf(L"\tComment: %s\n", pBuf2->usri2_comment);

                    wprintf(L"\tFlags (in hex): %x\n", pBuf2->usri2_flags);

                    wprintf(L"\tScript path: %s\n", pBuf2->usri2_script_path);

                    wprintf(L"\tAuth flags (in hex): %x\n",pBuf2->usri2_auth_flags);

                    wprintf(L"\tFull name: %s\n", pBuf2->usri2_full_name);

                    wprintf(L"\tUser comment: %s\n", pBuf2->usri2_usr_comment);

                    wprintf(L"\tParameters: %s\n", pBuf2->usri2_parms);

                    wprintf(L"\tWorkstations: %s\n", pBuf2->usri2_workstations);

                    wprintf(L"\tLast logon (seconds since January 1, 1970 GMT): %d\n",pBuf2->usri2_last_logon);

                    wprintf(L"\tLast logoff (seconds since January 1, 1970 GMT): %d\n",pBuf2->usri2_last_logoff);

                    wprintf(L"\tAccount expires (seconds since January 1, 1970 GMT): %d\n",pBuf2->usri2_acct_expires);

                    wprintf(L"\tMax storage: %d\n", pBuf2->usri2_max_storage);

                    wprintf(L"\tUnits per week: %d\n",pBuf2->usri2_units_per_week);

                    wprintf(L"\tLogon hours:");

 

                    for (j = 0; j < 21; j++)

                    {

                        wprintf(L" %x", (BYTE) pBuf2->usri2_logon_hours[j]);

                    }

                    wprintf(L"\n");

                    wprintf(L"\tBad password count: %d\n",pBuf2->usri2_bad_pw_count);

                    wprintf(L"\tNumber of logons: %d\n",pBuf2->usri2_num_logons);

                    wprintf(L"\tLogon server: %s\n", pBuf2->usri2_logon_server);

                    wprintf(L"\tCountry code: %d\n", pBuf2->usri2_country_code);

                    wprintf(L"\tCode page: %d\n", pBuf2->usri2_code_page);

                    break;

                case 4:

                    pBuf4 = (LPUSER_INFO_4) pBuf;

                    wprintf(L"\tUser account name: %s\n", pBuf4->usri4_name);

                    wprintf(L"\tPassword: %s\n", pBuf4->usri4_password);

                    wprintf(L"\tPassword age (seconds): %d\n",pBuf4->usri4_password_age);

                    wprintf(L"\tPrivilege level: %d\n", pBuf4->usri4_priv);

                    wprintf(L"\tHome directory: %s\n", pBuf4->usri4_home_dir);

                    wprintf(L"\tComment: %s\n", pBuf4->usri4_comment);

                    wprintf(L"\tFlags (in hex): %x\n", pBuf4->usri4_flags);

                    wprintf(L"\tScript path: %s\n", pBuf4->usri4_script_path);

                    wprintf(L"\tAuth flags (in hex): %x\n",pBuf4->usri4_auth_flags);

                    wprintf(L"\tFull name: %s\n", pBuf4->usri4_full_name);

                    wprintf(L"\tUser comment: %s\n", pBuf4->usri4_usr_comment);

                    wprintf(L"\tParameters: %s\n", pBuf4->usri4_parms);

                    wprintf(L"\tWorkstations: %s\n", pBuf4->usri4_workstations);

                    wprintf(L"\tLast logon (seconds since January 1, 1970 GMT): %d\n",pBuf4->usri4_last_logon);

                    wprintf(L"\tLast logoff (seconds since January 1, 1970 GMT): %d\n",pBuf4->usri4_last_logoff);

                    wprintf(L"\tAccount expires (seconds since January 1, 1970 GMT): %d\n",pBuf4->usri4_acct_expires);

                    wprintf(L"\tMax storage: %d\n", pBuf4->usri4_max_storage);

                    wprintf(L"\tUnits per week: %d\n",pBuf4->usri4_units_per_week);

                    wprintf(L"\tLogon hours:");

 

                    for (j = 0; j < 21; j++)

                    {

                        wprintf(L" %x", (BYTE) pBuf4->usri4_logon_hours[j]);

                    }

 

                    wprintf(L"\n");

                    wprintf(L"\tBad password count: %d\n",pBuf4->usri4_bad_pw_count);

                    wprintf(L"\tNumber of logons: %d\n",pBuf4->usri4_num_logons);

                    wprintf(L"\tLogon server: %s\n", pBuf4->usri4_logon_server);

                    wprintf(L"\tCountry code: %d\n", pBuf4->usri4_country_code);

                    wprintf(L"\tCode page: %d\n", pBuf4->usri4_code_page);

 

                    if (ConvertSidToStringSid(pBuf4->usri4_user_sid, &sStringSid))

                    {

                        wprintf(L"ConvertSidToSTringSid() is OK!\n");

                        wprintf(L"\tUser SID: %s\n", sStringSid);

                        LocalFree(sStringSid);

                    }

                    else

                        wprintf(L"ConvertSidToSTringSid() failed with error %d\n",GetLastError());

                   

                    wprintf(L"\tPrimary group ID: %d\n",pBuf4->usri4_primary_group_id);

                    wprintf(L"\tProfile: %s\n", pBuf4->usri4_profile);

                    wprintf(L"\tHome directory drive letter: %s\n",pBuf4->usri4_home_dir_drive);

                    wprintf(L"\tPassword expired information: %d\n",pBuf4->usri4_password_expired);

                    break;

                case 10:

                    pBuf10 = (LPUSER_INFO_10) pBuf;

                    wprintf(L"\tUser account name: %s\n", pBuf10->usri10_name);

                    wprintf(L"\tComment: %s\n", pBuf10->usri10_comment);

                    wprintf(L"\tUser comment: %s\n",pBuf10->usri10_usr_comment);

                    wprintf(L"\tFull name: %s\n", pBuf10->usri10_full_name);

                    break;

                case 11:

                    pBuf11 = (LPUSER_INFO_11) pBuf;

                    wprintf(L"\tUser account name: %s\n", pBuf11->usri11_name);

                    wprintf(L"\tComment: %s\n", pBuf11->usri11_comment);

                    wprintf(L"\tUser comment: %s\n",pBuf11->usri11_usr_comment);

                    wprintf(L"\tFull name: %s\n", pBuf11->usri11_full_name);

                    wprintf(L"\tPrivilege level: %d\n", pBuf11->usri11_priv);

                    wprintf(L"\tAuth flags (in hex): %x\n",pBuf11->usri11_auth_flags);

                    wprintf(L"\tPassword age (seconds): %d\n",pBuf11->usri11_password_age);

                    wprintf(L"\tHome directory: %s\n", pBuf11->usri11_home_dir);

                    wprintf(L"\tParameters: %s\n", pBuf11->usri11_parms);

                    wprintf(L"\tLast logon (seconds since January 1, 1970 GMT): %d\n",pBuf11->usri11_last_logon);

                    wprintf(L"\tLast logoff (seconds since January 1, 1970 GMT): %d\n",pBuf11->usri11_last_logoff);

                    wprintf(L"\tBad password count: %d\n",pBuf11->usri11_bad_pw_count);

                    wprintf(L"\tNumber of logons: %d\n",pBuf11->usri11_num_logons);

                    wprintf(L"\tLogon server: %s\n",pBuf11->usri11_logon_server);

                    wprintf(L"\tCountry code: %d\n",pBuf11->usri11_country_code);

                    wprintf(L"\tWorkstations: %s\n",pBuf11->usri11_workstations);

                    wprintf(L"\tMax storage: %d\n", pBuf11->usri11_max_storage);

                    wprintf(L"\tUnits per week: %d\n",pBuf11->usri11_units_per_week);

                    wprintf(L"\tLogon hours:");

 

                    for (j = 0; j < 21; j++)

                    {

                        wprintf(L" %x", (BYTE) pBuf11->usri11_logon_hours[j]);

                    }

                   

                    wprintf(L"\n");

                    wprintf(L"\tCode page: %d\n", pBuf11->usri11_code_page);

                    break;

                case 20:

                    pBuf20 = (LPUSER_INFO_20) pBuf;

                    wprintf(L"\tUser account name: %s\n", pBuf20->usri20_name);

                    wprintf(L"\tFull name: %s\n", pBuf20->usri20_full_name);

                    wprintf(L"\tComment: %s\n", pBuf20->usri20_comment);

                    wprintf(L"\tFlags (in hex): %x\n", pBuf20->usri20_flags);

                    wprintf(L"\tUser ID: %u\n", pBuf20->usri20_user_id);

                    break;

                case 23:

                    pBuf23 = (LPUSER_INFO_23) pBuf;

                    wprintf(L"\tUser account name: %s\n", pBuf23->usri23_name);

                    wprintf(L"\tFull name: %s\n", pBuf23->usri23_full_name);

                    wprintf(L"\tComment: %s\n", pBuf23->usri23_comment);

                    wprintf(L"\tFlags (in hex): %x\n", pBuf23->usri23_flags);

 

                    if (ConvertSidToStringSid(pBuf23->usri23_user_sid, &sStringSid))

                    {

                        wprintf(L"ConvertSidToSTringSid() is OK!\n");

                        wprintf(L"\tUser SID: %s\n", sStringSid);

                        LocalFree(sStringSid);

                    }

                    else

                        wprintf(L"ConvertSidToSTringSid() failed with error %d\n",GetLastError());

                    break;

                default:

                    break;

                }

            }

        }

        // Otherwise, print the system error.

        else

            fwprintf_s(stderr, L"NetUserGetinfo() failed with error: %d\n", nStatus);

        // Free the allocated memory.

        if (pBuf != NULL)

            NetApiBufferFree(pBuf);

        switch (i)

            {

        case 0:

        case 1:

        case 10:

            i++;

            break;

        case 2:

            i = 4;

            break;

        case 4:

            i = 10;

            break;

        case 11:

            i = 20;

            break;

        case 20:

            i = 23;

            break;

        default:

            i = 24;

            break;

        }

    }

    return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

NetUserGetInfo() Function Program Example: A sample console program output

 

 

 

 

< Win32 Network Management APIs 19 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 21 >