我想将列表中的联系人显示到TListBox上。我已在Windows 7 PC上安装并运行Communicator,并运行Delphi XE。我正在使用CommunicatorAPI_TLB和CommunicatorPrivate_TLB。
我单击登录和注销按钮,程序将按预期运行:Communicator登录和注销。凉。
问题是当我尝试单击列表用户按钮时。 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;
最佳答案
将两个类型转换更改为使用as
。如果问题是该接口不可用,则至少会收到一条有意义的错误消息。
更改
Contacts := IMessengerContacts(Communicator.MyContacts);
Groups := IMessengerGroups (Communicator.MyGroups);
至
Contacts := Communicator.MyContacts as ImessengerContacts;
Groups := Communicator.MyGroups as IMessengerGroups;
您可能应该在要进行类型转换的其他地方做同样的事情以获取接口。礼貌地要求他们比强行抓住他们总是更好。 :)