Avatar of sathish_raos
sathish_raos
 asked on

Windows logged in user's SessionId is changed when the machine comes out of hibernate

I have a windows service written in c# which monitors the session of all the logged in windows users, I get the correct session id in the windows service for the logged in users when they lock / unlock the machine.

I have noticed sometimes that when the machine resumes from hibernate and when the user unlocks the machine, the session id for that user is not the actual session id that was allocated to the user instead it is some weird long number, it ranges from 28 to 6422639 or any other big number. I noticed that this does not happen consistently. I checked the session id for the applications running for that user session in task manager and it shows the correct session id there. Has anyone seen this problem? I have seen this problem in Windows XP, Vista and 7. I have not tried in Windows 2003 or 2008 hence I don't know how it works there.

If anyone has seen this issue can you tell me how have you fixed it or work around for it?

Thank you,
Sathish
C#

Avatar of undefined
Last Comment
sathish_raos

8/22/2022 - Mon
erik_nodland

Hi

Are you using the built in .NET method? Have you tried using a pInvoke way?

[DllImport("kernel32.dll")]
static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId);
static void Main(string[] args)
{
Process _currentProcess = Process.GetCurrentProcess();
uint _processID = (uint)_currentProcess.Id;
uint _sessionID;
bool _result = ProcessIdToSessionId(_processID, out _sessionID);
Console.WriteLine("ProcessIdToSessionId Result: " + _result.ToString());
Console.WriteLine("Process ID = " + _processID.ToString());
Console.WriteLine("Session ID = " + _sessionID.ToString());
Console.ReadLine();
}

thanks

Erik
sathish_raos

ASKER
Thank you for responding Erik, My session detection happens in service and I have overridden OnSessionChange() in c# windows service to determine the session events and session id.
erik_nodland

Ahh ok I see. What account are you running your service under and also do you have an example of one of the numbers that you get returned in error for the session ID.

thanks

Erik
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sathish_raos

ASKER
I don't get an error in the session id but the session id returned as 6422639 or 28 or some 7 digit number.
erik_nodland

Hi

What account are you running the service under currently? You say the session ID returned is intermittent. Sometimes it is correct. Is this during normal operation or through debugging?

thanks

Erik
sathish_raos

ASKER
The service is running under "Local System" account. The session id returned during OnSessionChange is mostly correct. Sometimes it returns a wrong id. This happens in the production environment. I happened to see this in the log files.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
sathish_raos

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sathish_raos

ASKER
Answer was not provided