[英]Remote Desktop Connection - C# Events
遇到了一個小問題。
我們有一個來自Netgear的面向互聯網的VPN,允許員工和教師使用他們家中的RDC訪問學校網絡。
他們使用他們的網絡瀏覽器登錄VPN,點擊我們的一個遠程服務器,然后他們進入RDC。
人們雖然有一個大問題,但是已經注銷了。 它似乎逃脫了他們的頭腦。 所有用戶正在執行的操作是單擊RDC客戶端上的“關閉”按鈕,該按鈕不會將其注銷。
我們正在構建一個程序來解決這個問題,想法是“掛鈎”到遠程桌面API,然后檢查會話是否斷開連接,如果是,我們注銷用戶。
該程序將作為服務或物理最小化EXE在后台運行。
我們正在用C#構建它。 那么有誰知道可以使用.NET 4調用的任何RDC事件? 這將使我們知道用戶何時關閉會話。
如果您需要更多信息,請告訴我。
干杯
得到它了。
呼叫
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
}
}
這比上面的其他答案更好。
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.