Click here to Skip to main content
11,633,998 members (75,140 online)
Click here to Skip to main content

Controlling Skype with C#

, 29 Oct 2013 CPOL 162.6K 5.7K 108
Rate this:
Please Sign up or sign in to vote.
C# example code for controlling Skype
Sample Image - skypecontrolapicsharp.gif

Read this first - Discontinuation of Skype API

Microsoft plans to switch off the Skype desktop API in December 2013. The explanation is that:

"The Desktop API was created in 2004 and it doesn’t support mobile application development. We have, therefore, decided to retire the Desktop API in December 2013."

However, many developers are using Skype API in their products enhancing Skype functionality. And many Skype users rely on these products in their businesses and everyday life.

There is a petition initiative which requests Skype to reconsider this decision.

Introduction

I have put together an example code in C# for controlling the Skype VoIP application. The Skype Control API documentation is freely available. So why not give it a try with C#?

The downloadable ZIP package contains a Visual Studio 2005 solution with two projects - the Skype Control class library and the Test Application.

Background

The Control API for Windows uses window messages for communication between Skype and client applications. This involves usage of Windows API functions. Working with window messages is not directly supported by the .NET Framework classes. Fortunately there is a mechanism in .NET called platform invoke, which enables calls to unmanaged code in DLLs.

I will not explain the Control API in detail here, however I will provide enough information for everyone to start experimenting with the code and playing with Skype through the Test application.

Communication Initiation

Skype uses window messages to communicate with client applications. Registered window messages are used to initiate communication. The SkypeControlAPIDiscover message is used to find the Skype window handle by broadcasting it to all windows. A client application window handle must be specified when sending this message. Skype, if running, will respond with SkypeControlAPIAttach message sent directly to our client window.

internal class Constants
{
    public const string SkypeControlAPIDiscover = "SkypeControlAPIDiscover";
    public const string SkypeControlAPIAttach = "SkypeControlAPIAttach";
}

The LPARAM of the SkypeControlAPIAttach message contains the attach code. This can be Success in case of a successful connection. When the client application tries to connect for the first time, Skype pops up an authorization dialog and notifies the client by sending SkypeControlAPIAttach message with the PendingAuthorisation code. Depending on the user action, Skype will next send a Success or Refused notification.

public enum SkypeAttachStatus : uint
{
    Success = 0,
    PendingAuthorizaion = 1,
    Refused = 2,
    NotAvailable = 3,
    Available = 0x8001
}

When the Skype application is starting, it broadcasts SkypeControlAPIAttach message with the Available code.

Be aware that Skype assumes that our client application will process messages with result of 1 within one second, otherwise the connection times out and the communication is closed. This is important to know while debugging the client. Fortunately Skype can be set to a special "development mode" (by modifying the registry, see Skype API documentation).

Sending Commands

The WM_COPYDATA window message is used for sending Skype commands and receiving command replies and notifications.

Handling the WM_COPYDATA messages in C# is a little bit tricky. The CopyDataStruct needs to be correctly marshaled to and from the native code. The straightforward solution is to simply pass the CopyDataStruct this way:

[DllImport("user32.dll")]
public static extern IntPtr SendMessageTimeout(IntPtr windowHandle,
    uint Msg,
    IntPtr wParam,
    ref CopyDataStruct lParam,
    SendMessageTimeoutFlags flags,
    uint timeout,
    out IntPtr result
);

Note that when filling CopyDataStruct, the Length member also counts the terminating '0' character.

Code for getting the CopyDataStruct form is also straightforward, the Message class has a handy GetLParam method for this.

Platform.CopyDataStruct aCDS = 
    (Platform.CopyDataStruct)m.GetLParam(typeof(Platform.CopyDataStruct));

string aResponse = aCDS.Data;

The SkypeControl Library

All the code for handling window message level communication can be found in the SkypeClient class (SkypeControl library). Platform specific stuff is in the Platform.cs. All of the above is internal to the Skype control library.

The SkypeProxy class is designed to be used for controlling Skype by a client application. Currently, it is only wrapping calls to the SkypeClient class and contains only methods for low level Skype API communication. It is meant to be expanded with methods that encapsulate Skype API at a higher level, e.g. GetUserStatus, Ping.

To control the Skype, just create an instance of the SkypeProxy class. Communication is initiated by a call to Connect. A successful connection is indicated by firing of the SkypeAttach event with AttachStatus set to Success.

Commands are sent to Skype by calling Command with appropriately formatted text command. Results and notifications from Skype are received through the SkypeResponse event.

For more information on Skype Control API and command syntax, please refer to the Skype documentation which is freely available at Skype developer zones.

Enjoy!

History

  • 15th February, 2006: Initial post.

License

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

Share

About the Author

