Click here to Skip to main content
15,878,871 members
Articles / Desktop Programming / Win32

Skype Status Changer

Rate me:
Please Sign up or sign in to vote.
4.86/5 (12 votes)
10 Oct 2013CPOL3 min read 51.2K   1.9K   22   4
Change Skype status when user locks Windows.

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:

C#
using SKYPE4COMLib;
 
namespace SkypeStatusChangerLib.SkypeApi
{
    public class SkypeHelper
    {
        // other constants
        // ....
        public const int SkypeProtocol = 7;
 
        private readonly Skype _skype;
 
        public SkypeHelper()
        {
            _skype = new Skype();
 
            // attach using Skype Protocol 7 API
            _skype.Attach(SkypeProtocol, false);
        }
 
        public bool SetUserStatus(UserStatus status)
        {
            try
            {
                // set Skype user status
                _skype.ChangeUserStatus((TUserStatus) status);
            }
            catch (Exception)
            {
                return false;
            }
 
            return true;
        }  
// ... other code

So, to change Skype status (e.g., to DoNotDisturb) we need to write just three statements: 

C#
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<code><code> event:

C#
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:

Image 1

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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionQuestion from an End User Pin
Justin Brehms29-Oct-13 8:08
Justin Brehms29-Oct-13 8:08 
AnswerRe: Question from an End User Pin
savbace29-Oct-13 22:21
savbace29-Oct-13 22:21 
Question5 Pin
Kevin Marois21-Jun-13 5:34
professionalKevin Marois21-Jun-13 5:34 
AnswerRe: 5 Pin
savbace24-Jun-13 2:13
savbace24-Jun-13 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.