public static object InvokeScript(IHTMLDocument doc, string scriptName, object[] args) { object result = null; NativeMethods.tagDISPPARAMS tagDISPPARAMS = new NativeMethods.tagDISPPARAMS(); tagDISPPARAMS.rgvarg = IntPtr.Zero; try { IDispatch dispatch = doc.Script as IDispatch; if (dispatch != null) { Guid empty = Guid.Empty; string[] rgszNames = new string[] { scriptName }; int[] array = new int[] { -1 }; int iDsOfNames = dispatch.GetIDsOfNames(ref empty, rgszNames, 1, SafeNativeMethods.GetThreadLCID(), array); if (SafeNativeMethods.Succeeded(iDsOfNames) && array[0] != -1) { if (args != null) { Array.Reverse(args); } tagDISPPARAMS.rgvarg = ((args == null) ? IntPtr.Zero : SafeNativeMethods.ArrayToVARIANTVector(args)); tagDISPPARAMS.cArgs = ((args == null) ? 0 : args.Length); tagDISPPARAMS.rgdispidNamedArgs = IntPtr.Zero; tagDISPPARAMS.cNamedArgs = 0; object[] array2 = new object[1]; if (dispatch.Invoke(array[0], ref empty, SafeNativeMethods.GetThreadLCID(), 1, tagDISPPARAMS, array2, new NativeMethods.tagEXCEPINFO(), null) == 0) { result = array2[0]; } } } } catch (Exception ex) { if (ClientUtils.IsSecurityOrCriticalException(ex)) { throw; } } finally { if (tagDISPPARAMS.rgvarg != IntPtr.Zero) { SafeNativeMethods.FreeVARIANTVector(tagDISPPARAMS.rgvarg, args.Length); } } return(result); }