How can I deploy a C# ActiveX object via a remotely loaded DLL (without prior registration) using IE8/IE9 and then access it with javascript?

StackOverflow https://stackoverflow.com/questions/4789110

سؤال

I need to remotely load a .NET DLL that contains an ActiveX object (non-visual) and then access it via javascript using the new ActiveXObject() method.

Currently IE8 is correctly loading this DLL with the path from the codebase attribute on the object tag, but the ActiveXObject is failing due the ActiveX bootstrapper not finding the DLL in the registry.

I am using ProcMon to track the events that are occurring, and can verify that the DLL is being downloaded, and that the registry is being probed by the new ActiveXObject method. this second part is failing though since the ActiveX object is not in the registry..

<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>

If I use regasm I can provide the necessary registrations and then it all works, however I don't want to deploy an installer for this purpose - I understand that IE should register the DLL for me - I just don't know what mechanism does this.

The .NET class has the necessary attributes to make this all work within regasm, but it seems that the registry code is not being invoked. (Registration code was poached from here)

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";
        }
    }
}
هل كانت مفيدة؟

المحلول

The simple answer to this question is that you can't; IE will not load an activex control that it does not know about.

What you can do is find a way to install the DLL (and thus register the ActiveX control) and then load it after it has been installed. Natively, this isn't possible to do "automatically" without any prompt from the user, probably because that would be a nightmareish security hole.

Now, if you're just looking for the "native" install method for ActiveX control, that would be a CAB file that you specify. It won't do everything for you "automatically", but it can be used to have IE prompt the user to install the ActiveX control, which will cause your installer to run.

You can find information on how to use this method at http://msdn.microsoft.com/en-us/library/aa751974(v=vs.85).aspx

Doing what you're trying to do in C# is probably not a very flexible method; it will only work if the user has .net installed, and I have never tried using a CAB file to install a .net ActiveX control. I would recommend that you consider using a regular browser plugin or C++ ActiveX control. You could look at FireBreath for creating something in C++ that would work as an ActiveX control but also on all other browsers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top