调用Marshal.QueryInterface的问题

时间:2011-02-22 14:26:47

标签: c# interop

我试图通过调用Marshal.QueryInterface方法来确定COM对象的接口。

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

public WordFileExtracter(string filename, string contentPrefix, int startAttachmentNumber)
        {
            var wordApp = new Word.Application();
            object confirmConversions = false;
            object readOnly = true;

            object missing = Type.Missing;

            // Opening the Word document
            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)
                    {
                        case "AcroExch.Document.7":
                            Guid myGuid = new Guid();
                            IntPtr pInterface;

                            // ERROR! Argument '2': cannot convert
                            // from 'int' to 'ref System.Guid'
                            Marshal.QueryInterface(IntPtr.Zero, CLSIDFromProgID("AcroExch.Document.7", out myGuid), out pInterface);

                            // If for example it implements the 
                            // IPersistStorage interface, then I
                            // cast to this type
                            IPersistStorage persist = (IPersistStorage)inlineShape.OLEFormat.Object as IPersistStorage;

我没有使用Interop的经验。在调用Marshal.QueryInterface时,它会抱怨“out myGuid”参数。我究竟做错了什么?

1 个答案:

答案 0 :(得分:2)

没关系。我想通了。

CLSIDFromProgID("AcroExch.Document.7", out myGuid);
Marshal.QueryInterface(IntPtr.Zero, ref myGuid, out pInterface);

除Marshal.QueryInterface之外的作业是否不接受IntPtr.Zero作为参数。但这是另一个问题。