Skip to content

Instantly share code, notes, and snippets.

@krdlab
Created November 21, 2012 15:56
Show Gist options
  • Save krdlab/4125621 to your computer and use it in GitHub Desktop.
Save krdlab/4125621 to your computer and use it in GitHub Desktop.
Skype4COM and WM_WTSESSION_CHANGE
using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KrdLab.Skype
{
// 画面ロックを検知して,Skype の UserStatus を Away/Online と切り換えるだけ.
public partial class SkypeControlForm : Form
{
[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags);
[DllImport("wtsapi32.dll", SetLastError = true)]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);
private const int WM_WTSSESSION_CHANGE = 0x02B1;
private const int WTS_SESSION_LOCK = 0x7;
private const int WTS_SESSION_UNLOCK = 0x8;
private const int NOTIFY_FOR_THIS_SESSION = 0;
private event Action<Message> WTSessionChange;
private readonly SKYPE4COMLib.Skype skype;
public SkypeControlForm()
{
InitializeComponent();
this.HandleCreated += (s, e) =>
WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION);
this.HandleDestroyed += (s, e) =>
WTSUnRegisterSessionNotification(this.Handle);
this.Load += OnLoad;
this.WTSessionChange += (m) =>
{
switch ((int)m.WParam)
{
case WTS_SESSION_LOCK:
OnWTSessionLocked();
break;
case WTS_SESSION_UNLOCK:
OnWTSessionUnlocked();
break;
}
};
this.skype = new SKYPE4COMLib.Skype();
}
#region Message Handler
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_WTSSESSION_CHANGE)
{
this.WTSessionChange(m);
}
base.WndProc(ref m);
}
void OnLoad(object sender, EventArgs e)
{
AttachSkypeClient();
LogInfo("loaded");
}
void OnWTSessionLocked()
{
skype.ChangeUserStatus(SKYPE4COMLib.TUserStatus.cusAway);
LogInfo("change to Away");
}
void OnWTSessionUnlocked()
{
skype.ChangeUserStatus(SKYPE4COMLib.TUserStatus.cusOnline);
LogInfo("change to Online");
}
#endregion
private void AttachSkypeClient()
{
if (!skype.Client.IsRunning)
{
skype.Client.Start(true, true);
}
skype.Attach(8, true);
}
private void LogInfo(string message)
{
msgBox.Items.Add(NewLoggingItem(message));
}
private string NewLoggingItem(string message)
{
return DateTime.Now + ": " + message;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment