簡體   English   中英

我如何使用Marshal.QueryInterface?

[英]How do I use Marshal.QueryInterface?

我正在嘗試使用Word文檔中的一些嵌入對象。 早先的一張海報告訴我,這不是直截了當的。 以下是相關答案的摘錄:

“正如我前面提到的,利用嵌入式對象的編程模型來執行保存是一種快捷方式。有一個更復雜的解決方案可以用於任何嵌入式對象。為了將對象嵌入到第一位,它必須支持其中一個COM IPersist接口(即IPersistStorage,IPersistStreamInit,IPersistFile等)。因此,可以通過調用OLEFormat.Object上的Marshal.QueryInterface(以確定適當的持久性接口)來提取嵌入對象,並相應地進行轉換。然后調用適當的方法。根據您使用的持久性接口,您可能需要調用一些其他方法來在文件頂部公開相應的存儲。此外,根據嵌入對象的類型,您可能仍需要激活能夠成功查詢持久性接口的QueryInterface之前的對象。“

所以我有興趣公開對象正在實現的接口。 我能找到的最接近的是這里 到目前為止的代碼如下,非常感謝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接口類型,.NET將為您執行QueryInterface調用。 也就是說,你可以寫: IPersistStorage persist = (IPersistStorage) obj;

但是我不清楚代碼中的哪個對象實現了IPersistStorageIPersistStreamInit等。

我不確定你打算做什么但是可以調用QueryInterface 唯一的問題是你在這里有一個ProgID ,你需要先從它獲取CLSID 您可以通過pInvoking CLSIDFromProgId函數來完成。

[DllImport("ole32.dll")]
static extern int CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string lpszProgID, out Guid pclsid);

然后,您可以按如下方式調用:

Marshal.QueryInterface(IntPtr.Zero, CLSIDFromProgID(progID), out pInterface);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM