win7,如何获取当前用户的登录时间? [英] win7,How to get the current user login time?
本文介绍了win7,如何获取当前用户的登录时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨。
i希望得到当前用户登录系统的时间。
i use IADsUser :: get_LastLogin(),但ADsGetObject返回错误代码。
i从本网站下载的演示中找到代码,我的操作系统是win7。
hi.
i want to get the time when the current user login system.
i use IADsUser::get_LastLogin(),but the ADsGetObject return a error code.
i find the code from a demo that download from this Website,and My operating system is win7.
IADsWinNTSystemInfo *pNTsys;
HRESULT hr = CoCreateInstance(CLSID_WinNTSystemInfo,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADsWinNTSystemInfo,
(void**)&pNTsys);
pNTsys->AddRef();
BSTR bstrCompName;
pNTsys->get_ComputerName(&bstrCompName);
CString cscompName=bstrCompName;
SysFreeString(bstrCompName);
BSTR bstrDomainName;
pNTsys->get_DomainName(&bstrDomainName);
CString CSDomainName=bstrDomainName;
SysFreeString(bstrDomainName);
pNTsys->Release();
//From ADSPath
ADSPath.Format("WinNT://%s/%s",CSDomainName,cscompName);
//Get the container object
//*****************here get error code group not be found
hr=ADsGetObject(ADSPath.AllocSysString(),IID_IADsContainer,(void **)&pUsers);
推荐答案
我认为AD的东西只适用于域控制器(?)。
这是我用于登录时的一些代码的摘录(对不起,如果它有语法错误就编译而不编译):
I think the AD stuff is only for use on a domain controller (?).
Here is an extract of some code I use for login time (sorry it is editted without compile if it has syntax errors):
#include <Ntsecapi.h>
// [link to secur32.lib]
BOOL GetLogonLUID (LUID *pLuid)
{
BOOL bSuccess = FALSE;
HANDLE hThread = NULL;
TOKEN_STATISTICS ts = {0};
DWORD cbReturnLength = sizeof(TOKEN_STATISTICS);
bSuccess = OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hThread);
if (bSuccess)
{
bSuccess = GetTokenInformation (hThread, TokenStatistics, &ts, sizeof(TOKEN_STATISTICS), &cbReturnLength);
if (bSuccess)
*pLuid = ts.AuthenticationId;
}
if (hThread)
CloseHandle (hThread);
return bSuccess;
}
SYSTEMTIME& GetLoginTime()
{
static SYSTEMTIME st = {0};
FILETIME ft = {0};
LUID LogonLuid = {0};
PSECURITY_LOGON_SESSION_DATA pLogonSessionData = NULL;
GetLogonLUID (&LogonLuid);
NTSTATUS ntStatus = LsaGetLogonSessionData (&LogonLuid, &pLogonSessionData);
if (ntStatus == 0)
{
if (pLogonSessionData->LogonTime.QuadPart)
{
FileTimeToLocalFileTime ((const FILETIME *)&pLogonSessionData->LogonTime, &ft);
FileTimeToSystemTime (&ft, &st);
}
}
if (pLogonSessionData)
LsaFreeReturnBuffer(pLogonSessionData);
return st;
}
这篇关于win7,如何获取当前用户的登录时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文