如何使用Marshal.QueryInterface? [英] How do I use Marshal.QueryInterface?

查看:235
本文介绍了如何使用Marshal.QueryInterface?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Word文档中某些嵌入对象的工作。较早的海报告诉我,这是不是直线前进。这里是链接答案的摘录:




正如我前面提到的,利用
嵌入对象的编程模型
执行节省是
快捷的一些东西。有一个更复杂的
溶液,将与任何
嵌入对象工作。为了使嵌入在
对象第一
的地方,它必须支持的COM
的IPersist接口之一(即
IPersistStorage,的IPersistStreamInit,
IPersistFile等)。因此,
嵌入对象可以总是通过在
OLEFormat.Object主叫
Marshal.QueryInterface萃取
据此(以确定
适当持久性接口),
铸造,然后调用
将适当的方法。根据
这持久性接口使用,
你可能需要调用一些额外的
方法揭露了一个文件的顶部适当的
存储。此外,
取决于嵌入式
对象的类型,你可能仍然需要激活
对象能够
成功的QueryInterface为
持久性接口之前。




所以,我感兴趣的是其中暴露接口的对象正在实施。我能找到的最接近的是的这里。到目前为止的代码是下面,并与Marshal.QueryInterface任何帮助是极大的赞赏。

  //打开word文档
对象缺少= Type.Missing;
this.document = wordApp.Documents.Open(
裁判FN,裁判confirmConversions,裁判只读,已参考失踪,失踪参考,参考失踪,
参考失踪,失踪参考,参考失踪,裁判失踪,失踪参考,
参考失踪,失踪参考,参考失踪,失踪参考,参考失踪);

的foreach(Microsoft.Office.Interop.Word.InlineShape inlineShape在this.document.InlineShapes)
{
如果(inlineShape.OLEFormat.ProgID!= NULL)
{
开关(inlineShape.OLEFormat.ProgID)
{
//这是一个PDF文件
案AcroExch.Document.7:
//元帅.QueryInterface(IntPtr的朋克,裁判的Guid IID,出的IntPtr PPV)​​;
中断;
默认:
中断;
}
}
}


解决方案

Marshal.QueryInterface 不应该是必要的 - 如果你把一个COM对象,并将其转换为一个COM接口类型,.NET确实在的QueryInterface 你的电话。也就是说,你可以这样写: IPersistStorage坚持=(IPersistStorage)OBJ;



但它不是我清楚哪些对象中的代码执行 IPersistStorage 的IPersistStreamInit 等。


I am trying to work with some embedded objects in Word documents. An earlier poster told me that this is not straight forward. Here is an excerpt of the linked answer:

"As I mentioned earlier, utilizing the embedded object's programming model to perform the save is something of a shortcut. There is a more involved solution that will work with any embedded object. In order for the object to be embedded in the first place, it must support one of the COM IPersist interfaces (i.e IPersistStorage, IPersistStreamInit, IPersistFile, etc). Therefore, an embedded object can always be extracted by calling Marshal.QueryInterface on the OLEFormat.Object (to determine the appropriate persistance interface), casting accordingly and then calling the appropriate method. Depending on which persistence interface you use, you may need to call some additional methods to expose the appropriate storage over the top of a file. Also, depending on the type of embedded object, you may still need to activate the object prior to being able to successfully QueryInterface for the persistance interfaces."

So I am interested in exposing which interface the object is implementing. The closest I could find is here. The code so far is below and any help with the Marshal.QueryInterface is greatly appreciated.

// Opening the word document
object missing = Type.Missing;
this.document = wordApp.Documents.Open(
                ref fn, ref confirmConversions, ref readOnly, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing);

foreach (Microsoft.Office.Interop.Word.InlineShape inlineShape in this.document.InlineShapes)
            {
                if (inlineShape.OLEFormat.ProgID != null)
                {
                    switch (inlineShape.OLEFormat.ProgID)
                    {
                        // This is a pdf file
                        case "AcroExch.Document.7":
                            //Marshal.QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv);
                            break;
                        default:
                            break;
                    }
                }
            }

解决方案

Marshal.QueryInterface shouldn't be necessary - if you take a COM object and cast it to a COM interface type, .NET does the QueryInterface call for you. That is, you can write: IPersistStorage persist = (IPersistStorage) obj;

However it's not clear to me which object in the code implements IPersistStorage, IPersistStreamInit etc.

这篇关于如何使用Marshal.QueryInterface?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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