刚接触C#,头叫我用C#写个组件,然后用JavaScript再调用一下那组件,结果搞得自己很郁闷
跟上面说的那样,我在网上找了N久,度娘古哥都找上了。。。就是找不到解决的办法。。。
现在有怀疑是不是自己写的COM组件有问题,这里也顺便把代码也拷上来,反正也只是自己在学着整的。。。
希望能得到各大虾的指点。。。我真的是搞得快崩溃了。。。谢先
- C# code
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace mcmath { [Guid("0939781F-2776-442b-AB4E-A57317856E9D")] public class mcMathComp:IDisposable { public bool bTest = false; public long Add(long val1, long val2) { return val1 + val2; } public bool Extra { get { return bTest; } set { bTest = Extra; } } #region IDisposable 成员 public void Dispose() { throw new Exception("The method or operation is not implemented."); } #endregion #region IDisposable 成员 void IDisposable.Dispose() { throw new Exception("The method or operation is not implemented."); } #endregion } }
- JScript code
<HTML> <HEAD> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <TITLE>Test My DLL </TITLE> <script language="JavaScript" type="text/javascript"> var obj = new ActiveXObject("mcmath.mcMathComp");//mcmath.mcMathComp是这个dll用regasm注册后在注册表里可以看到的组件名称 function init() { TestDLL(); } function TestDLL() { var sum = obj.add(23, 60); //var ken = obj.add(22, 20); //alert ("Load success666!!!"); //alert("ken is " + ken); alert("sum is "+sum); } </script> <OBJECT id = "mcMathComp" name = "mcMathComp" classid = "0939781F-2776-442B-AB4E-A57317856E9D" codebase = "mcmath.dll#version=1,0,0,0"> </OBJECT> <META content="text/html; charset=gb2312" http-equiv=Content-Type> </HEAD> <BODY id=tx_config onload=javascript:init()> <input name="Submit4" type="button" style="width:200px" onClick="javascript:TestDLL();return false;" value="Test DLL Now!!!"> </BODY> </HTML>
------解决方案--------------------
检查ie的安全性设置,允许不安全的 activex
------解决方案--------------------
1.确定你使用的客户端浏览器是IE
2.确定使用这个浏览器的PC注册了这个控件
3.确定IE设置的安全策略是否允许执行它。