It seems that NOTIFY_FOR_THIS_SESSION flag of this function doesnot do what it is meant to under certain conditions. This problem was seen in Windows XP SP2 but has not been tested in any other Windows OS versions.
Scenario 1 (Where it works as it is supposed to):
Try calling WTSRegisterSessionNotification() with NOTIFY_FOR_THIS_SESSION flag set in your program. Then goto 'Start->Log Off' then click on 'Switch User'. Now select a new user. Then keeping this new account open (which will be required to be so for scenario 2) switch back to your original user account. You will have received WTS_SESSION_LOCK and WTS_SESSION_UNLOCK as expected; both events related to the session from where your code is running.
Scenario 2 (Where it fails to work as it is supposed to):
Now restart your program and again goto 'Start->Log Off' then 'Switch User'. This time unlock the new account you had locked earlier. Again switch to your original account to see what messages your window has got. This time too WTS_SESSION_LOCK and WTS_SESSION_UNLOCK will have been caught. But there is a problem. WTS_SESSION_LOCK is fired correctly for the original session but WTS_SESSION_UNLOCK will have been fired while unlocking the new account and no event will have fired when the user is unlocking the session where your program is running (whom you asked to be notified for its lock/unlock event).
This problem is easily confirmed if you call WTSGetActiveConsoleSessionId() when lock and unlock events are caught in your code. You will see that you get different session ids in Scenario 2 instead of being same.
Solution:
To be notified correctly of session change events concerning only your session, save session id of your process by calling GetCurrentProcessId() and ProcessIdToSessionId() function in a variable. Also, call WTSRegisterSessionNotification() with NOTIFY_FOR_ALL_SESSIONS (Even a process running under guest account can receive notifications of global session change under Windows XP SP2; not tested in any otherOS vers). When you receive WM_WTSSESSION_CHANGE message in your windows procedure, see if WTSGetActiveConsoleSessionId() and session id of your process (which you saved earlier in a variable) are equal or not. If they are equal then this event is concerned with your session.
Happy Coding,
Sanje2v