关于Delphi COM编程中OleVariant类型作为参数引用的问题(300分)

  • 主题发起人 主题发起人 winge
  • 开始时间 开始时间
W

winge

Unregistered / Unconfirmed
GUEST, unregistred user!
在用Delphi对Lotus Sametime进行COM编程中,代码如下:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,ComObj, OleServer, STComTkLib_TLB;

type
TForm1 = class(TForm)
Button1: TButton;
STCommunityService: TCommunityService;

STLookupService: TLookupService;
STResolver: TResolver;
STIm: TIm;
STIMService: TInstantMessagingService;
STUser: TSTUser;
procedure Button1Click(Sender: TObject);
procedure STCommunityServiceLoggedIn(Sender: TObject;
var ev: OleVariant);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure STResolverResolved(Sender: TObject; var ev: OleVariant);
procedure STResolverResolveFailed(Sender: TObject; var ev: OleVariant);
private

public
{ Public declarations }


end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
STCommunityService.LoginByPassword('SametimeServer','User','password');
end;

procedure TForm1.STCommunityServiceLoggedIn(Sender: TObject;
var ev: OleVariant);
begin
ShowMessage('Login OK');

STResolver.ConnectTo(STLookupService.CreateResolver(-1,-1));
STResolver.Resolve('user2');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;

procedure TForm1.STResolverResolved(Sender: TObject; var [red]ev: OleVariant[/red]);
var
iST : IResolveEvent;
evdd: OleVariant;
isu : ISTuser;
begin
ShowMessage('Resolved OK');

[red]STIMService.CreateIm(ev.GetResolvedUser,IST_ENC_LEVEL_NONE,1);[/red]

end;

procedure TForm1.STResolverResolveFailed(Sender: TObject;
var ev: OleVariant);
begin
ShowMessage('Resolved Failed');
end;

end.
在STResolver的Resolved事件中的参数ev为OleVariant类型变量,根据类型库声明:
procedure TResolver.InvokeEvent(DispID: TDispID; var Params: TVariantArray);
begin
case DispID of
-1: Exit; // DISPID_UNKNOWN
1: if Assigned(FOnResolved) then
FOnResolved(Self, Params[0] {const IResolveEvent});
2: if Assigned(FOnResolveConflict) then
FOnResolveConflict(Self, Params[0] {const IResolveEvent});
3: if Assigned(FOnResolveFailed) then
FOnResolveFailed(Self, Params[0] {const IResolveEvent});
end; {case DispID}
end;
可看出ev为IResolveEvent,其GetResolvedUser的函数作为CreateIm的参数输入,CreateIm类型库声明如下:
function TInstantMessagingService.CreateIm(const STUserObject: ISTUser; EncLevel: Integer;
ImType: Integer): IIm;

在编译过程中始终无法通过,其在VB中的实现如下:
Private Sub MyResolver_Resolved(ByVal ev As STComTkLib.IResolveEvent)
TInstantMessagingService.CreateIm ev.GetResolvedUser
UserName.SetFocus
End Sub

请教各位高手在Delphi中如何解决?谢谢!
如需涉及编程的软件环境,可于我联系。
 
你有没有试一下强制类型转换?
 
试过了,OleVariant类型无法进行类的强制转换
 
过程中也可以声明接口和类
 
only you:
如何写
 

到底是什么错误或原因通不过编译?
将错误信息提供看看!
 
如果直接使用ev:
STIMService.CreateIm(ev.GetResolvedUser,IST_ENC_LEVEL_NONE,1);
会产生:[Error] Unit1.pas(91): Incompatible types: 'Variant' and 'ISTUser'错误

如果进行类型转换:
STIMService.CreateIm(ISTUser(ev.GetResolvedUser),IST_ENC_LEVEL_NONE,1);
会产生:[Error] Unit1.pas(91): Invalid typecast错误
 
你的转换方式写错了。
应该类似于:
(ev as ISTUSer).GetResolvedUser
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
706
import
I
I
回复
0
查看
651
import
I
后退
顶部