Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.



C++
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);
Posted
Updated 7-Jan-13 3:55am
v2

1 solution

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):

C++
#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;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900