使用API中的函数:NetUserEnum来枚举用户 NetUserGetInfo来获取用户的信息

使用API中的函数:NetUserEnum来枚举用户;NetUserGetInfo来获取用户的信息.
思路应该是这样的。 
以下为Windows Conlse Appilication下的枚举所有用户信息的代码(帐户名、帐户状态、是否需要密码(此处可能判断不准确)、上次登录时间、密码是否可更改、密码过期时间):
#include "iostream"
#include "windows.h"
#include "lm.h"
#include "assert.h"
#include "string"
#include "time.h"
using namespace std;

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

string Prev(DWORD n)//帐户权限
{
 switch(n)
 {
 case 0:
  return "Guest";
  break;
 case 1:
  return "User";
  break;
 case 2:
  return "Administrator";
  break;
 default:
  return "Unknown";
  break;
 }
}
string LastLogon(DWORD n)//上次登录时间
{
 if(n==0)
  return "Unknown Last logon time.";
 else
 {
  time_t last_logon_time;
  last_logon_time = n;
  return ctime(&last_logon_time);
 }
}
string ExpiriedTime(unsigned long  n)//密码过期时间
{
 if(n==0||n==NULL)
  return "Never Expiried.";
 else
 {
  time_t current;
  current=n;

return ctime(¤t);
 }
}
//判断帐户状态:数组各个值对应的意义如下
/*
  UF_SCRIPT     登录脚本执行  1   
  UF_ACCOUNTDISABLE   用户帐户不可用  2
      
  UF_HOMEDIR_REQUIRED   要求有用户目录  8
  UF_LOCKOUT     帐户锁定   16
  UF_PASSWD_NOTREQD   不要求用户密码  32
  UF_PASSWD_CANT_CHANGE  不能更改密码  64

以下为帐户类型:
  UF_TEMP_DUPLICATE_ACCOUNT 域用户帐户   256
  UF_NORMAL_ACCOUNT   普通帐户   512

  UF_INTERDOMAIN_TRUST_ACCOUNT域内可信任帐户  2048
  UF_WORKSTATION_TRUST_ACCOUNT工作组可信任帐户 4096
  UF_SERVER_TRUST_ACCOUNT  备份域控制器帐户 8192
  UF_DONT_EXPIRE_PASSWD  密码不过期   65536
*/
int ToBinary(long n,int k)
{
 int num[17];
 int i=16;
 //密码永不过期
 long m=n;
 if(k>17||k<1)
  k=1;
 while(m!=0)
 {
  
  num[i]=m%2;
  m=m/2;
  i--;

 }
 num[i]='\0';
 return num[17-k];
}
int GetAllUser()
{
LPUSER_INFO_1 pBuf = NULL;
LPUSER_INFO_1 pTmpBuf;

DWORD dwLevel = 1;
DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
DWORD dwEntriesRead = 0;
DWORD dwTotalEntries = 0;
DWORD dwResumeHandle = 0;
DWORD i;
DWORD dwTotalCount = 0;
NET_API_STATUS nStatus;
LPTSTR pszServerName = NULL;

do
{
   nStatus = NetUserEnum(NULL,
    dwLevel,
    FILTER_NORMAL_ACCOUNT, // global users
    (LPBYTE*)&pBuf,
    dwPrefMaxLen,
    &dwEntriesRead,
    &dwTotalEntries,
    &dwResumeHandle);

   //
   // If the call succeeds,
   //
   if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
   {
    if ((pTmpBuf = pBuf) != NULL)
    {
    //
    // Loop through the entries.
    //
     for (i = 0; (i < dwEntriesRead); i++)
     {
      assert(pTmpBuf != NULL);

      if (pTmpBuf == NULL)
      {
       fprintf(stderr, "An access violation has occurred\n");
       break;
      }
      //
      // Print the name of the user account.
      //
   /*
   typedef struct _USER_INFO_1 {
     LPWSTR    usri1_name;   //用户名
     LPWSTR    usri1_password;  //用户密码
     DWORD     usri1_password_age; //当前密码使用时间(单位:秒)
     DWORD     usri1_priv;   //用户权限 0 来宾; 1 普通用户; 2 管理员.
     LPWSTR    usri1_home_dir;  //
     LPWSTR    usri1_comment;  //备注,可能为NULL
     DWORD     usri1_flags;   //
     LPWSTR    usri1_script_path; //返回Unicode string,包含用户登录的脚本。可以为NULL
    }USER_INFO_1, *PUSER_INFO_1, *LPUSER_INFO_1;

*/
  wprintf(L"Account No.%d\n",dwTotalCount+1);

  LPUSER_INFO_3 bufptr=NULL;
  LPUSER_INFO_2 buf=NULL;
  NET_API_STATUS t3,t2;
  t3=NetUserGetInfo(0,pTmpBuf->usri1_name,3,(LPBYTE*)&bufptr);
  t2=NetUserGetInfo(0,pTmpBuf->usri1_name,2,(LPBYTE*)&buf);
  //帐户名称:
  wprintf(L"\t Account Name:  %s \n",bufptr->usri3_name);
  //上次登录时间:单位,秒;计算起始时间为1970年1月1日00:00
  if(bufptr->usri3_last_logon==0)
   cout<<"\t Account Last Logon Time: "<<"上次登录时间未知"<<endl;
  else
  {
   long m=bufptr->usri3_last_logon;
   time_t last;
   last = m;
   cout<<"\t Account Last Logon Time: "<<ctime(&last);
  }

  //密码过期时间:单位,秒;计算起始时间为1970年1月1日00:00. 如果为 0 ,表示永不过期
  if(bufptr->usri3_password_expired==0||bufptr->usri3_password_expired==NULL)
   cout<<"\t Password Expiried Time: "<<"密码永不过期"<<endl;
  else
  {
   long n=bufptr->usri3_password_expired;
   time_t pwd;
   pwd = n;
   cout<<"\t Password Expiried Time: "<<ctime(&pwd)<<endl;
  }

  //判断账户过期时间:如果过期时间为:65536*65536-1,表示永不过期
 /* if(buf->usri2_acct_expires==4294967295)
   cout<<"\t Account Expiried Time:"<<"帐户永不过期"<<endl;
  else
  {
   long k=buf->usri2_acct_expires;
   time_t acct;
   acct = k;
   cout<<"\t Account Expiried Time:"<<ctime(&acct)<<endl;
  }
  */
  //判断帐户状态 usri2_flags:
  /*
  value      mean    int   
  UF_SCRIPT     登录脚本执行  1   
  UF_ACCOUNTDISABLE   用户帐户不可用  2
      
  UF_HOMEDIR_REQUIRED   要求有用户目录  8
  UF_LOCKOUT     帐户锁定   16
  UF_PASSWD_NOTREQD   不要求用户密码  32
  UF_PASSWD_CANT_CHANGE  不能更改密码  64

  UF_DONT_EXPIRE_PASSWD  密码不过期   65536

  以下为帐户类型:
  UF_TEMP_DUPLICATE_ACCOUNT 域用户帐户   256
  UF_NORMAL_ACCOUNT   普通帐户   512

  UF_INTERDOMAIN_TRUST_ACCOUNT域内可信任帐户  2048
  UF_WORKSTATION_TRUST_ACCOUNT工作组可信任帐户 4096
  UF_SERVER_TRUST_ACCOUNT  备份域控制器帐户 8192
  
  */
  long n=buf->usri2_flags;
  //帐户状态:
  if(ToBinary(n,2))
   cout<<"\t Account Status : "<<"帐户未启用"<<endl;
  else
   cout<<"\t Account Status : "<<"帐户已启用"<<endl;
  //密码是否可更改:
  if(ToBinary(n,7))
   cout<<"\t Password Need : "<<"不需要密码"<<endl
    <<"\t Password Change : "<<"密码不能更改"<<endl;
  else
   cout<<"\t Password Nedd : "<<"需要密码"<<endl
    <<"\t Password Change : "<<"密码可以更改"<<endl;

pTmpBuf++;
      dwTotalCount++;
     }
    }
   }
   else{
    fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   }
   if (pBuf != NULL)
   {
    NetApiBufferFree(pBuf);
    pBuf = NULL;
   }
}while(nStatus == ERROR_MORE_DATA);

fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);

return 0;
}

void main()
{

 GetAllUser();
 cin.get();
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值