[WinAPI] Cum enumar userii de pe computer?

Folosire Windows API in programe C/C++ (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

[WinAPI] Cum enumar userii de pe computer?

Post by Ovidiu Cucu » 21 Jul 2011, 22:58

Intrebare
Cum enumar conturile utilizatorilor de pe computer?

Raspuns
Se poate folosi functia NetQueryDisplayInformation.

Exemplu

Code: Select all

#include <windows.h>
#include <lm.h>
#include <list>
#pragma comment(lib, "Netapi32.lib")

struct USER_ACCOUNT_INFO
{
   std::wstring sName;
   std::wstring sFullName;
   std::wstring sComment;
};

NET_API_STATUS GetUserAccountsInfo(std::list<USER_ACCOUNT_INFO>& listUsers)
{
   listUsers.clear();
   PNET_DISPLAY_USER pBuffer = NULL, pInfo = NULL;
   NET_API_STATUS ret = NERR_Success;
   DWORD dwRecCount = 0, dwIndex = 0;
   do
   {
      // for local computer, pass NULL in first parameter
      ret = ::NetQueryDisplayInformation(NULL, 1, dwIndex, 1000, 
                      MAX_PREFERRED_LENGTH, &dwRecCount, (PVOID*)&pBuffer);

      if((NERR_Success == ret) || (ERROR_MORE_DATA == ret))
      {
         pInfo = pBuffer;
         for(DWORD dwRecIndex = 0; dwRecIndex < dwRecCount; dwRecIndex++)
         {
            USER_ACCOUNT_INFO uai;
            uai.sName = pInfo->usri1_name;
            uai.sFullName = pInfo->usri1_full_name;
            uai.sComment = pInfo->usri1_comment;
            listUsers.push_back(uai);

            dwIndex = pInfo->usri1_next_index;
            pInfo++;
         }
         ::NetApiBufferFree(pBuffer);
      }
   }while (ret == ERROR_MORE_DATA);

   return ret;
}
Resurse << Back to Windows API Index



Post Reply