InvokeMember --- Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

Add-in Express™ Support Service
That's what is more important than anything else

InvokeMember --- Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
Trying to bind my addon module to javascript and failing. 
Robert Apostolico




Posts: 70
Joined: 2013-09-06
I must be missing something.
I'm having issues similar to this thread:
http://www.add-in-express.com/forum/read.php?PAGEN_1=1&FID=10&TID=9771&2=&10=&9771=&3=#nav_start
but cannot find the solution.

The name of my C# module is IEModule, running in the namespace SigBOP (in case any of that matters).

Inside the C# add-in, I have the following public method:

        public void MyMethod(ref string xsDataParameter) {
            MessageBox.Show( new WinAPIwindow( this.ParentHandle), xsDataParameter);
            xsDataParameter = xsDataParameter + " i was changed by c#";
        }


I also inject my javascript into the page via DocumentComplete2 event:


        private void IEModule_DocumentComplete2(object pDisp, string url, bool rootDocLoaded) {
            try {
                if (rootDocLoaded && IsTargetDomain(url)) {
                    injectController();
                }
            }
            catch (Exception ex) {
                SigBOP.Utils.ErrorWriteToLog(ex);                
            }
        }


The code is properly injected, as I have javascript console.log ("info: my plugin injected/etc"); in the code and that text/information is logged into the console window; so I know the injection portion is working.


In JAVASCRIPT code that's injected into the page, I have the following variable declared @ top of module:

//  this is defined here so the C# program can alter its value/turn it into a pointer back to the IEModule.cs
var myAddOn = null;


// At the BOTTOM of the injected code, is a javascript test-method/sanity check:
function InvokeCsharpLibrary() {
    try {
        if (myAddOn !== null) {
            var psOriginal = "Original Value";
            myAddOn.MyMethod(psOriginal);
            alert(psOriginal);
        } else {
            console.log("info: InvokeCsharpLibrary - myAddOn === null");
        }

    } catch (e) {
        console.log("ERROR: InvokeCsharpLibrary - " + e.message);
    }
}



Back in the C# add-in, I have a command button, with the following code attached to its click event:


        private void adxCmdSigBOPicon_OnClick(object sender, object htmlDoc) {

            try {


                //  attach the c# ADXIE add-on to the browser js session
                //  NOTE: myAddOn is defined, and set to Null in gmToolBar_js module!
                //  this code below redefines myAddOn as a pointer to this module.
                //  then the js code can call/access public methods here!
                try {
                    //object poEngine = HTMLDocument.scripts;
                    //object []poRef   = {this};
                    //int response = (int) poEngine.GetType().InvokeMember("myAddOn",BindingFlags.SetProperty, null, poEngine, poRef );
                    object scriptEngine = HTMLDocument.scripts;
                    if (scriptEngine != null) {

                        // >> the below line causes the exception <<
                        scriptEngine.GetType().InvokeMember("myAddOn", BindingFlags.SetProperty, null, scriptEngine, new object[] {this});

                    }
                }
                catch (Exception ex) {
                    SigBOP.Utils.ErrorWriteToLog(ex);
                }


                try {
                    HTMLDocument.parentWindow.execScript("InvokeCsharpLibrary();");
                }
                catch (Exception ex) {
                    SigBOP.Utils.ErrorWriteToLog(ex);
                }

            }
            catch (Exception ex) {
                SigBOP.Utils.ErrorWriteToLog(ex);
            }
        }




When I click my Add-in's button and it attempts to perform the InvokeMember, I get this exception:

04:05:15.232 --- Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
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 SigBOP.IEModule.adxCmdSigBOPicon_OnClick(Object sender, Object htmlDoc) in c:\Test\ie\SigBOP\SigBOP\IEModule.cs:line 190
mscorlib


However, I can also see in the F12 tools console, the below message:
info: InvokeCsharpLibrary - myAddOn === null

