检测用户存在

时间:2009-06-18 18:29:26

标签: vb.net winapi

我想要做的是检测用户是否正在使用运行Windows(2000或更高版本)的计算机。我最好不要使用屏幕保护程序。

背景:我们有一个客户服务部门,它位于一个寻线组中,可以“可用”。如果它们是“可用”,则呼叫将被路由到他们的电话而不是坐在队列中。当电话在分机上振铃时,呼入呼叫者会听到“振铃”声而不是保持音乐。不幸的是,我们也有代表在他们去吃午餐或离开当天时忘记将自己从“可用”中取出来。

最终结果可能是使用.NET

编写的

有什么想法吗?

4 个答案:

答案 0 :(得分:3)

应该使用API​​ GetLastInputInfo()。

答案 1 :(得分:2)

如果用户锁定其工作站,或者组策略在一定量的空闲时间后强制屏幕锁定,则可以使用Windows API使用WTSRegisterSessionNotification和{{3}订阅“会话通知”事件}。一旦您要求Windows让您的应用程序知道会话已锁定,您可以使用它作为用户不在场的指示。

好读:WTSUnRegisterSessionNotification

示例:

private const int NOTIFY_FOR_THIS_SESSION = 0x0;
private const int WM_WTSSESSION_CHANGE = 0x2B1;
private const int WTS_CONSOLE_CONNECT = 0x1;
private const int WTS_CONSOLE_DISCONNECT = 0x2;
private const int WTS_SESSION_LOCK = 0x7;
private const int WTS_SESSION_UNLOCK = 0x8;
private const int WM_DESTROY = 0x2;
private const int WM_ACTIVATEAPP = 0x1C;

// The WTSUnRegisterSessionNotification function unregisters the specified 
// window so that the specified window receives no more session-change notifications.
[DllImport("Wtsapi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);

// The WTSRegisterSessionNotification function registers the specified 
// window to receive session-change notifications.
[DllImport("Wtsapi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, Int32 dwFlags);

// The PostQuitMessage function indicates to the system that a thread 
// has made a request to quit. The PostQuitMessage function is typically used in 
// response to a WM_DESTROY message.
[DllImport("user32.dll")]
private static extern void PostQuitMessage(Int32 nExitCode);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(HandleRef hwnd, int msg, int wparam, int lparam);

private void Unsubscribe() {
   if (this.Handle != IntPtr.Zero) {
      WTSUnRegisterSessionNotification(this.Handle);
   }
}

private void Subscribe() {
   if (this.Handle != IntPtr.Zero) {
      WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION);
   }
}

// override WndProc in order to catch process the Session-Notification events:
protected override void WndProc(ref Message m) {
   switch (m.Msg) {
      case WM_WTSSESSION_CHANGE:
         switch (m.WParam.ToInt32()) {
            case WTS_CONSOLE_CONNECT:
               // User-switch : Sign-on
               break;
            case WTS_CONSOLE_DISCONNECT:
               // User-switch : Sign-off
               break;
            case WTS_SESSION_LOCK:
               // Screen Lock
               break;
            case WTS_SESSION_UNLOCK:
               // Screen Unlock
               break;
            default:
               break;
         }
         break;
      default:
         break;
   }

   base.WndProc(ref m);
}

答案 2 :(得分:0)

您可以使用Windows API添加低级键盘/鼠标挂钩,然后在暂时没有活动时标记为离开,然后在活动再次开始时标记为可用。

http://www.codeproject.com/KB/cs/globalhook.aspx有相关内容,但它在C#中。

答案 3 :(得分:0)

查看SetWindowsHookEx和WH_FOREGROUNDIDLE类。