Introduction
This tip describes an implementation of a little app that changes the Skype status to "Away" whenever the user locks Windows and brings it back to the previous status when the user unlocks the system.
Background
I use Skype very intensively in my work. And when I leave my workplace I usually lock PC but Skype status still be "Online". So co-workers still writing me messages because they assume I online. Also we have corporate IM tool that changes users status regarding to Windows user activity. I decided to reach similar functionality in Skype.
Implementation
Skype4COM Library
Skype4COM is an ActiveX component that represents the Skype API as objects, with properties, commands, events and notifications. It is easy to use with C# due to .NET Interoperability. You can download COM library from http://dev.skype.com/accessories/skype4com. The library also could be found in the C:\Program Files\Common Files\Skype directory after Skype installation. If your OS is x64 look in Program Files (x86) instead of Program Files.
- Add reference on Skype4COM.dll to project
- Change Platform Target of project to x86
- If Skype4COM.dll was not registered as COM server (you'll get an exception about this in your app) register it using the command line:
regsvr32 <path> (replace <path> by location of Skype4COM.dll)
SkypeHelper
Let's consider part of the SkypeHelper
class responsible for status changing:
using SKYPE4COMLib;
namespace SkypeStatusChangerLib.SkypeApi
{
public class SkypeHelper
{
public const int SkypeProtocol = 7;
private readonly Skype _skype;
public SkypeHelper()
{
_skype = new Skype();
_skype.Attach(SkypeProtocol, false);
}
public bool SetUserStatus(UserStatus status)
{
try
{
_skype.ChangeUserStatus((TUserStatus) status);
}
catch (Exception)
{
return false;
}
return true;
}
So, to change Skype status (e.g., to DoNotDisturb) we need to write just three statements:
var skype = new Skype();
skype.Attach(7, false);
skype.ChangeUserStatus(TUserStatus.cusDoNotDisturb);
Windows Lock and Unlock Events
We need to now when user locks and unlock Windows to change Skype status. Therefore we write event handler to Microsoft.Win32.SystemEvents.SessionSwitch
event:
private const string StatusChanged = "Status changed to: ";
private static SkypeHelper skyper = new SkypeHelper();
private static UserStatus lastUserStatus = UserStatus.Unknown;
SystemEvents.SessionSwitch += systemEventsSessionSwitch;
private static void systemEventsSessionSwitch(object sender, SessionSwitchEventArgs e)
{
switch (e.Reason)
{
case SessionSwitchReason.SessionLock:
lastUserStatus = skyper.GetUserStatus();
skyper.SetUserStatus(UserStatus.Away);
Console.WriteLine(StatusChanged + UserStatus.Away);
break;
default:
skyper.SetUserStatus(lastUserStatus);
Console.WriteLine(StatusChanged + lastUserStatus);
break;
}
}
Here we save user status before locking, change it to Away and restore status after unlocking.
Access Skype
First time app connects to Skype you need to allow connection from Skype:
P.S. All manipulations of user status must be done when Skype is running.
Source Code
- SkypeStatusChanger.zip is the simple prototype app that covers current article. It contains two projects: library (with some additional methods not described here) and console application.
- SkypeStatusChanger_Extended.zip is more real-world app with UI and other features. However, basic functionality is based on this article. You can dig deeper by yourself.
Points of Interests
- Simple Skype API usage
- Windows system events handling
Bad news: Skype to retire Desktop API
According to Skype official news Desktop API (Skype4COM is wrapper around it) is no longer supported and will be discontinued in December 2013.
Alternatively Skype tries to provide cross-platform API (desktop, web, mobile) and introduces Skype URIs - a new generation of API. But for now Skype URIs have very poor functionality compared to old Desktop API:
- switching focus to the Skype client.
- initiating audio calls to other Skype users, phones, or mobiles-both one-to-one dialogs and multi-party conferences.
- initiating video calls to another Skype user.
- sending instant messages to an individual or establishing a group multi-chat.
I hope that Skype team working on extending Skype URIs and will deliver it soon (maybe). So me and other developers that used Desktop API will be able to migrate their apps to URIs.
Updates
- Added source files of extended SkypeStatusChanger application - June 21, 2013
- Ending of Skype Desktop API support.
References
Happy developing!