我想将列表中的联系人显示在TListBox上。我在Windows 7 PC上安装并运行了Communicator,运行Delphi XE。我正在使用CommunicatorAPI_TLB和CommunicatorPrivate_TLB。
我单击登录和注销按钮,程序按预期工作:我的Communicator登录和注销。凉。
问题是当我尝试点击list-users按钮时。 Contacts.Count
方法似乎让我违反了访问权限。我用小组尝试了它,结果相同。谁能发现我做错了什么?
{ This IMessenger3 Class Inherits from the IMessenger2 interface -> IMessenger... }
Communicator : IMessenger3;
Contacts : IMessengerContacts;
Contact : IMessengerContact;
Groups : IMessengerGroups;
Connected : Boolean;
End;
Var
frmMain: TfrmMain;
Implementation
{$R *.dfm}
{ ---------------------------------------------------------------------------- }
Procedure TfrmMain.FormCreate(Sender: TObject);
Begin
Communicator := CoMessenger.Create;
End; { FormCreate Procedure }
Procedure TfrmMain.btnSignInClick(Sender: TObject);
Begin
Communicator.AutoSignin;
Connected := True;
End; { btnSignInClick Procedure }
Procedure TfrmMain.btnSignOutClick(Sender: TObject);
Begin
Communicator.Signout;
Connected := False;
End; { btnSignOutClick Procedure }
Procedure TfrmMain.btnLoadContactsClick(Sender: TObject);
Var
ContactIndex : Integer;
Begin
{ Load my contacts into a listbox }
Contacts := IMessengerContacts (Communicator.MyContacts);
Groups := IMessengerGroups (Communicator.MyGroups);
If (Contacts <> Nil) Then Begin
try
showmessage (inttostr(Groups.Count));
showmessage (inttostr(Contacts.count));
except
end;
(*
For ContactIndex := 0 To (Contacts.Count) Do Begin
Contact := IMessengerContact (Contacts.Item (ContactIndex));
{ Add the contact to the list }
lbxContacts.AddItem (Contact.FriendlyName, Nil);
End; { For }
*)
End; { If <> Nil }
End;
答案 0 :(得分:2)
将两个类型转换改为使用as
。如果问题是接口不可用,您至少会收到一条有意义的错误消息。
更改
Contacts := IMessengerContacts(Communicator.MyContacts);
Groups := IMessengerGroups (Communicator.MyGroups);
到
Contacts := Communicator.MyContacts as ImessengerContacts;
Groups := Communicator.MyGroups as IMessengerGroups;
您可能应该对要进行类型转换的其他地方做同样的事情来获取接口。如果有可能礼貌地要求它们而不是强行抓住它们总是更好。 :)
答案 1 :(得分:0)