Sending JavaScript code from c# windows application to a running instance of IE does not work?
<p>I'm working on a project in which I need to send a JavaScript function from c# windows application to a running instance of internet explorer and then the IE run the script and return the result to my c# application. (Very confusing? =) )</p>

<p>Just to make it more clear, here is the case:</p>

<p>I get the <code>IHTMLDocument2</code> of the running instance of internet explorer like this:</p>

<pre><code> htmlDocument = ObjectFromLresult(lResult,typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2;
</code></pre>

<p>Then I want to send a JavaScript function to the IE instance, the code can be something like this:</p>

<pre><code>string code = "function myTest() { alert('ready'); } myTest();";
</code></pre>

<p>I use the following code to send the JavaScript code:</p>

<pre><code>htmlDocument.Script.GetType().InvokeMember("eval", System.Reflection.BindingFlags.InvokeMethod, null, htmlDocument.Script, new object[] { code });
</code></pre>

<p><strong>now here is the question:</strong></p>

<p>Sending the above simple JavaScript code to IE works fine, but if I want to send codes that work with events (like <code>onmouseover</code>, <code>onmouseclick</code> ,etc), it  does not work and results in the following exception:</p>

<p><em>TargetInvocationException was unhandled.
Exception has been thrown by the target of an invocation.</em></p>

<p>Q: how I can send JavaScript codes that use events like :</p>

<pre><code>document.body.onmouseover = function(mEvent){...};
</code></pre>

<p><strong>note:</strong> It's not ASP.net or web application, It's desktop application.</p>

<p>I always appreciated the knowledge and great ideas of you. Thanks in advance. :)</p>
