遇到一个小问题。
我们有一个来自Netgear的面向互联网的VPN,允许员工和教师使用他们家中的RDC访问学校网络。
他们使用他们的网络浏览器登录VPN,点击我们的远程服务器之一,他们就是RDC。
人们虽然有一个大问题,但已经注销了。它似乎逃脱了他们的头脑。所有用户正在执行的操作是单击RDC客户端上的“关闭”按钮,该按钮不会将其注销。
我们正在构建一个程序来解决这个问题,想法是“挂钩”到远程桌面API,然后检查会话是否断开连接,如果是,我们会注销用户。
该程序将作为服务或物理最小化EXE在后台运行。
我们正在用C#构建它。那么有谁知道可以使用.NET 4调用的任何RDC事件?这将使我们知道用户何时关闭会话。
如果您需要更多信息,请与我们联系。
干杯
答案 0 :(得分:4)
知道了。
致电
SystemEvents.SessionSwitch += new SessionSwitchEventHandle SystemEvents_SessionSwitch);
然后
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.RemoteDisconnect || e.Reason == SessionSwitchReason.ConsoleDisconnect)
{
// Log off the user...
}
else
{
// Physical Logon
}
}
答案 1 :(得分:1)
这比上面的其他答案更好。
public Form1()
{
InitializeComponent();
if (!WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION))
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);
[DllImport("WtsApi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags);
[DllImport("WtsApi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);
// constants that can be passed for the dwFlags parameter
const int NOTIFY_FOR_THIS_SESSION = 0;
const int NOTIFY_FOR_ALL_SESSIONS = 1;
// message id to look for when processing the message (see sample code)
const int WM_WTSSESSION_CHANGE = 0x2b1;
// WParam values that can be received:
const int WTS_CONSOLE_CONNECT = 0x1; // A session was connected to the console terminal.
const int WTS_CONSOLE_DISCONNECT = 0x2; // A session was disconnected from the console terminal.
const int WTS_REMOTE_CONNECT = 0x3; // A session was connected to the remote terminal.
const int WTS_REMOTE_DISCONNECT = 0x4; // A session was disconnected from the remote terminal.
const int WTS_SESSION_LOGON = 0x5; // A user has logged on to the session.
const int WTS_SESSION_LOGOFF = 0x6; // A user has logged off the session.
const int WTS_SESSION_LOCK = 0x7; // A session has been locked.
const int WTS_SESSION_UNLOCK = 0x8; // A session has been unlocked.
const int WTS_SESSION_REMOTE_CONTROL = 0x9; // A session has changed its remote controlled status.
protected override void OnHandleDestroyed(EventArgs e)
{
// unregister the handle before it gets destroyed
WTSUnRegisterSessionNotification(this.Handle);
base.OnHandleDestroyed(e);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_WTSSESSION_CHANGE)
{
int value = m.WParam.ToInt32();
if (value == WTS_REMOTE_DISCONNECT)
{
ExitWindowsEx(4, 0); // Logout the user on disconnect
}
else if (value == WTS_REMOTE_CONNECT)
{
MessageBox.Show("Welcome to the VPN. There is no need to Logout anymore, as when you close this session it will automatically log you out");
}
}
base.WndProc(ref m);
}