ActiveX development for browser plug-ins (4)

Source: Internet
Author: User

Briefly summarize the previous articlesArticleThe content of "ActiveX development of browser plug-ins (I)" briefly introduces how to develop ActiveX plug-ins Based on MFC using C ++ in vs.net 2008, activeX development for browser plug-ins (ii) describes problems that may occur during plug-ins development, and ActiveX development for browser plug-ins (III) describes how to register a plug-in and how to package it into a cab file. However, we have not mentioned how to call the plug-in on the Web page so far. This article focuses on this issue.

 

1. Use the <Object> label to call ActiveX

1. Basic Object Tag usage

The simplest and most common method to call ActiveX plug-ins on HTML pages is:

<ObjectID= "Cardaccessor"Classid= "CLSID: 03ad53e8-d7e7-485d-a39a-d07b37defbc9"Width= "0"Height= "0"></Object>

The ID attribute does not need to be interpreted. Like the IDs of other elements in HTML, It is the unique identifier of each element in the DOM tree. Width and height indicate the placeholder size of the ActiveX in the web page. For Activex that only provides interfaces without UI interfaces, set it to 0, because no content needs to be displayed on the page (for ActiveX interfaces that need to be displayed, you need to create a dialog and write the corresponding logic in the project, refer to the "A complete ActiveX web control tutorial" instance ).

ClassidAttribute is a key attribute here, and IE can be used to correctly find ActiveX to be called. Each ActiveX has a unique ID, which is classid. When we create the MFC ActiveX Control Project, vs.net 2008 generates this ID for us.ProgramFind the id value at the bottom of the. IDL file:

Generally, it is not recommended to manually modify the uuid value in the program, because this ID value is also used in the xxxxctrl. cpp file, but the format is different:

After the control is successfully registered, the information such as the classid and control file location are written into the registry, as shown below:

Of course, if ActiveX also defines other attributes, you can assign values to them in the form of attributes in <Object>.

 

If the user's computer has already registered the plug-in (for example, through the setup.exe method), then the HTML reference to the previous sectionCodeThen you can use js to call the interface and attributes of the plug-in. (If you are prompted again, ActiveX can only be run in the IE browser, that is, <object…> This Code does not work properly in Firefox and other browsers ).

 <  Fieldset  >           <  Legend  > Read card no testing </  Legend  >  Card No:  <  Input  Type = "Text"  ID  = "Txtcardno_read"  Maxlength  = "32"  Class  = "TXT disable"  Readonly  />           <  Input  Type  = "Button"  ID  = "Btnread"  Value  = "Read cardno" Class  = "BTN"  Onclick  = "Javascript: readcardno ();"   />      </  Fieldset  >          <  Fieldset  >           <  Legend  > Write Card No testing </  Legend >  Card No:  <  Input  Type  = "Text"  ID  = "Txtcardno_write"  Maxlength  = "32"  Class  = "TXT"   />           <  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 (  "  Card reading successful! "  );}  Else  {Alert (  "  Card reading failed! The error code is:  "   +  RET );}}  Catch (E) {alert (E. Message )}}  Function  Writecardno (){  VaR  Cardno  =  Txtcardno_write.value;  Try  {Objcard. cardno  = Cardno;  VaR  RET  =  Objcard. writecardno ();  If  (Ret  =   0  ) {Alert (  " Card written successfully!  "  );}  Else  {Alert (  "  Failed to write the card! The error code is:  "   +  RET );}}  Catch (E) {alert (E. Message )}}  </  Script  > 

 

2. How to determine whether ActiveX is registered when object tags are used

<SCRIPT type = "text/JavaScript">VaRObjcard = Document. getelementbyid ("cardaccessor");If(Objcard. Object =Null) {Alert ("The cardaccessor plug-in is not installed! ");}Else{Alert ("The cardaccessor plug-in has been detected! ");}</SCRIPT>

