c++怎么获取到操作系统的在线用户个数?

求代码,或者用什么API能快速获取到?求大神解答?
如:Windows上的图片说明

操作系统变量homepath,就是存放这个玩意的
C:\Users\br>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\br\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\br //就是这个
LOCALAPPDATA=C:\Users\br\AppData\Local
可以用getenv()函数访问,用putenv()写入
比如

 #include<stdio.h>
#include<stdlib.h>
void main()
{
    printf("%s\n",getenv("homepath")); 
}

操作系统变量homepath,就是存放这个玩意的
C:\Users\br>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\br\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\br //就是这个
LOCALAPPDATA=C:\Users\br\AppData\Local
可以用getenv()函数访问,用putenv()写入

WTSGetActiveConsoleSessionId
https://msdn.microsoft.com/en-us/library/aa383835(v=vs.85).aspx

当前登录的用户信息保存在/var/run/utmp文件中,用户信息结构体在/usr/include/bits/utmp.h定义,可以参考who命令的实现

这几天正好在看这方面的内容,和你分享下,代码如下

 #include <windows.h>
#include<iostream>
#include <string>
#include <WtsApi32.h>

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

using namespace std;

int main()
{
    setlocale(LC_ALL, "chs");
    DWORD dwReserved = 0;
    DWORD dwVersion = 1;
    WTS_SESSION_INFO *ppSessionInfo = NULL;
    DWORD dwSessionCount;

    BOOL result = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,
        dwReserved,
        dwVersion,
        &ppSessionInfo,
        &dwSessionCount
    );
    for (int nSessionIndex=0;nSessionIndex<dwSessionCount;nSessionIndex++)
    {
        BOOL bResult = FALSE;
        LPWSTR  pBuffer = NULL;
        PWTSINFO  pWtsinfo = NULL;
        DWORD dwBufferLen = 0;
        bResult = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
            ppSessionInfo[nSessionIndex].SessionId,
            WTSUserName,
            &pBuffer,
            &dwBufferLen);
        if (bResult)
        {
            if (wcslen(pBuffer)!=0)
            {
                wcout << pBuffer << endl;
            }
            WTSFreeMemory(pBuffer);
        }
    }
    getchar();
    return 0;
}

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: "<<"上次登录时间未知"< else
{
long m=bufptr->usri3_last_logon;
time_t last;
last = m;
cout<<"\t Account Last Logon Time: "< }
//密码过期时间:单位,秒;计算起始时间为1970年1月1日00:00. 如果为 0 ,表示永不过期
if(bufptr->usri3_password_expired==0||bufptr->usri3_password_expired==NULL)
cout<<"\t Password Expiried Time: "<<"密码永不过期"< else
{
long n=bufptr->usri3_password_expired;
time_t pwd;
pwd = n;
cout<<"\t Password Expiried Time: "< }
//判断账户过期时间:如果过期时间为:65536*65536-1,表示永不过期
/* if(buf->usri2_acct_expires==4294967295)
cout<<"\t Account Expiried Time:"<<"帐户永不过期"< 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);