开发者

Problem calling Marshal.QueryInterface

I am trying to determine the interface of a COM object by calling the Marshal.QueryInterface method.

[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;
            开发者_如何学Pythonobject 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;

I don't have experience with Interop. When calling Marshal.QueryInterface, it complains about the "out myGuid" parameter. What am I doing wrong?


Nevermind. I figured it out.

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

Does the job, except Marshal.QueryInterface doesn't accept IntPtr.Zero as a parameter. But that's another question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