Visual studio 2008如何使用Skype API

    技术2025-01-31  20

     

    两种使用方式:一、向Skype进程发送消息(可以参考msgapitest),二、注册Skype4COM.dll,使用该Active控件。本文主要讲述第二种方式,同时在文末介绍在实际使用过程中遇到的一些问题,最后不得不换成第一种方式。

     

    首先创建SkypeAPIDemo项目,项目采用MFC Application模板,对话框类型,其它配置默认。

     

    资源视图拉开对话框资源选择IDD_SKYPEAPIDEMO_DIALOG。在新打开的Dialog Tab页,对话框区域右键选择Insert ActiveX Control,在弹出的Insert ActiveX Control对话框中选择Skype Class

     

    自动在资源文件中插入如下代码

    #define IDC_SKYPE1                      1000

     

    ……

     

    CONTROL         "",IDC_SKYPE1,"{830690FC-BF2F-47A6-AC2D-330BCB402664}",WS_TABSTOP,0,0,60,25

     

    ……

     

    /

    //

    // Dialog Info

    //

    IDD_SKYPEAPIDEMO_DIALOG DLGINIT

    BEGIN

        IDC_SKYPE1, 0x376, 16, 0

    0x0000, 0x0000, 0x0800, 0x0000, 0x094d, 0x0000, 0x043d, 0x0000,

        0

    END

     

    类视图选择项目SkypeAPIDemo,右键选择Add Class。在弹出的Add Class对话框中选择MFC Class From Activex Control Templates。在弹出的Add Class From ActiveX Control Wizard对话框中选择Skype Class<1.0>,在Interfaces 列表框中选择Iskype Interface添加到Generated classes列表框。项目就添加了CSkype类。

     

     

    右键Skype ActiveX控件选择Add Variable,在Variable Name中填写m_SkypeCtl,其它默认

     

     

    SkypeAPIDemoDlg.h文件中添加了如下代码

    CSkype m_SkypeCtl;

     

    类视图选择项目SkypeAPIDemo,右键选择Add Class。在弹出的Add Class对话框中选择MFC Class From TypeLib Templates。在弹出的Add Class From TypeLib Wizard对话框中选择Skype4COM 1.0  Type Library.<1.0>,在Interfaces 列表框中将需要的类添加到Generated classes列表框(eg. 选择ICall,项目会添加CCall头文件)

     

    接下来添加Skype事件,在Dialog Tab页中选择整个对话框,打开Properties Window,在Control Events Categorized中拉开IDC_SKYPE1,选择需要的事件,比如监听Skype事件 AttachmentStatus 在后面的Text控件中填写OnAttachmentStatus,按下回车。

    会添加如下代码

    SkypeAPIDemo.h

    DECLARE_EVENTSINK_MAP()

    void OnAttachmentStatus(long Status);

     

    SkypeAPIDemo.cpp

    BEGIN_EVENTSINK_MAP(CSkypeAPIDemoDlg, CDialog)

    ON_EVENT(CSkypeAPIDemoDlg, IDC_SKYPE1, 4, CSkypeAPIDemoDlg::OnAttachmentStatus, VTS_I4)

    END_EVENTSINK_MAP()

     

    void CSkypeAPIDemoDlg::OnAttachmentStatus(long Status)

    {

    // TODO: Add your message handler code here

    }

    可以在OnAttachmentStatus实现中添加如下代码打印Attachment状态

    switch(Status) { /* apiAttachUnknown = -1, apiAttachSuccess = 0, apiAttachPendingAuthorization = 1, apiAttachRefused = 2, apiAttachNotAvailable = 3, apiAttachAvailable = 4 */ case 0: OutputDebugString("!!! Connected;"); break; case 1: OutputDebugString("!!! Pending authorization" ); break; case 2: OutputDebugString("!!! Connection refused" ); break; case 3: OutputDebugString("!!! Skype API not available" ); break; case 4: OutputDebugString("!!! Try connect now (API available);" ); m_SkypeCtl.Attach(7, false); break; default: OutputDebugString("SkypeTapiServer与Skype客户端处于其它状态"); break; } 

     

    以下以如何获得Skype好友为例,说明如何使用Skype API。关于接口说明请参考Skype4COM.chm文件。

    无论如何使用Skype,最上层的接口类就是ISKype(Skype is the default interface for the class)。在Skype接口说明中查找获取Skype好友的接口。

     

    IUserCollection Friends [get] This command queries the users in a contact list.

     

    从接口说明中可以得到以下信息:

    接口名称:get_Friends(ISkype接口members分两类:一、Public member functions,大致是动作类的那种,包括呼叫、发消息之类的;二、Properties,属性类接口,分get和set两种,主要用于获得类属性或是设置类属性的,而get_Friends属于Properties->get分类)

    返回:IUserCollection对象

     

    再查看IUserCollection接口说明,我们发现以下对于获得好友列表有用的接口

    LONG Count [get] This command queries the number of users in a UserCollection. 

     

    IUser Item ([in] LONG Index) [get] This command queries an item in a UserCollection with a specified index value. 

    注意Item下标是从1开始的,如果参数为0的话会报错。

     

    再查看IUser接口说明,我们发现以下对于获得好友列表有用的接口 

    BSTR Handle [get, set] This command queries/sets the Skypename of the user. 

     

    接下来就可以实现获得Skype好友功能了。

    首先通过MFC class From TypeLib添加IUserCollection与IUser接口(注意:如果在Skype.tlb位置产生com类编译错误,请注释新添加的接口类文件中的#import Skype4COM.dll语句)。

     

    CUserCollection friends = m_SkypeCtl.get_Friends(); TRACE("!!! Get Friends %d", friends.get_Count()); for(int i=0;i<friends.get_Count();i++) { CUser aUser = friends.get_Item(i+1); TRACE("%d %s", i, aUser.get_Handle()); }  

    注意:

    一、监控进程必须得与Skype进程处于同等权限之下(要么都是标准用户,要么都是管理员权限)。

    二、如果在注意一前提下,你的监听程序无法监听skype(skype没有弹出是否同意attach提示)的话该怎么做?

    It is starting to sound like your application/program is not approved to be used by the current Skype user. 

    Anytime your program changes, it will be required to be re-authorized. 

    You can check the status of your program authorization using the Skype client: 

    Tools -> Options -> Advanced -> Manage Other Programs Access To Skype 

    Is a record there for your program? 

     <http://forum.skype.com/index.php?showtopic=291901>

     

    在实际使用过程中遇到两个问题:一、对话框页选择Skype ActiveX控件,转到属性页时,整个Visual Studio 2008失去响应,无法恢复。在Visual studio 2010上试了试还是同样问题。二、无法对多个Skype实例同时监控。

     

    最新回复(0)