Visual Basic中调用MSN API函数
作者:东坡下载 来源:uzzf 发布时间:2010-10-14 13:43:01 点击:
了解这方面内容的起因是早上有位朋友让我看一段关于MSN开发程序,现在无法运行,我打开工程发现是针对4.7编写的,而他的计算机安装的6.2,因为我也用6.2所以我在VB中查看了一下,发现是缺少Messenger Type Library。但是多了Messenger API Type Library,可能是作为Messenger Type Library的替代,但它的功能不如以前的Messenger Type Library 全面,起码我现在就发现它不能修改本地的MyFriendlyName,不明白为什么把它弄成只读的,MyStatus是可写。
下面简单用VB具体说明怎么调用它:
首先当然是引用对象Messenger API Type Library了。
然后声明一个对象MessengerAPI.Messenger。
Private WithEvents oMSN As MessengerAPI.Messenger |
我们需要的东西都在这里边,简单介绍几个:
MyContacts:IMessengerContacts 对象
MyFriendlyName: 当前的昵称
MyGroups: IMessengerGroupss对象
MyPhoneNumber:返回电话号码
MyProperty:现在没有使用
MyServiceId :返回Service id应该是{9b017612-c9f1-11d2-8d9f-0000f875c541}]
MyServiceName:返回Service name应该是 .NET Messenger Service
MySigninName:返回当前的签名,eMail地址
MyStatus:返回当前状态,MISTATUS枚举值
其实本地属性和通过PrimaryService得到的一样
做几个简单的演示:
首先是登陆演示,使用MessengerAPI.Messenger的 SignIn 或者AutoSignin方法:
Public Sub SignIn(ByVal bAuto As Boolean, Optional ByVal sSignInName As String, Optional ByVal sPassword As String) On Error GoTo ErrorHandle If bAuto = True Then oMSN.AutoSignin Else ManualSignIn: oMSN.SignIn 0, sSignInName, sPassword End If Exit Sub ErrorHandle: Dim MConstants As MSGRConstants MConstants = err.Number If MConstants = MSGR_E_FAIL Then GoTo ManualSignIn ElseIf MConstants = MSGR_E_ALREADY_LOGGED_ON Then ’已经登陆 End If End Sub |
获取全部用户列表:
通过Messenger的MyContacts对象(它是只读的)
它其实就是一个IMessengerContacts对象,而IMessengerContacts类又是IMessengerContact类的一个集合,在IMessengerContact中保存联系人信息。
因此可以这样遍历联系人。
Dim oContacts As IMessengerContacts Dim oContact As IMessengerContact Dim iCount As Integer Dim i As Long Set oContacts = oMSN.MyContacts iCount = oContacts.Count For i = 0 To iCount - 1 Set oContact = MsgrContacts.Item List1.AddItem MsgrContact.FriendlyName & vbTab & "(" & getStatusDesc(MsgrContact.Status) & ")" Next |
getStatusDesc函数用来返回状态文本描述:
Private Function getStatusDesc(eStatus As MISTATUS) As String Select Case eStatus Case MISTATUS_AWAY getStatusDesc = "走开了" Case MISTATUS_BE_RIGHT_BACK getStatusDesc = "马上回来" Case MISTATUS_BUSY getStatusDesc = "在忙碌" Case MISTATUS_IDLE getStatusDesc = "在发呆" Case MISTATUS_INVISIBLE getStatusDesc = "隐身" Case MISTATUS_LOCAL_CONNECTING_TO_SERVER getStatusDesc = "正在链接服务器" Case MISTATUS_LOCAL_DISCONNECTING_FROM_SERVER getStatusDesc = "从服务器断开" Case MISTATUS_LOCAL_FINDING_SERVER getStatusDesc = "正在搜索服务器" Case MISTATUS_LOCAL_SYNCHRONIZING_WITH_SERVER getStatusDesc = "正在和服务器同步" Case MISTATUS_OFFLINE getStatusDesc = "离线" Case MISTATUS_ON_THE_PHONE getStatusDesc = "在接电话" Case MISTATUS_ONLINE getStatusDesc = "在线" Case MISTATUS_OUT_TO_LUNCH getStatusDesc = "外出就餐" Case MISTATUS_UNKNOWN getStatusDesc = "未知" End Select End Function |
获取组列表:
方法同获取联系人,只是换了一下对象.
Dim oGroups As IMessengerGroups Dim oGroup As IMessengerGroup Set oGroups = oMSN.MyGroups For Each oGroup In oGroups List1.AddItem oGroup.Name Next |
下面就是将组和用户关联起来:
明白上面的方法,这个很容易实现,IMessengerGroup类的对象中含有IMessengerContact类对象的集合。
Dim oGroups As IMessengerGroups Dim oGroup As IMessengerGroup Dim oContacts As IMessengerContacts Dim oContact As IMessengerContact Set oGroups = oMSN.MyGroups For Each oGroup In oGroups Set oContacts = oGroup.Contacts For Each oContact In oContacts List1.AddItem oGroup.Name & " " & oContact.SigninName Next Next |
再介绍一下服务:
也是用样道理:
Dim oServices As IMessengerServices Dim oService As IMessengerService Set oServices = oMSN.Services Set oService = oServices.PrimaryService MsgBox oService.MyFriendlyName & getStatusDesc(oService.MyStatus) & oService.MySigninName & oService.ServiceId & oService.ServiceName |
我现在怎么觉得PrimaryService的意义不大,IMessengerServices中就一个PrimaryService可用,还是只读的。
当然各个对象可能还有一些相应的方法就不多介绍,我介绍的这些通过对象浏览器就可以看明白,其中还有对象或成员我现在还没学会使用,现在只是大致了解一下,我希望看到这篇文章的人也有了一点初步了解。
[收藏此文章]