Gabriel Szabo
Software Developer (Senior)
Slovakia Slovakia
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionHelp!!! Pin
Member 105840735-Aug-14 6:48
memberMember 105840735-Aug-14 6:48 
Questionhow can I integrate kype as part of my application. Pin
thuandung26-Jun-14 17:34
memberthuandung26-Jun-14 17:34 
QuestionHow to record skype conversation Pin
Tridip Bhattacharjee24-Apr-14 0:58
memberTridip Bhattacharjee24-Apr-14 0:58 
SuggestionSkype call divert huwaei 3g modem Pin
sahufankariapper5-Apr-14 12:17
membersahufankariapper5-Apr-14 12:17 
Questionsend message Pin
vladko4141-Mar-14 2:43
membervladko4141-Mar-14 2:43 
QuestionWin 8 Metro Style Skype app Pin
matthias_7923-Dec-13 4:42
membermatthias_7923-Dec-13 4:42 
QuestionDesktop API retirement - a lie Pin
Cristian Amarie23-Nov-13 21:08
memberCristian Amarie23-Nov-13 21:08 
NewsFYI : Skype has announced the termination of API Pin
pipiscrew31-Oct-13 14:22
memberpipiscrew31-Oct-13 14:22 
Questionperfectoooo Pin
Gun Gun Febrianza29-Oct-13 6:23
member Gun Gun Febrianza29-Oct-13 6:23 
Questionupdates Pin
kiquenet.com11-Aug-13 20:50
memberkiquenet.com11-Aug-13 20:50 
GeneralMy vote of 5 Pin
Ed Nutting4-Sep-12 4:44
memberEd Nutting4-Sep-12 4:44 
QuestionThis is fantastic Pin
kevroy31426-Mar-12 11:03
memberkevroy31426-Mar-12 11:03 
GeneralMy vote of 5 Pin
Member 31518218-Jun-11 3:24
memberMember 31518218-Jun-11 3:24 
Generalhow to get APP2APP command responses Pin
Akeeq6-May-11 5:06
memberAkeeq6-May-11 5:06 
GeneralRe: how to get APP2APP command responses Pin
Akeeq13-May-11 19:40
memberAkeeq13-May-11 19:40 
actually i am getting that responce. the whole thing work well in 32bit machine. but in 64 bit i seems like getting "541 Error"
what are the changes require for this application to work well in 64 bit
Akeeq
GeneralRe: how to get APP2APP command responses Pin
Jason Newland28-Nov-13 11:54
memberJason Newland28-Nov-13 11:54 
GeneralThank You! Pin
Zviadi892-May-11 22:56
memberZviadi892-May-11 22:56 
GeneralMy vote of 5 Pin
Mastak.ua15-Nov-10 23:09
memberMastak.ua15-Nov-10 23:09 
GeneralThank for this example Pin
Gexrjd14-Mar-10 0:03
memberGexrjd14-Mar-10 0:03 
General[Message Deleted] Pin
it.ragester28-Mar-09 5:35
memberit.ragester28-Mar-09 5:35 
GeneralSkype login Pin
KhangBKIT9-Oct-08 10:06
memberKhangBKIT9-Oct-08 10:06 
GeneralNot UTF 8 Pin
Tor-Helge Persett29-Jul-08 21:26
memberTor-Helge Persett29-Jul-08 21:26 
Generalwow, super examplik diky moc Pin
Member 301469616-Jun-08 22:35
memberMember 301469616-Jun-08 22:35 
GeneralConnect Refused [modified] Pin
Rajan Hans22-Apr-08 17:06
memberRajan Hans22-Apr-08 17:06 
GeneralRe: Connect Refused Pin
Gabriel Szabo23-Apr-08 3:54
memberGabriel Szabo23-Apr-08 3:54 
GeneralRe: Connect Refused Pin
Rajan Hans23-Apr-08 4:54
memberRajan Hans23-Apr-08 4:54 
GeneralRe: Connect Refused Pin
xaml.net4-Aug-10 23:20
memberxaml.net4-Aug-10 23:20 
GeneralUnicode bug fix Pin
Vlad019-Sep-07 6:42
memberVlad019-Sep-07 6:42 
GeneralRe: Unicode bug fix Pin
Mikhail Diatchenko12-Oct-08 21:34
memberMikhail Diatchenko12-Oct-08 21:34 
QuestionWhy not just use Skype4COM.dll? Pin
Vlad013-Sep-07 4:26
memberVlad013-Sep-07 4:26 
GeneralSkype Wrapper for .NET Pin
Nicolae Mogoreanu21-Jul-07 10:51
memberNicolae Mogoreanu21-Jul-07 10:51 
GeneralVB.NET version Pin
hlbcal18-Feb-07 4:14
memberhlbcal18-Feb-07 4:14 
Generalneed help!!! Pin
gfhyang3-Jul-06 21:02
membergfhyang3-Jul-06 21:02 
GeneralRe: need help!!! Pin
10der9-Nov-13 11:33
member10der9-Nov-13 11:33 
QuestionSkype sound stream Pin
Ermi30-Mar-06 21:33
memberErmi30-Mar-06 21:33 
Generaljópofa :) Pin
jocojoco23-Feb-06 6:11
memberjocojoco23-Feb-06 6:11 
GeneralRe: jópofa :) Pin
Gabriel Szabo23-Feb-06 23:55
memberGabriel Szabo23-Feb-06 23:55 
Generalgreat Pin
MP3Observer16-Feb-06 22:49
memberMP3Observer16-Feb-06 22:49 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 29 Oct 2013
Article Copyright 2006 by Gabriel Szabo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid