ActiveX Development of Browser plugins (iv) __activex

Source: Internet
Author: User
Tags access properties unique id

A brief summary of the previous articles of the content, "browser plugin ActiveX development (i)" briefly introduced how to use C + + Vs.net 2008 under the development of MFC based ActiveX Plug-ins, "Browser plugin ActiveX development (ii)" Introduces the problems that you may encounter when developing Plug-ins, the ActiveX development of browser Plug-ins (iii) Describes how to register plug-ins and how to package them into CAB files. However, so far there is no specific reference to how to invoke Plug-ins in a Web page, this article is focused on this issue.

First, use <Object> tag to invoke ActiveX

1. Basic usage of object label

The easiest and most common way to invoke an ActiveX plug-in in an HTML page is to:

<object id= "Cardaccessor" 
    classid= "clsid:03ad53e8-d7e7-485d-a39a-d07b37defbc9"     
    width= "0" 
    height= "0" >
</object>

The id attribute does not have to be explained, as is the ID of the other elements in the HTML, the unique identification of the elements in the DOM tree. Width and height represent the size of the ActiveX in a Web page, and set it to 0 for an ActiveX that only provides an interface without a UI interface, because there is no need to display anything on the page (for ActiveX that needs to display the interface, You need to create dialog and write logic in your project, referring to the "A Complete ActiveX Web control Tutorial" instance.

The ClassID attribute is a very critical attribute here, and it is through him that IE can correctly find the ActiveX to invoke. Each ActiveX has a unique ID to represent, this is classid, when we create the MFC ActiveX control Project Vs.net 2008 to help us generate this ID, you can find this ID value at the bottom of the program's. idl file:

It is generally not recommended to manually modify this UUID value in the program, because this ID value is also used in the XxxxCtrl.cpp file, except that it behaves differently:

After the control is registered successfully, information such as the ClassID and control file location is written to the registry, as follows:

Of course, if ActiveX also defines other attributes, it can also be assigned to them in the form of a property in <object>.

      If the user's computer has already registered the plugin (for example, by Setup.exe), then the HTML reference to the previous code can then invoke the plugin's interface and properties through JS (again, ActiveX can only run in IE browser, that is <object...> This code in Firefox and other browsers are not working properly.   

<fieldset> <legend>read Card No testing</legend>: <input type= "text" id= "Txtcardn O_read "maxlength=" "class=" txt disable "readonly/> <input type=" button "id=" Btnread "value=" Read Cardn O "class=" btn "onclick=" Javascript:readcardno (); "/> </fieldset> <fieldset> <lege Nd>write Card No testing</legend> number: <input type= "text" id= "Txtcardno_write" maxlength= "" class= "TX T "/> <input type=" button "id=" Btnwrite "value=" Write Cardno "class=" btn "onclick=" Javascript:writecardno (); "/> </fieldset> <script type=" Text/javascript "> var txtcardno_read = document.getElementById ("
    Txtcardno_read ");
    var txtcardno_write = document.getElementById ("Txtcardno_write");
   
    var objcard = document.getElementById ("Cardaccessor");
        
        function Readcardno () {txtcardno_read.value = ""; try{var ret = objcard.reAdcardno ();
                if (ret = = 0) {txtcardno_read.value = Objcard.cardno; Alert ("read card successful.")
            "); else {alert ("read card failed.")
            The error code is: "+ ret"; The catch (E) {alert (e.message)}} function Writecardno () {var car

        DNo = Txtcardno_write.value;
            try {objcard.cardno = Cardno;

            var ret = Objcard.writecardno (); if (ret = 0) {alert ("write card succeeded.")
            "); else {alert ("Write card failed.")
            The error code is: "+ ret"; The catch (E) {alert (e.message)}} </script>

2. How to tell if ActiveX is registered when using object label

<script type= "Text/javascript" >
    var objcard = document.getElementById ("Cardaccessor");

    The IF (objcard.object==null) {
        alert ("Cardaccessor plug-in) is not installed. ");
    }
    else{
        alert ("The Cardaccessor plugin has been detected. ");
    }
</script>
You can also determine whether a plug-in is installed by accessing a known-defined property in ActiveX, or if the returned property value is undefined, which indicates that the plug-in is not detected. For example:
<script type= "Text/javascript" >
    var objcard = document.getElementById ("Cardaccessor");

    The IF (OBJCARD.CARDNO==UNDEFINEDL) {
        alert ("Cardaccessor plug-in) is not installed. ");
    }
    else{
        alert ("The Cardaccessor plugin has been detected. ");
    }
</script>

3, how to let ie automatically download and install Plug-ins and intelligent upgrade

If you detect the plugin is not installed, how to let ie automatically download the plugin from the specified location and automatically install it. Quite simply, you can use the CodeBase property in the OBJECT tag:

<object id= "Cardaccessor" 
    classid= "CLSID:03AD53E8-D7E7-485D-A39A-D07B37DEFBC9" 
    cardaccessor.cab#version=1,0,0,1 "
    width=" 0 " 
    height=" 0 ">
</object>

The codebase value is formatted as "xxxxx.cab#version=1,0,0,1". The ' # ' front section is the location of the CAB file, either as an absolute position on the server or as a relative position. The "#" section later represents the version number of the currently referenced cab package. When IE detects that the system does not register the specified plug-in, it downloads the cab from the codebase specified location and copies the files to the specified location and registers the specified controls as described in the. inf file. (Note: In the practical application involves the signature question, after the article again)

Even if the plugin is registered locally, IE will take the registered control version number compared to the version number specified in CodeBase, and if the version number specified in CodeBase is greater than the registered plug-in version number, IE will still download the CAB package from the codebase specified location and re-register the plug-in.

As mentioned in the previous article, for convenience of management, the version number of the OCX required to be registered in the CAB package is generally considered the same as the CAB version number.

When a plug-in or plug-in relies on a file that needs to be upgraded, simply update the appropriate file and upgrade the appropriate file version in the. inf (for ease of administration, regardless of whether the OCX is updated, the version number of the OCX file is upgraded as the version number represents the entire cab). It is then repackaged into a cab and published to the server, and the version number of the CodeBase property value in the OBJECT tag in HTML is updated. The next time the user accesses the page, IE will automatically download the upgraded cab and re-register the plug-in. Actually, after my test, the CAB package on the server does not make any changes, as long as the value of version in CodeBase is added, the corresponding plugin will be downloaded and registered again.

second, the use of JavaScript new ActiveXObject to invoke ActiveX

If you do not use the object label, you can also directly through the JS ActiveXObject to create an instance of the specified ActiveX to achieve the purpose of invoking the plug-in interface (ie under the XMLHttpRequest call is this principle). For example:

var objcard = new ActiveXObject ("UPRAIN.CARDACCESSORCTRL.1");

If the plug-in is already registered, the plug-in interface and access properties can then be invoked via Objcard.

The parameters of the ActiveXObject function are the ProgID of the corresponding plug-ins rather than the classid. The ProgID value of the corresponding plug-in can also be found or modified in the XxxxCtrl.cpp file in the project, as shown in the following figure:

The second parameter of IMPLEMENT_OLECREATE_EX represents the ProgID of the current plug-in, which can be modified to suit the actual needs. In fact, the corresponding classid can be found through ProgID in the registry, and the two are related:

So how do you tell if ActiveX is installed? In fact, if ActiveX is not installed, creating a plug-in object through the new ActiveXObject will throw the "Automation server cannot create object" exception. So you can do it in the following ways:

try {
         Objcard = new ActiveXObject ("UPRAIN.CARDACCESSORCTRL.1");                
    }
catch (e) {
         alert (' Invoke ActiveX failed. ");
    }

However, I encountered two situations in the actual testing process:

1 the "Automation server cannot create objects" exception does not necessarily indicate that the plugin is not installed, it may also be because the plug-in does not implement initialization or scripting security interface, which is blocked by IE, you need to adjust IE "Tools-options-security-Custom Level" in the "ActiveX Controls and Plug-ins" part of the setting;

2 Sometimes, the same registered Plug-ins, through the OBJECT tag reference can normally invoke the interface, but the new ActiveXObject way to invoke the plug-in interface failed.
=======================================================================
Noven (Jasson Qian)
------------------------------------------------------
Blog Park: http://qguohog.cnblogs.com
Csdn:http://blog.csdn.net/sallay




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.