Type.InvokeMember Throwing "COMException: Type mismatch" inside Extension method

0

I'm working with Adobe Acrobat SDK, and I am trying to clean up the COM calls by wrapping the InvokeMember call in an Extension method to make the code a bit more readable.

The extension method I am trying to write is as follows:

using System;
using System.Reflection;

public static class Invoke_Extension
{
    public static object CallJSfunction(this Object adobeComObject, string sJSfunction, params object[] oPlist)
    {

        Type T = adobeComObject.GetType();

        return T.InvokeMember(
                        sJSfunction,
                        BindingFlags.GetProperty | //fixed per Simon's comment
                        BindingFlags.Public |
                        BindingFlags.Instance,
                        null, adobeComObject, oPlist);
    }
}

It would replace the COM calls in Adobe's example code as follows:

    // Example from the Adobe SDK sample code
using System;
using System.Reflection;
using Acrobat;
//---------------- Create necessary objects ----------------------
    AcroAVDoc g_AVDoc = new AcroAVDoc();
    g_AVDoc.Open(filename, "");
    CAcroPDDoc pdDoc = (CAcroPDDoc)g_AVDoc.GetPDDoc();
    //Acquire the Acrobat JavaScript Object interface from the PDDoc object
    Object jsObj = pdDoc.GetJSObject();

//------- This is the part I am trying to make more readable --------
    Type T = jsObj.GetType();

    // total number of pages
    double nPages = (double)T.InvokeMember(
                "numPages",
                BindingFlags.GetProperty |
                BindingFlags.Public |
                BindingFlags.Instance,
                null, jsObj, null);

//-------------------------------------------------------------------------
//-- using the extension method, the two calls above are replaced with: ---

    double nPages2 = (double)jsObj.CallJSfunction("numPages", null);
/--------------------------------------------------------------------------
// ...but the InvokeMember call within the extension method is throwing an error.




//BTW, the optional parameters in the Extension method is because some of the functions have more than one parameter eg.
    object[] addTextWatermarkParam = { currentTime.ToShortTimeString(), 1, "Helvetica", 100, blueColorObj, 0, 0, true, true, true, 0, 3, 20, -45, false, 1.0, false, 0, 0.7 };
    T.InvokeMember(
            "addWatermarkFromText",
            BindingFlags.InvokeMethod |
            BindingFlags.Public |
            BindingFlags.Instance,
            null, jsObj, addTextWatermarkParam);
// which would be replaced with
    jsObj.CallJSfunction("addWatermarkFromText",currentTime.ToShortTimeString(), 1, "Helvetica", 100, blueColorObj, 0, 0, true, true, true, 0, 3, 20, -45, false, 1.0, false, 0, 0.7 );

The complete error reads:

System.Reflection.TargetInvocationException HResult=0x80131604 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args) at Invoke_Extension.CallJSfunction(Object adobeComObject, String sJSfunction, Object[] oPlist) in Z:\mike\Downloads\Adobe\Acrobat DC SDK\Version 1\InterAppCommunicationSupport\C#Samples\JSObjectControlCS\JSObjectControlCS\Invoke_Extension.cs:line 11 at JSObjectControlCS.JSObjectControlForm.searchButton_Click(Object sender, EventArgs e) in Z:\mike\Downloads\Adobe\Acrobat DC SDK\Version 1\InterAppCommunicationSupport\C#Samples\JSObjectControlCS\JSObjectControlCS\Form1.cs:line 239 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at JSObjectControlCS.Program.Main() in Z:\mike\Downloads\Adobe\Acrobat DC SDK\Version 1\InterAppCommunicationSupport\C#Samples\JSObjectControlCS\JSObjectControlCS\Program.cs:line 17

Inner Exception 1: COMException: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

c#
com
extension-methods
acrobat-sdk
asked on Stack Overflow Apr 4, 2019 by mike • edited Apr 4, 2019 by mike

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0