So the button got clicked, it attempted to bind the IEModule to the JS session and failed,
but was still able to successfully call the "InvokeCsharpLibrary". (So, I've got the toolbar able to run the JS code, but NOT the other way around.)

As pointed out by others in the above mentioned thread, i've got access to HTMLDocument.scripts,
NOT an HTMLDocument.Script ... which may be the underlying cause??

What am I missing??

Thank you!
BApostolico
Posted 20 May, 2014 15:35:06 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Robert,

Try to change the definition of the MyMethod method to 'public string MyMethod(string xsDataParameter)'.
Posted 21 May, 2014 05:04:11 Top
Robert Apostolico




Posts: 70
Joined: 2013-09-06
I made the following changes:

In the JavaScript

function InvokeCsharpLibrary() {
    try {
        if (myAddOn !== null) {
            var psOriginal = "Original Value";
            var psChanged  = myAddOn.MyMethod(psOriginal);
            alert(psChanged);
        } else {
            console.log("info: InvokeCsharpLibrary - myAddOn === null");
        }

    } catch (e) {
        console.log("ERROR: InvokeCsharpLibrary - " + e.message);
    }
}


In the C# add-on:

        public string MyMethod(string xsDataParameter) {
            MessageBox.Show( new WinAPIwindow( this.ParentHandle), xsDataParameter);
            return xsDataParameter + " i was changed by c#";
        }


I'm still getting the same exception:
08:17:26.966 --- Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
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 SigBOP.IEModule.adxCmdSigBOPicon_OnClick(Object sender, Object htmlDoc) in c:\Test\ie\SigBOP\SigBOP\IEModule.cs:line 190
mscorlib

The problem (as far as I can tell) does not stem from the name or type of MyMethod.
I never get to call that because the button-click code that attempts to assign the myAddOn pointer to point to the ADXIE IEModule fails:



                try {
                    object scriptEngine = HTMLDocument.scripts;
                    if (scriptEngine != null) {
                        // this is line #190 !! this is the line causing the exception !! <<<<<<<<<<<
                        scriptEngine.GetType().InvokeMember("myAddOn", BindingFlags.SetProperty, null, scriptEngine, new object[] {this});
                    }
                }
                catch (Exception ex) {
                    SigBOP.Utils.ErrorWriteToLog(ex);
                }


Additionally, as a sanity check, look at the below image that shows BOTH the declaration of myAddOn variable and the trace in the console.


User added an image

Line 2 shows the console.log code.
Line 13 shows the variable declaration.
The "info:" trace in the console window shows the code was executed and variable declared.

So why is the call from C# into the js session failing?

Running IE11, Windows 7 64-bit, 24 GB ram.

PLEASE ADVISE.
BApostolico
Posted 21 May, 2014 07:31:13 Top
Robert Apostolico




Posts: 70
Joined: 2013-09-06
Reminder/Follow-up question:

Is the above problem tied in any way to the differences between:

HTMLDocument.Script

-vs-

HTMLDocument.scripts

???
BApostolico
Posted 21 May, 2014 07:39:04 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Robert,

In our examples we use 'HTMLDocument.Script'.
Posted 21 May, 2014 08:39:03 Top
Robert Apostolico




Posts: 70
Joined: 2013-09-06
UPDATE: Solved!
The last question ( HTMLDocument.Script vs HTMLDocument.scripts ) is MOST definitely the key.

MORE INFO:
********** HTMLDocument.Script is a HIDDEN member of the object.
********** use object browser ( Ctrl-W J ) hit the gear/settings icon, [x] Show Hidden Types and Members!

You'll then SEE the grayed-out "Script" member.
THAT is what you need to reference.

Even with hidden members shown, MS Intellisense will probably mess-up what you're trying to type.
I just double-checked; if you type ".Script;" when you hit the semicolon, the IDE changes it to ".scripts;"
You have to LEAVE the semicolon on the line, delete the trailing s and change the leading s to "S".
ONLY then will it leave what you typed alone.

THEN you'll be able to call your add-in code/module from the JavaScript session!
BApostolico
Posted 21 May, 2014 08:39:07 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Thank you very, very much! Together we stand!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 22 May, 2014 01:30:03 Top