|
Hi,
The NetUserGetInfo function is an old derelict function. (pun intentional) Maybe you could use the Security and Identity[^] framework to get session information.
Disclaimer: This is a code sample, it doesn't handle dynamic daylight saving time. Also, the pointer arithmetic could be refactored.
#pragma comment(lib, "Secur32.lib")
#include <iostream>
#include <windows.h>
#include <ntsecapi.h>
INT main()
{
DWORD lc = 0;
DWORD status = 0;
PLUID list = nullptr;
LsaEnumerateLogonSessions(&lc, &list);
for (DWORD i = 0; i < lc; i++)
{
PSECURITY_LOGON_SESSION_DATA pData;
status = LsaGetLogonSessionData((PLUID)((INT_PTR)list + sizeof(LUID) * i), &pData);
if (0 == status)
{
if (Interactive == pData->LogonType)
{
FILETIME ft;
SYSTEMTIME st_utc, st_local;
TIME_ZONE_INFORMATION tzi;
ft.dwHighDateTime = pData->LogonTime.HighPart;
ft.dwLowDateTime = pData->LogonTime.LowPart;
GetTimeZoneInformation(&tzi);
FileTimeToSystemTime(&ft, &st_utc);
SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_local);
wprintf(L"UserName: %s\n", pData->UserName.Buffer);
wprintf(L"Last logon %s: %d/%d/%d-%d:%d:%d:%d\n", tzi.StandardName, st_local.wMonth, st_local.wDay, st_local.wYear, st_local.wHour, st_local.wMinute, st_local.wSecond, st_local.wMilliseconds);
}
LsaFreeReturnBuffer(pData);
}
}
}
I noticed that the C# guys have much better time zone tools[^]. For some reason nobody invested any time into doing this for the public native API.
So I whipped up something real quick for converting to an arbitrary time zone:
typedef struct _REG_TZI_FORMAT
{
LONG Bias;
LONG StandardBias;
LONG DaylightBias;
SYSTEMTIME StandardDate;
SYSTEMTIME DaylightDate;
} REG_TZI_FORMAT;
BOOL ConvertToTzSpecificTimeZone(const wchar_t time_zone[], TIME_ZONE_INFORMATION * tzi, const SYSTEMTIME * lpUniversalTime, const LPSYSTEMTIME lpLocalTime)
{
HKEY k;
DWORD size = 0;
REG_TZI_FORMAT tzdb;
BOOL bRet = FALSE;
SecureZeroMemory(tzi, sizeof(tzi));
LSTATUS status = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", &k);
if (ERROR_SUCCESS == status)
{
size = sizeof(REG_TZI_FORMAT);
status = RegGetValueW(k, time_zone, L"TZI", RRF_RT_REG_BINARY, 0, &tzdb, &size);
if (ERROR_SUCCESS == status)
{
tzi->Bias = tzdb.Bias;
tzi->DaylightBias = tzdb.DaylightBias;
tzi->DaylightDate = tzdb.DaylightDate;
tzi->StandardBias = tzdb.StandardBias;
tzi->StandardDate = tzdb.StandardDate;
wcscpy_s(tzi->StandardName, time_zone);
bRet = SystemTimeToTzSpecificLocalTime(tzi, lpUniversalTime, lpLocalTime);
}
RegCloseKey(k);
}
return bRet;
}
You would use it something like this:
if (ConvertToTzSpecificTimeZone(L"Tokyo Standard Time", &tzi, &st_utc, &st_local))
{
wprintf(L"Last logon %s: %d/%d/%d-%d:%d:%d:%d\n", tzi.StandardName, st_local.wMonth, st_local.wDay, st_local.wYear, st_local.wHour, st_local.wMinute, st_local.wSecond, st_local.wMilliseconds);
}
Best Wishes,
-David Delaune
edit: added call to RegCloseKey
modified 6-Jun-21 14:30pm.
|
|
|
|
|
Hey, @Randor, that works *great* !!!!!!!!!!! Thank you so much!!
I do have *one* minor issue with it, though...
Is this intended to be a 64-bit-only operation??
I use MinGW, not Visual C++, for all of my app development, and at this point, I still use a 32-bit compiler... However, I cannot build this with 32-bit MinGW:
> g++ -Wall -O2 login_lsa.cpp -o login_lsa.exe -lsecur32
login_lsa.cpp: In function 'INT main()':
login_lsa.cpp:14: error: 'LsaEnumerateLogonSessions' was not declared in this scope
login_lsa.cpp:17: error: 'PSECURITY_LOGON_SESSION_DATA' was not declared in this scope
login_lsa.cpp:17: error: expected ';' before 'pData'
login_lsa.cpp:19: error: 'pData' was not declared in this scope
login_lsa.cpp:19: error: 'LsaGetLogonSessionData' was not declared in this scope
I searched through *all* .h and .hpp files in c:\mingw, and these functions were not declared anywhere...
Mind ye, it *does* build and run just fine with 64-bit MinGW.
(in case anyone is not familiar with MinGW, it stands for Minimal Gnu for Windows, and is a port of the GNU toolchain to Windows, using Windows libraries for most services.)
My MinGW 32-bit toolchain *does* have netsecapi.h and secur32.lib (in its format), but these LSA functions appear to not be included...
I also tried current TDM build of MinGW, which is gcc 10.3.0, but it does not contain these functions either...
|
|
|
|
|
Derell Licht wrote: Is this intended to be a 64-bit-only operation??
Not sure what gave you that idea. That code will build and function correctly on both 32/64 bit platforms. The library 'Secur32.lib' was named a really long time ago and just like the path'C:\Windows\System32' the decision was made to keep the old name.
Derell Licht wrote: I searched through *all* .h and .hpp files in c:\mingw, and these functions were not declared anywhere...
I very rarely use the MinGW compiler tools. The errors you are getting are simple #include errors. I checked the msys2 source code repository and found mingw-w64/ntsecapi.h which is dated Dec 13, 2013 has all of the required function declarations.
Sounds like you just need to update your development environment.
Best Wishes,
-David Delaune
|
|
|
|
|
Ahhh!! So you *are using 64-bit compiler then ?!?!
mingw-w64 is the 64-bit compiler... and in my 64-bit compiler, they are also present, but not in the 32-bit compiler...
Actually, though, I found a way to make this work with Mingw32...
I found the clue in a page for gkrellm application, which has a support file to add support for system functions which are missing in default MinGW-32 package...
What he did is use LoadLibrary() to load and obtain a handle for secur32.dll,
then used GetProcAddress() to get pointers to the required functions:
hSecur32 = LoadLibraryW(L"secur32.dll");
if (hSecur32 != NULL)
{
pfLELS = (pfLsaEnumerateLogonSessions)GetProcAddress(hSecur32, "LsaEnumerateLogonSessions");
if (pfLELS == NULL)
{
wprintf(L"Could not get address for LsaEnumerateLogonSessions() in secur32.dll\n");
}
That worked beautifully for me!!
And yes, your code *does* in fact return the login times that I was looking for; Thank You again!!
|
|
|
|
|
Derell Licht wrote: That worked beautifully for me!!
And yes, your code *does* in fact return the login times that I was looking for; Thank You again!!
You are welcome.
Best Wishes,
-David Delaune
|
|
|
|
|
Well, since this works so nicely, I thought I would include the complete, working demo function here... however, apparently, I cannot actually attach a file in a message, so I will just link to the file in my Github repository, in the program that it will be used in.
derbar/login_lsa.cpp at master · DerellLicht/derbar · GitHub[^]
Be sure to enable the STAND_ALONE macro to build stand-alone utility, either in the code, or by putting the macro on the command line, like this:
g++ -Wall -O2 -DSTAND_ALONE=1 login_lsa.cpp -o login_lsa.exe -lsecur32
It works very nicely!!
|
|
|
|
|
|
Actually, that's a good idea... I haven't created an article here in awhile...
I did so... here is a link to the article:
Determine Time Since Last Logon[^]
modified 8-Jun-21 10:11am.
|
|
|
|
|
thanks for sharing that.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Declare a structure "Student" which contains the following information:
1
i.
ii.
Student ID
Grade for 5 subjects
2. Write a function to
i
search for a Student ID and if exists write "Found", otherwise, write "Not Found"
Write the Student found (ID and Grade) to a file with file name "StudentFile.txt
In the main (0 function:
3.
i
ii.
ii.
Read the data of an array of 10 students from the user (i.e., keyboard)
Read from the user a student ID to search for
Invoke the function search to search for the Student ID read by the user
|
|
|
|
|
Well, it likes like your homework. But what is your "urgent question"?
|
|
|
|
|
|
MAHMOUD ALI Jun2021 wrote: DO YOU KNOW ANSWER ??
To WHAT question? 
|
|
|
|
|
|
See #11 here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Yes, I know the answer. So do many, many other people here.
You have to write some code to accomplish what is specified in this "assignment".
Pay special attention to the first part of that answer. "YOU have to write some code..."
Nobody is going to write this for you.
|
|
|
|
|
Drawing on another user's recent comment...
Do you think Usain Bolt got fast by having other people do his training?
|
|
|
|
|
Your question was so urgent you forgot to even ask it. 
|
|
|
|
|
I want through parameterizing a SQL query in my C++/MFC app using recordset parameterized class, but I am facing a problem with passing a UNICODE characters in parameter value
|
|
|
|
|
Zouaoui Billel wrote: but I am facing a problem Sorry, but there is no way we can guess what you mean. Please provide proper details of the problem, including the data you are using and the actual errors or messages that you see.
|
|
|
|
|
So you kept both your code and the problem description a secret.
How do you expect anyone to help you?
|
|
|
|
|
Message Closed
modified 15-May-23 19:07pm.
|
|
|
|
|
Haven’t seen any standard term. If you need such term, I’d rather go for “inclusion point”. The word “reference” has many uses and adding another one is not going to make things clearer.
Anyway it has no direct relation to the linker. It is purely a textual inclusion and it will go through preprocessor and compiler before.
Mircea
|
|
|
|
|
Message Closed
modified 15-May-23 19:07pm.
|
|
|
|
|
Sorry, I don't understand what you try to accomplish. Are you trying to document how your library has to be used by a client program? In this case, I've seen instructions like:
"... place an include directive to <cool_library.h> in your program"
or even:
"... place an include directive to <cool_library.h> before the include directive for <not_so_cool.h>"
If you are looking for something else, try to explain more.
Mircea
|
|
|
|
|