Delphi 中的接口编组
我想将 IVApplication 的接口引用从 Visio 加载项发送到我的另一台 COM 服务器。但我有奥莱例外。现在我这样做:
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);
if Succeeded(hres) then
Result := rhglobal;
IStrm := nil;
end;
end;
此后,我创建 COM 服务器的实例并将 rhglobal 传递给他。
我的COM服务器的代码:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不在 COM 服务器中定义一个方法并创建一个 VARIANT 参数呢? (或 IDispatch* 或 IUknown*)。
然后,您可以将 VisioApplication 传递到 COM 服务器,并在服务器端将其转换回 Visio_TLB.IVApplication 接口。
所以它看起来像这样:
Addin:
Comserver:
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:
Comserver: