How to use Windows controls (ActiveX) in the Web [go]

Source: Internet
Author: User

Recently done a Web project, you need to play the webcam captured live video in the Web page, we already have a play video of the use of C # written in the Windows control, how to embed it in the Web page? This requires the use of an ancient technique, ActiveX.

1. Convert. NET controls to ActiveX controls

The first thing to do is to convert our Windows video playback controls into ActiveX controls. First look at the definition of our video playback controls, which are based on the OMCS implementation and are fairly straightforward:

    public partial class Cameravideoplayer:usercontrol    {        private imultimediamanager Multimediamanager;        Public Cameravideoplayer ()        {            InitializeComponent ();        }        public void Test ()        {            random ran = new Random ();            string userID = "BB" + ran. Next (1001,9999). ToString ();            This. Initialize ("223.4.180.116", 9900, UserID, "aa01");        }        public void Initialize (string serverip, int port, string UserID, String Targetuserid)        {            try            {                This.multimediamanager = Multimediamanagerfactory.getsingleton ();                This.multimediaManager.Initialize (UserID, "", ServerIP, port);                This.cameraConnector1.BeginConnect (Targetuserid);            }            catch (Exception ee)            {                MessageBox.Show (EE. Message);            }        }

When its initialize method is called, it connects to the target user's camera and plays the video on its included CameraConnector1 control. This control works well in the Windows Form application, and now we step through the process to convert it to an ActiveX control.

(1) GUID

The ActiveX control is first a COM component, and the COM component has a unique GUID. As we can see later, in the Web, you need to locate and load the registered ActiveX control through a GUID.

If you are using VS2010, there is a "Create GUID" menu under the Tools menu, click it to create a new GUID, and then copy it as the Cameravideoplayer feature:

    [Guid ("d9906b42-56b3-4b94-b4f9-a767194a382f")]    public partial class Cameravideoplayer:usercontrol
(2) Implement IObjectSafety interface

When an ActiveX control is called in a browser, a warning box appears, prompting the unsafe control to be running. This is limited by the browser security policy, and the control implements the IObjectSafety interface to show the browser that it is legitimate. Add the definition of the IObjectSafety interface in the project:

    [Guid ("cb5bdc81-93c1-11cf-8f20-00805f2cd064"), InterfaceType (Cominterfacetype.interfaceisiunknown)]    Public interface IObjectSafety    {               void getinterfaccesafyoptions (System.Int32 riid,out System.Int32 Pdwsupportedoptions,out System.Int32 pdwenabledoptions);        void SetInterfaceSafetyOptions (System.Int32 riid, System.Int32 dwoptionssetmask, System.Int32 dwEnabledOptions);    }

And let Cameravideoplayer implement this interface:

    [Guid ("d9906b42-56b3-4b94-b4f9-a767194a382f")] public partial class Cameravideoplayer:usercontrol, IObjectSafety        {Private Imultimediamanager Multimediamanager;        Public Cameravideoplayer () {InitializeComponent ();            } public void Test () {Random ran = new Random (); string userID = "BB" + ran. Next (1001,9999).            ToString (); This.        Initialize ("223.4.180.116", 9900, UserID, "aa01");            public void Initialize (string serverip, int port, string UserID, String targetuserid) {try                {This.multimediamanager = Multimediamanagerfactory.getsingleton ();                This.multimediaManager.Initialize (UserID, "", ServerIP, Port);            This.cameraConnector1.BeginConnect (Targetuserid); } catch (Exception ee) {MessageBox.Show (EE.            Message); }} public void GetinterfaccesafyoptioNS (int riid, out int pdwsupportedoptions, out int pdwenabledoptions) {pdwsupportedoptions = 1;        Pdwenabledoptions = 2;    } public void SetInterfaceSafetyOptions (int riid, int dwoptionssetmask, int dwenabledoptions) {} }

The implementation of the two methods of the IObjectSafety interface can be done using the code above.

(3) Assembly settings

Next, we need to make a setting for the control's Assembly (Omcs_activex) to indicate that it will be used as a COM component. Open the AssemblyInfo.cs file, and first set the ComVisible attribute to True. Second, increase the allowpartiallytrustedcallers characteristics. As shown below:

    Set ComVisible to False to make the types    ///COM components in this assembly invisible. If you need to access a type from COM in this assembly,    //sets the ComVisible attribute on that type to true.    [Assembly:comvisible (true)]    [Assembly:allowpartiallytrustedcallers ()]

Finally, on the build page of the project properties, check the checkbox for COM Interop registration.

Thus, in addition to Omcs_activex.dll, the generated product of the compilation has omcs_activex.tlb (the type library file used by COM).

2. Making the installation program

The converted Cameravideoplayer ActiveX control is deployed on the IIS server, the first time a user opens a Web page, the control does not exist on the user's machine, so it is necessary to download the installation and register the ActiveX control on the user's machine. These can be achieved through the features of the VS bring-your-own setup program, and it's fairly straightforward.

(1) Add a new Setup project to the current solution.

(2) Import the main output of the Omcs_activex project into the installation project under "Application Folder".

(3) Modify the main output the file installation property of the register entry is vsdrpcom.

(4) Set the project properties of the Setup project, primarily the "Install URL" entry, to be set as the deployment-time address.

(5) If necessary, tick or remove some of the items in prerequisites.

(6) Compile the installation project, will generate two files Setup.exe, Setup1.msi. Copy them to the root directory of the Web site virtual directory.

3.Web Integration

Now let's write one of the simplest HTML to try to load the video playback of the ActiveX control Cameravideoplayer. As shown below:

Note the bold section, which shows two points:

(1) The browser uses the GUID to locate the ActiveX control.

(2) If the target ActiveX control does not exist on this computer, the installation program that is indicated by the CodeBase property is automatically downloaded.

After you have deployed the HTML file, open the Web page for the first time, as follows:

After you run the installation, the page refreshes and you can see that the ActiveX control has been successfully loaded in. Then, click the "Connect Webcam" button to test if the ActiveX control works as follows:

This way, ActiveX controls that are embedded in a Web page run as normal as normal Windows controls:)

How to use Windows controls (ActiveX) in the Web [go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.