1

I am attempting to call a C# method on my winform from a page opened in a web browser control similar to this:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting%28v=vs.110%29.aspx

On my winform I have this method:

    public void Test(String message)
    {
        MessageBox.Show(message, "client code");
    }

and on my web page I am calling: window.external.Test('called from script code');

When I attempt to call this I get a javascript error: "invalid procedure call or argument"

However when I modify my form's method to take no parameter (like public void Test()) and accordingly make the javascript call without the parameter, it works without any issue. I only have the issue when I am attempting to pass parameters (which I need to do).

more info: I do have the following attributes on my form class:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]

and I am assigning the ObjectForScripting:

webBrowser1.ObjectForScripting = this;

When debugging it does not appear to enter the forms method block.

I must be missing something simple.

noseratio
  • 59,932
  • 34
  • 208
  • 486
user2245759
  • 477
  • 6
  • 17

1 Answers1

0

Dumb mistake. I was evaluating that the function was not undefined as you normally would before calling a function from some other part of code. For some reason this evaluation was where the error was being raised, not the call itself (that was actually ok).

user2245759
  • 477
  • 6
  • 17
  • 1
    Just curious, as I'm facing exactly the same problem now. How did you resolve the error, while still checking if the function is defined? – Tohnmeister Oct 28 '15 at 07:52
  • You could put the function call inside a try/catch block. – Sergey Jul 27 '18 at 10:57
  • i guess you used something like "if (window.external.foo != undefined)" to check if the method is available. This only works when that method takes no parameter. A proper way is to use "if (typeof(window.external.foo) != "undefined")". – Silent Sojourner Feb 19 '20 at 21:34