The following code throws an access violation exception ONLY after publishing as a ClickOnce application and installed. The error doesn't occur if running it from Visual Studio even if I'm on release mode:
public static object InvokeScript(this IHTMLDocument2 document, string scriptName, object[] args = null)
{
FileLog.WriteLine("-------------------------------------- Will invoke script --------------------------------------");
FileLog.WriteLine("document: " + document == null ? "NULL" : document.ToString());
FileLog.WriteLine("scriptName: " + scriptName);
FileLog.WriteLine("args: " + (args == null ? "NULL" : string.Join(", ", args.Select(a => a == null ? "NULL" : a.ToString()))));
FileLog.WriteLine("Stack: " + GetStack());
object result = null;
try
{
Type type = GetType(document);
result = Invoke(document, scriptName, args, result, type);
}
catch (Exception ex)
{
if (IsSecurityOrCriticalException(ex))
{
throw;
}
}
FileLog.WriteLine("-------------------------------------- Invoked script --------------------------------------");
return result;
}
private static object Invoke(IHTMLDocument2 document, string scriptName, object[] args, object result, Type type)
{
FileLog.WriteLine("Will Invoke");
result = type.InvokeMember(scriptName, System.Reflection.BindingFlags.InvokeMethod, null, ((IHTMLDocument)document).Script, args);
FileLog.WriteLine("Invoked");
return result;
}
private static Type GetType(IHTMLDocument2 document)
{
FileLog.WriteLine("Will get type");
Type type = (((IHTMLDocument)document).Script).GetType();
FileLog.WriteLine("Got type");
return type;
}
This is my attempt to implement this suggestion, but seems like I'm doing something wrong. Also, the script won't always throw an exception. I've tested several pages and they all worked fine, except for this site.
The exception happen just in the Type type = (((IHTMLDocument)document).Script).GetType();
line (the last thing being logged is "Will get type").
The script I"m attempting to run is a script that doesn't exist on the page yet (is basically a test to see if the script exists and if it doesn't then I'd inject the script into the page).
Any idea what I'm doing wrong?