Interface Marshalling in Delphi

こ雲淡風輕ζ 提交于 2019-12-12 03:45:13

问题


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;

回答1:


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;


来源:https://stackoverflow.com/questions/2455183/interface-marshalling-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!