Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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);
 
Posted 6-Jan-13 20:09pm
Edited 7-Jan-13 3:55am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 6,651
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,542


Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100