20. FAQ o Win API

Q> Как узнать какие пользователи есть на моей (или не моей) машине?
A>

#define STRICT
#include <windows.h>
#include <lm.h>
#include <iostream.h>
#include <tchar.h>

void UserEnum()
{
    BOOL keepGoing = TRUE ;
    DWORD entriesRead, totalEntries ;
    USER_INFO_2 * pInfo = NULL ;
    DWORD resumeHandle = 0 ; // must be 0 to start with
    char nameBuf[ UNLEN + 1 ] ; // constants defined in LMCONS.H
    char commentBuf[ MAXCOMMENTSZ + 1 ] ;
    WCHAR serverName[ 100 ] ;
    lstrcpyW( serverName, L"\\\\PDC" ) ; 
    while ( keepGoing )
    {
        NET_API_STATUS ret = NetUserEnum(
            serverName, 
            2,
            0,
            (LPBYTE *)&pInfo, // Important:  ADDRESS of POINTER
            sizeof( USER_INFO_2 ) * 100, // requested buffer size; 
            &entriesRead,
            &totalEntries,
            &resumeHandle ) ;

        keepGoing = ( ret == ERROR_MORE_DATA ) ;

        if ( ret == 0 || ret == ERROR_MORE_DATA )
        {
            DWORD i ;
            for ( i = 0 ; i < entriesRead ; i++ )
            {
                //  Note that strings in the INFO structures
                //  will ALWAYS be Unicode, regardless of
                //  your settings!  Even though they're declared
                //  as LPTSTR, they're always LPWSTR.
                //  I'm compiling for non-Unicode, so I
                //  convert them to ANSI strings...
                //  Check for NULL pointers in the INFO structure
                LPWSTR pName = (LPWSTR)pInfo[ i ].usri2_name ;
                LPWSTR pComm = (LPWSTR)pInfo[ i ].usri2_comment ;
                if ( pName == NULL )
                {
                    lstrcpy( nameBuf, "(no name!)" ) ;
                }
                else if ( lstrlenW( pName ) == 0 )
                {
                    lstrcpy( nameBuf, "(empty name!)" ) ;
                }
                else
                {
                    WideCharToMultiByte( CP_OEMCP, 0,
                        pName, -1,
                        nameBuf, UNLEN,
                        NULL, NULL ) ;
                }
                if ( pComm == NULL )
                {
                    lstrcpy( commentBuf, "(no comment!)" ) ;
                }
                else if ( lstrlenW( pComm ) == 0 )
                {
                    lstrcpy( commentBuf, "(empty comment!)" ) ;
                }
                else
                {
                    WideCharToMultiByte( CP_OEMCP, 0,
                        pComm, -1,
                        commentBuf, MAXCOMMENTSZ,
                        NULL, NULL ) ;
                }
                cout << nameBuf << ":  " << commentBuf << endl ;
            }
        }
        else
        {
            cout << "NetUserEnum error " << ret << endl ;
        }

        if ( pInfo )
        {
            NetApiBufferFree( pInfo ) ;
            pInfo = NULL ;
        }
    }
}

2000(c)

Электронные Книги по компьютерным программам
На правах рекламы:
Регистрация. | Забыли пароль?
Логин
Пароль
Запомнить меня