How to use Windows Control (ActiveX) on the web)

Source: Internet
Author: User
Transferred from: bytes?

A recently developed web project needs to play the real-time video captured by the camera on the webpage. We already have a Windows Control that uses C # To play the video, how can I embed it into a webpage? This requires the use of an old technology, ActiveX.

1. Convert. Net Control to ActiveX Control

The first thing we need to do is to convert our windows video playback control into an ActiveX control. Let's take a look at the definition of our video playback control, which is implemented based on omcs, which is quite simple:

    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 the initialize method is called, the video is connected to the target user's camera and played on the cameraconnector1 control. This control works well in the Windows form application. Now we will convert it to ActiveX control step by step.

(1) guid

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

If vs2010 is used, there is a "create guid" menu under the tool menu. Click it to create a new guid and copy it as a feature of cameravideoplayer:

    [Guid("D9906B42-56B3-4B94-B4F9-A767194A382F")]    public partial class CameraVideoPlayer : UserControl
(2) Implement the IObjectSafety interface

When ActiveX controls are called in a browser, a warning box appears, prompting that insecure controls are running. This is restricted by browser security policies. Controls use the IObjectSafety interface to indicate to the browser that they are legal. Add the IObjectSafety interface definition 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 above code can be used to implement both methods of the IObjectSafety interface.

(3) Assembly settings

Next, we need to set the control assembly (omcs_activex) to indicate that it will be used as a COM component. Open the assemblyinfo. CS file and set the comvisible attribute to true. Secondly, the allowpartiallytrustedcallers feature is added. As follows:

// Set comvisible to false to make the type in this Assembly invisible to the COM component. If you need to access the types in this Assembly from com, // set the comvisible attribute on this type to true. [Assembly: comvisible (true)] [Assembly: allowpartiallytrustedcallers ()]

Finally, on the "generate" page of the Project property, check the checkbox of "register for com interoperability.

In this way, in addition to omcs_activex.dll, omcs_activex.tlb (the type library file used by com) is also included in the compilation product ).

2. Create an installer

After conversion, the cameravideoplayer ActiveX control is deployed on the IIS server. When a user opens a webpage for the first time, the control does not exist on the user's machine, download and install the ActiveX control and register it on your machine. These functions can be implemented through the built-in Installation Program Creation function of Vs, which is also quite simple.

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

(2) import the main output of the omcs_activex project to the "Application folder" of the installation project.

(3) modify the register entry in the installation attribute of the main output file to vsdrpcom.

(4) set the project properties of the installation project, mainly the "installation URL" item, to be set as the deployment address.

(5) If necessary, check or remove some items that are required by the system.

(6)compile the installation project and generate two files: setup.exe and setup1.msi. Copy them to the root directory of the Web site virtual directory.

3. Web Integration

Now let's write a simple HTML to try to load the ActiveX control cameravideoplayer for video playback. As follows:

<HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Note that the bold section describes two points:

(1) The browser uses guid to locate ActiveX controls.

(2) If the local machine does not have the target ActiveX control, the installer indicated by the codebase attribute is automatically downloaded for installation.

After you deploy the HTML file, open the webpage for the first time, as shown below:

After the installation is completed, the page is refreshed and you can see that the ActiveX control has been successfully loaded in. Then, click "connect to the camera" to test whether the ActiveX control works properly, as shown below:

In this way, the ActiveX control embedded in the web page runs as normal as a Windows Control :)

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.