Link to home
Start Free TrialLog in
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
Avatar of erik_nodland
erik_nodland
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of sathish_raos
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.
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
I don't get an error in the session id but the session id returned as 6422639 or 28 or some 7 digit number.
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
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.
ASKER CERTIFIED SOLUTION
Avatar of sathish_raos
sathish_raos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Answer was not provided