gpt4 book ai didi

c# - 如何使用 Marshal.QueryInterface?

转载 作者:太空狗 更新时间:2023-10-29 22:55:07 26 4
gpt4 key购买 nike

我正在尝试使用 Word 文档中的一些嵌入对象。较早的张贴者告诉我,这不是直截了当的。以下是链接答案的摘录:

"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."

所以我有兴趣公开对象正在实现的接口(interface)。我能找到的最接近的是 here .到目前为止的代码在下面,非常感谢对 Marshal.QueryInterface 的任何帮助。

// 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 不是必需的 - 如果您采用 COM 对象并将其转换为 COM 接口(interface)类型,.NET 会为您调用 QueryInterface。也就是说,您可以这样写:IPersistStorage persist = (IPersistStorage) obj;

但是我不清楚代码中的哪个对象实现了IPersistStorageIPersistStreamInit 等。

关于c# - 如何使用 Marshal.QueryInterface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5077172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com