[问题]使用WTS来查询使用者登入的资讯

楼主: jayzhuang (Jay)   2019-11-14 19:28:18
开发平台(Platform): (Ex: Win10, Linux, ...)
Win10
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
vs2019
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
C#是使用Cassia
C++目前没有
问题(Question):
各位大大好,在下目前公司需要我做个新功能,
要能让系统抓取AD网域内各台电脑的使用者帐户登入的资讯
基本的:session id、网域名称、电脑名称、登入时间、连线状态
在下有两个状况想要询问一下
1.使用WTSQuerySessionInformation来查询帐号登入的资讯
如下图1是使用query session的方式来找寻,希望可以显示的是这样

但我目前只能显示session id、所在的网域群组、使用者名称
还差了登入时间与登入状态
(明明用了WTSLogonTime与WTSConnectState)还是不给显示....
喂入的资料(Input):
(目前都先输入在程式码内)
预期的正确结果(Expected Output):
类似这样:(1.47是网域内其中一台电脑)

错误结果(Wrong Output):
我把此程式码放入到1.47内执行的结果:

程式码(Code):(请善用置底文网页, 记得排版,禁止使用图档)
#include <Windows.h>
#include <vector>
#include <string>
#include <iostream>
#include <WtsApi32.h>
#include <tchar.h>
#pragma comment(lib, "WtsApi32.lib")
using namespace std;
//
typedef std::basic_string<TCHAR> tstring;
//Get current sessions
bool EnumSessionIds(std::vector<DWORD>& list)
{
list.clear();
WTS_SESSION_INFO* pSI = NULL;
DWORD dwSICount;
BOOL bRes = WTSEnumerateSessions(WTS_CURRENT_SERVER,0,1,&pSI,&dwSICount);
if(bRes==0)
return false;
for (unsigned int i = 0; i < dwSICount; i++)
list.push_back(pSI[i].SessionId);
WTSFreeMemory(pSI);
return true;
}
//Get Username from session id
bool GetSessionUserName(DWORD dwSessionID,tstring& username)
{
LPTSTR pBuffer = NULL;
DWORD dwBufferLen;
bool bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, WTSUserName, &pBuffer, &dwBufferLen);
if (bRes == FALSE)
return false;
username = pBuffer;
WTSFreeMemory(pBuffer);
return true;
}
//Get domain name from session id
bool GetSessionDomain(DWORD dwSessionID,tstring& domain_name)
{
LPTSTR pBuffer = NULL;
DWORD dwBufferLen;
bool bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, WTSDomainName, &pBuffer, &dwBufferLen);
if (bRes == false)
return false;
domain_name = pBuffer;
WTSFreeMemory(pBuffer);
return true;
}
// Get ConnectState from session id
bool GetConnectState(DWORD dwSessionID, tstring& ConnectState)
{
LPTSTR pBuffer = NULL;
DWORD dwBufferLen;
bool bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, WTSConnectState, &pBuffer, &dwBufferLen);
if (bRes == false)
return false;
ConnectState = pBuffer;
WTSFreeMemory(pBuffer);
return true;
}
//Get LogonTime from session id
bool GetLogonTime(DWORD dwSessionID, tstring& logontime)
{
LPTSTR pBuffer = NULL;
DWORD dwBufferLen;
bool bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionID, WTSLogonTime, &pBuffer, &dwBufferLen);
if (bRes == false)
return false;
logontime = pBuffer;
WTSFreeMemory(pBuffer);
return true;
}
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
//显示电脑端的使用者帐户的session id列表
std::vector<DWORD> sessionIds;
bool bRes = EnumSessionIds(sessionIds);
if (!bRes)
{
// error
return 0;
}
// enum sessions-> session列表,透过vector显示
std::vector<DWORD>::iterator iter;
for (iter = sessionIds.begin(); iter != sessionIds.end(); iter++)
{
cout << "

Links booklink

Contact Us: admin [ a t ] ucptt.com