C#调用htmlfile组件,并执行js函数

时间:2022-04-30 22:01:37

联系QQ:564955427

C#调用com组件需要使用CreateInstance,当然,我们也可以通过反编译vb.net里面的CreatObject来修改成C#代码。我从网上下载了一个

[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)] public static object CreateObject(string ProgId, [Optional, DefaultParameterValue("")] string ServerName) { object obj2; if (ProgId.Length == 0) { throw new Exception ("Creatobject error!"); } if ((ServerName == null) || (ServerName.Length == 0)) { ServerName = null; } else if (Thread.CurrentThread.CurrentCulture.CompareInfo.Compare(Environment.MachineName, ServerName, CompareOptions.IgnoreCase) == 0) { ServerName = null; } try { Type typeFromProgID; if (ServerName == null) { typeFromProgID = Type.GetTypeFromProgID(ProgId); } else { typeFromProgID = Type.GetTypeFromProgID(ProgId, ServerName, true); } obj2 = Activator.CreateInstance(typeFromProgID); } catch (COMException ex) { if (ex.ErrorCode == -2147023174) //800706ba The RPC server is unavailable, The server name is wrong in the client machine‘s System Registry entry for the remote application { throw new Exception("Creatobject error! the RPC server is unavailable!"); } throw new Exception("Creatobject error! " + ex.Message); } catch (Exception e) { throw new Exception("Creatobject error! " + e.Message); } return obj2; }

  这样,我们也可以在C#里面直接使用CreatObject了

我们还使用前一篇文章中用到的测试网站,改成c#代码如下:

dynamic ohtmlDoc = CreateObject("htmlfile"); string js = 要反混淆的js代码; //1、反混淆js js = "document.write(" + js.Substring(5); ohtmlDoc.write("<html>"); js = "<script>" + js + "</script>"; ohtmlDoc.write(js); ohtmlDoc.write("</html>"); string html = ohtmlDoc.body.innertext; //反混淆之后的html

  然后再使用htmlfile.parentWindow.execScript(js代码),,就可以通过eval执行js函数了。

需要注意的是,dynamic 只在.net 4.0之后支持,如果是之前的版本,请使用反射调用。