我需要远程加载一个包含 ActiveX 对象(非可视)的 .NET DLL,然后使用新的 ActiveXObject() 方法通过 javascript 访问它。
当前 IE8 使用对象标记上代码库属性的路径正确加载此 DLL,但由于 ActiveX 引导程序未在注册表中找到 DLL,ActiveXObject 失败。
我正在使用 ProcMon 来跟踪正在发生的事件,并且可以验证是否正在下载 DLL,以及是否正在通过新的 ActiveXObject 方法探测注册表。第二部分失败了,因为 ActiveX 对象不在注册表中。
<body>
<object
name="Hello World"
classid="clsid:E86A9038-368D-4e8f-B389-FDEF38935B2F"
codebase="http://localhost/bin/Debug/Test.ActiveX.dll">
</object>
<script type="text/javascript">
var hw = new ActiveXObject("Test.ActiveX.HelloWorld");
alert(hw.greeting());
</script>
</body>
如果我使用regasm我可以提供必要的注册然后一切正常,但是我不想为此目的部署安装程序 - 我知道 IE 应该为我注册 DLL - 我只是不知道这是什么机制.
.NET 类具有使这一切在 regasm 中工作的必要属性,但似乎没有调用注册表代码。(注册码是从这里偷的)
namespace Test
{
[Guid("E86A9038-368D-4e8f-B389-FDEF38935B2F")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface IHelloWorld
{
[DispId(0)]
string Greeting();
}
[ComVisible(true)]
[ProgId("Test.ActiveX.HelloWorld")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IHelloWorld))]
public class HelloWorld : IHelloWorld
{
[ComRegisterFunction()]
public static void RegisterClass(string key)
{
// Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
StringBuilder sb = new StringBuilder(key);
sb.Replace(@"HKEY_CLASSES_ROOT\ ", ""); // <-- extra space to preserve prettify only.. not in the real code
// Open the CLSID\{guid} key for write access
using (RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true))
{
// And create the 'Control' key - this allows it to show up in
// the ActiveX control container
using (RegistryKey ctrl = k.CreateSubKey("Control"))
{
ctrl.Close();
}
// Next create the CodeBase entry - needed if not string named and GACced.
using (RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true))
{
inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
inprocServer32.Close();
}
// Finally close the main key
k.Close();
}
}
...
public string Greeting()
{
return "Hello World from ActiveX";
}
}
}