接口编组在Delphi [英] Interface Marshalling in Delphi

查看:253
本文介绍了接口编组在Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将IVApplication的Interface Ref从Visio加载项发送到我的另一个COM服务器。但我有Ole异常。现在我这样做:



Visio加载项中的代码:

  var 
IStrm:IStream;
hres:HResult;
rhglobal:HGLOBAL;
VisioAppl:IVApplication;
begin

hres:= CreateStreamOnHGlobal(0,TRUE,IStrm);
if Succeeded(hres)then
hres:= CoMarshalInterface(IStrm,IID_IVApplication,VisioAppl,
MSHCTX_LOCAL,0,
MSHLFLAGS_NORMAL);
if(Succeeded(hres))then
begin
hres:= GetHGlobalFromStream(IStrm,rhglobal);
如果成功(hres)then
结果:= rhglobal;
IStrm:= nil;
end;
end;

之后,我创建了我的COM服务器的实例,并传递rhglobal给他。



我的COM服务器的代码:

 程序(AHGlobal:HGlobal); 
var
VisioAppl:Visio_TLB.IVApplication;
iStrm:IStream;
hres:HResult;
begin

iStrm:= Nil;
VisioAppl:= nil;
hres:= CreateStreamOnHGlobal(AHGlobal,FALSE,iStrm);
if(SUCCEEDED(hres))then
begin

hres:= CoUnmarshalInterface(iStrm,Visio_TLB.IVApplication,VisioAppl);
iStrm:= nil;
ShowMessage('Result:'+ BoolToStr(SUCCEEDED(hres))); < - result 0
ShowMessage(VisioAppl.ProductName); < ----错误
end;

end;


解决方案

COM服务器并创建一个VARIANT参数? (或IDispatch *或IUknown *)。



然后,您可以将VisioApplication传递到COM服务器,并在服务器端将其转换回Visio_TLB.IVApplication接口。 / p>

因此,它将如下所示:



Addin:

 过程SendAppToComServer(aIntf:Visio_TLB.IVApplication); 
begin
MyComServer.PassVisioApp(aIntf);
end;

Comserver:

 code> procedure TMyComServer.PassVisioApp(VisioApp:OleVariant); 
var
VisioAppIntf:Visio_TLB.IVApplication;
begin
VisioAppIntf:= VisioApp;
ShowMessage(VisioAppIntf.ProductName);
end;


I want to send Interface Ref of IVApplication from Visio Add-in to my other one COM server. But I have Ole exception. Now i do that:

Code in Visio Add-in:

var 
  IStrm: IStream;
  hres: HResult;
  rhglobal: HGLOBAL;
  VisioAppl: IVApplication; 
begin

   hres := CreateStreamOnHGlobal(0, TRUE, IStrm);
      if Succeeded(hres) then
        hres := CoMarshalInterface(IStrm, IID_IVApplication, VisioAppl,
                            MSHCTX_LOCAL, 0,
                            MSHLFLAGS_NORMAL);
      if (Succeeded(hres)) then
      begin
          hres := GetHGlobalFromStream(IStrm, rhglobal);
          if Succeeded(hres)  then
             Result := rhglobal;
          IStrm := nil;
      end;
 end;

After this I create instance of my COM server and pass rhglobal to him.

Code of my COM server:

procedure (AHGlobal: HGlobal);
var
  VisioAppl: Visio_TLB.IVApplication;
  iStrm: IStream;
  hres: HResult;
begin

      iStrm := Nil;
      VisioAppl:= nil;
      hres := CreateStreamOnHGlobal(AHGlobal, FALSE, iStrm);
      if (SUCCEEDED(hres)) then
      begin

        hres := CoUnmarshalInterface(iStrm, Visio_TLB.IVApplication, VisioAppl);
        iStrm := nil;
        ShowMessage('Result:' + BoolToStr(SUCCEEDED(hres)));  <-- result 0 
        ShowMessage(VisioAppl.ProductName); <----  Error
      end;

end;

解决方案

Why don't you just define a method in your COM server and make a VARIANT parameter? (or IDispatch* or IUknown*).

Then you can just pass the VisioApplication to your COM server and at the serverside cast it back to the Visio_TLB.IVApplication interface.

So it will look like this:

Addin:

procedure SendAppToComServer(aIntf: Visio_TLB.IVApplication);
begin
  MyComServer.PassVisioApp(aIntf);
end;

Comserver:

procedure TMyComServer.PassVisioApp(VisioApp: OleVariant);
var
  VisioAppIntf: Visio_TLB.IVApplication;
begin
  VisioAppIntf := VisioApp;
  ShowMessage(VisioAppIntf.ProductName);
end;

这篇关于接口编组在Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