In addition, you can access a known property defined in ActiveX to determine whether the plug-in has been installed. If the returned property value is undefined, no plug-in is detected. For example:

<SCRIPT type = "text/JavaScript">VaRObjcard = Document. getelementbyid ("cardaccessor");If(Objcard. cardno =Undefinedl) {alert ("The cardaccessor plug-in is not installed! ");}Else{Alert ("The cardaccessor plug-in has been detected! ");}</SCRIPT>

 

3. How to enable IE to automatically download and install plug-ins and intelligently upgrade them

If no plug-in is installed, how can I enable IE to automatically download and install the plug-in from a specified location? It's easy to use the codebase attribute in the object tag:

    Object   id   =" cardaccessor "  classid   =" CLSID: 03ad53e8-d7e7-485d-a39a-d07b37defbc9 "   codebase    =" cardaccessor. cab # version = 1, 0, 1 "  width   =" 0 "  height   =" 0 " >    Object  > 

The codebase value format is "XXXXX. Cab # version = ". '#' The front part is the location of the cab file, which can be an absolute or relative location on the server. '#' Indicates the version number of the referenced cab package. When ie detects that the system has not registered the specified plug-in, it downloads the cab from the location specified by codebase to the local device and downloads the cab according to the code. the description of the INF file copies each file to a specified location and registers the specified control. (Note: signature issues are involved in actual applications, which will be described later)

Even if the plug-in has been registered locally, ie will compare the version of the registered control with the version number specified in codebase. if the version number specified in codebase is greater than the version number of the registered plug-in, IE will still download the cab package from the specified location of codebase and re-register the plug-in.

As mentioned in the previous article, for ease of management, the version number of the ocx file to be registered in the cab package is considered as the cab version number.

When the plug-in or plug-in dependent files need to be upgraded, you only need to update the corresponding files and. to facilitate management, the version of the corresponding file in INF is upgraded (no matter whether the ocx file is updated or not, the version number of the ocx file is also upgraded, because the version number represents the entire cab ), then, package it into a cab and release it to the server. Then, update the version of the codebase attribute value in the html object tag. When you access this page next time, ie will automatically download the upgraded cab and re-register the plug-in. In fact, after my tests, the cab package on the server does not change in time. As long as the version value in codebase is added, the corresponding plug-in will be re-downloaded and registered.

 

2. Use new activexobject in JavaScript to call ActiveX

If you do not use the object tag, you can directly use activexobject of js to create an ActiveX instance to call the plug-in Interface (this is the principle of XMLHttpRequest in IE ). For example:

 
VaRObjcard =NewActivexobject ("uprain. cardaccessorctrl.1 ");

If the plug-in has been registered, you can use objcard to call the plug-in interface and access attributes.

The parameters of the activexobject function are the progid of the corresponding plug-in, rather than the classid. In the xxxxctrl. cpp file of the project, you can also find or modify the progid value of the corresponding plug-in, for example:

That is, the second parameter of implement_olecreate_ex indicates the progid of the current plug-in. The value can be modified as needed. In fact, the corresponding classid can be found through progid in the registry, and the two are associated:

 

So how can we determine whether ActiveX has been installed? In fact, if ActiveX is not installed, creating a plug-in object using new activexobject will throw the "Automation server cannot create an object" exception. So use the following method:

 
Try{Objcard=NewActivexobject ("uprain. cardaccessorctrl.1");}Catch(E) {alert ("ActiveX call failed! ");}

 

However, I encountered two situations during the actual test:

1) if the exception "The Automation server cannot create objects" does not necessarily mean that the plug-in is not installed, or it may be because the plug-in has not implemented initialization or the Script Security interface, and thus is intercepted by IE, you need to adjust the settings of ActiveX controls and plug-ins in IE tools-options-security-Custom Level;

2) Sometimes, the registered plug-in can call the interface normally by referencing the object tag, but the new activexobject method fails to call the plug-in interface.

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.