Проблема вызова Marshal.QueryInterface - PullRequest
1 голос
/ 22 февраля 2011

Я пытаюсь определить интерфейс COM-объекта, вызывая метод Marshal.QueryInterface.

[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 Ответ

2 голосов
/ 22 февраля 2011

Nevermind.Я понял это.

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

Работает, кроме Marshal.QueryInterface не принимает IntPtr.Zero в качестве параметра.Но это другой вопрос.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...