Hook windows logon/logoff events

2019-04-17 10:25发布

I am having a service which would be running at SYSTEM level. Now, i want to track the logged on user in it. Earlier i was trying to get the logged in user name from GetUserName api but in my case it returns "SYSTEM" every time.

Is there anyway to get logged on username in my case? or is there any hook that i can install so that i may get which user logged on?

P.S: I am mainly working in Delphi 2007 but these question are specific to the Win32 API.

3条回答
贼婆χ
2楼-- · 2019-04-17 11:03

You need to be more specific in your requirements. There may be multiple users logged on at any given time, either locally or remotely. Do you only care about interactive users?

The most common request is how to get the locally logged-on interactive user, i.e. the person who is physically sat at the keyboard and screen. There are various issues that you must consider before you decide how to proceed. MSDN has a good entry on Window Stations that will explain the situation better than I can.

查看更多
【Aperson】
3楼-- · 2019-04-17 11:11

I am unfamiliar with Delphi's implementation of Windows services but wherever you set the controls accepted by the service you should add SERVICE_ACCEPT_SESSIONCHANGE. Then in your HandlerEx callback function the dwEventType parameter will be one of the WM_WTSSESSION_CHANGE values and the lpEventData will be a pointer to a WTSSESSION_NOTIFICATION structure that contains the session ID of the event.

You can use this info along with the terminal services API to determine who did what.

查看更多
太酷不给撩
4楼-- · 2019-04-17 11:21

You can use SENS to listen to subscribe to log-in notifications.

查看更多
登录 后发表回答