OCX Package CAB with JS call detailed tutorial

Source: Internet
Author: User
Tags try catch

Recently in a WEB project, you need to call OCX to connect the card reader. Originally do not want to use OCX technology, because the ActiveX technology is produced by Microsoft, so that the entire system can only use IE browser (other browsers can be in the form of plug-ins to support OCX calls). But trying a lot of ways to invoke client DLL discovery is not going to make sense.    Finally helpless under, only then intends to study the OCX technology. Online all say that the ActiveX technology is very troublesome, there will be a variety of errors, do not know when the real need to study, only to find that this is true.    The main problem is that the error and the real cause of the issue is not related or very small, and even many times there is no error, only keep trying.    Anyway, here's how to fix the OCX. First of all, this tutorial only about the OCX package CAB and JS call OCX, here does not discuss how to write OCX through C + +.    Because I only do Java development, for C + + does not study. development system: win8.1 64-bit test via system: Win7 64-bit, win8.1 64-bit, XP 32-bit

the relationship between OCX and CAB. You know, after getting the OCX, if you want to call, first to register.    In the case of not registering, there is no way to call OCX. The simplest way to register is to register manually. Manual registration requires a command-line operation. It may not be a problem for a developer to deal with the command line more or less, but for a customer who uses our system, it is really inappropriate for him to do command-line operations. No matter how detailed our documentation is, it's unfriendly to customers, and manual registration is designed to be a security issue. Therefore, it is necessary to automatically register the OCX without the customer's knowledge. This is the role of the CAB.

    Manually registering OCXwe assume that the directory where OCX is located is: D:/ocx/xpbutton/xpbutton.ocx

Through Regsvr32 xpbutton.ocx, so we manually registered the OCX. If you want to uninstall OCX, we can anti-registration: regsvr32/u Xpbutton.ocx. To register and uninstall, we all need to run the CMD console as an administrator. --Special attention is needed here! If the following error occurs during registration:
1. First see if you are running CMD as an administrator. 2. If this is not possible, consider putting the appropriate OCX into the system directory (a). 32-bit system to C:\Windows\System32 B). 64-bit system to C:\Windows\SysWOW64 3. If you still get an error, you can be sure that the DLL is missing. You can download a tool:Dependency Walker.     Look at the tutorials here, see which DLLs are missing, go online to download these DLLs,These DLLs are what you have to pack into the CAB package with OCX. Because your own computer is less, it is also possible that the client computer will be less these DLLs. Note here is: the preferred is the same as your system of the number of DLLs, certainly not wrong, really can not find 64-bit, only consider the 32-bit version.      After downloading the DLL, store the DLL under the System folder mentioned in the second step above, and then register again.

OCX Package CAB First download the OCX Packaging signature tool:OCX package Signature tool , access password DDB4.



Put the OCX and commands that need to be signed and packaged in the same folder.

To create a signing certificate:Run the following command at the command line: 1. Execute command:
Makecert.exe-ss Xpbutton-n "cn= here casually"-sv. \xpbutton.pvk-r. \xpbutton.cer



Enter three times, password, check the console appears succeeded indicates success.


The makefile is now: Xpbutton.cert and xpbutton.pvk


2. Run the command:
Cert2Spc.exe. \xpbutton.cer. \xpbutton.spc

Viewing the console, a succeeded appears to indicate success.

The file is generated at this time: XPBUTTON.SPC


Two orders ended, we got three files: Xpbutton.cer, xpbutton.pvk, XPBUTTON.SPC

to sign the OCX before you package the CAB, you first need to sign the OCX itself, which is important, and if you do not, you may make a mistake. 3. Run command: SignTool signwizard




































If you want to fill in the timestamp, you can fill in the following address: Http://timestamp.verisign.com/scripts/timstamp.dll




Wait a moment, the following prompt appears, indicating that the OCX signature is complete.


The console appears: Successfully completed signing wizard:<> indicates success.


writing an INF file INF files are also a priority, and if you write errors, they are not packaged correctly. ( You can download the INF file I wrote, on this basis, to make changes to ensure the correctness of the INF file ) INF Reference file: OCX INF file,Access password 49de. If the package CAB is not a problem, OCX, DLL will be downloaded to the c:/windows/ocx/directory, convenient for you to uninstall OCX, delete dll files.

[Version]
Signature= "$CHICAGO $"
advancedinf=2.0

[DefaultInstall]
Copyfiles=files
Registerocxs=registerfiles

[DefaultUninstall]
Cleanup=1
Delfiles=files
Unregisterocxs=registerfiles

[SourceDisksNames]
1 =%diskname%, "Xpbutton.cab", 1

[SourceDisksFiles]
Xpbutton.ocx=1
Msvcrtd.dll=1
Mfc42d.dll=1
mfco42d.dll=1

[Registerfiles]
%30%\windows\ocx\xpbutton.ocx

[DestinationDirs]
Files=30, Windows\ocx

[Files]
Xpbutton.ocx=xpbutton.ocx
Msvcrtd.dll=msvcrtd.dll
Mfc42d.dll=mfc42d.dll
Mfco42d.dll=mfco42d.dll

[Xpbutton.ocx]
File=thiscab
CLSID={134EE1CC-4B8A-4E74-8C41-F4990065E2E1}
fileversion=1,0,0,1
Registerserver=yes

[Msvcrtd.dll]
File=thiscab
fileversion=6.0.8337.0

[Mfc42d.dll]
File=thiscab
fileversion=6.0.8168.0

[Mcfo42d.dll]
File=thiscab
fileversion=6.0.8267.0
[Strings]
Diskname= "Windows\ocx"
The above is my INF file.           Explain several parts. 1. This adds 3 DLLs, if you do not need to package the DLL to the cab, you can refer to the blue section above. If there is no mcfo42d.dll then all the blue parts are removed and the rest is not moved. And so on
2.

for a simple explanation:File=thiscab copy copy, do not explain.              Because it is a 64-bit system, I test this write no problem. 32-bit systems can also be written like this: File-win32-x86=thiscab
CLSID here, the simplest way is to find the source of the OCX, ask the production of the OCX authors, they know what should be filled out here. If you can't find a producer, there are ways to manually register OCX with the previous article, and then view the search registry: Xpbutton
Find a similar registry structure on the left, and then 134EE1CC-4B8A-4E74-8C41-F4990065E2E1 will be the CLSID we need.Note: I registered the Xpbutton.ocx file in the C:\Windows\ocx\ directory. So the address on the right is C:\Windows\ocx\xpbutton.ocx .FileVersion is the same, the best way to find the source of OCX (developer), determine the version number, when writing OCX, the code will have an OCX corresponding version number. Originally and C communication, read the OCX C + + code, there are corresponding version information. The version information here must be consistent with the OCX version information.           Registerserver=yes means to automatically register this OCX after downloading.          You can see me in the following DLL file code does not have this sentence, meaning that the DLL is not required to register after downloading, if you need to register the DLL, the corresponding DLL region also need to add this statement. 3. The green part of the INF file is interpreted as the green section, which represents the version number of the DLL and how to determine the DLL version number.          It's actually very simple. Find the corresponding DLL right click, view properties, here the file version, is the version number we need,Please note that this is not a product version。
OCX Package CAB file           4. Run the command:
Cabarc. Exe-s 6144 n xpbutton.cab xpbutton.ocx xpbutton.inf
Need to explain this command: is to package the Xpbutton.ocx Xpbutton.inf file into a xpbutton.cab file, if we need to pack additional DLLs into the cab, then write: Cabarc.exe-s 6144 n Xpbutto N.cab xpbutton.ocx Msvcrtd.dll Xpbutton.inf and so on.
So far, we've packed the OCX into cabs, but not yet. A lot of tutorials to end, in fact, OCX's cumbersome far from over.

JS call OCX (CAB) Introducing the OCX control To create a new HTML file, we introduce the OCX in the following way:
An explanation: id= "Xpbutton" means that this object has an ID of Xpbutton, which can be used as a follow-up name. Classid= "Clsid:134ee1cc-4b8a-4e74-8c41-f4990065e2e1" see this, should be very familiar. Classid= "CLSID: This part copy, do not change. Just change the colon: The next part is OK. (I accidentally put the CLSID: This part of the missing, written as a classid= "134ee1cc-4b8a-4e74-8c41-f4990065e2e1", the result CAB file download not down).Note: Don't miss the CLSID.          Codebase= "./xpbutton.cab#version=1,0,0,1" This part of the code is to tell the browser if the CLSID is not found for the 134ee1cc-4b8a-4e74-8c41-f4990065e2e1 registry, That is, the system does not register this xpbutton.ocx, then go to find the corresponding Xpbutton.cab file. Here "./xpbutton.cab" means the Xpbutton.cab file that is stored in the same directory as the current HTML. "./" indicates the current path, which is the path where the HTML resides. The following "#version =1,0,0,1" indicates the current OCX version number, which is the OCX version number (FileVersion) written in the INF file in the CAB compression package.Note Version number: 1,0,0,1 is a comma "," delimited, not a dot ".", if you use the dot ".", then congratulations, you're wrong again. Additional Note: We have encountered three version number 1.                              When OCX is written, the C + + code specifies the version number of the OCX.                              2. When OCX packages a CAB file, the INF file specifies the version number of the current OCX. 3. When the HTML calls OCX, the CODEBASE table shows the OCX version number that needs to be called.     These three version numbers need to be consistent. Very interesting is, if you want to upgrade OCX, is actually very simple, let OCX writers upgrade OCX, and then we upgrade the version number in the INF file, and then the corresponding HTML version number also upgraded.          When the browser calls this HTML, if the version number in CODEBASE is found to be upgraded, the CAB file is automatically re-downloaded and re-registered. Before, in order to test whether the OCX upgrade itself has a problem, manually register this OCX, did not repackage the cab, the results of each access is found to be registered in the cab of the previous version of OCX. Description The browser will re-download the cab and re-register as soon as it finds that the registered OCX version and the CAB version are inconsistent. It is also important to note that after the OCX has been written. The CLSID is fixed. This OCX no matter which computer to register, look at the registry, the CLSID is the same, will not change. This is why we can write the CLSID directly in HTML. Since the client downloads the CAB, auto-registration, the OCX CLSID is the CLSID that we wrote in C + +.

JS Call OCX method
Explain the above code 1.  Xpbutton.                    AboutBox ();          Xpbutton actually does not appear out of thin air, here the Xpbutton is the ID of <object> tag, we see above can see, <object id= "Xpbutton" >......</object>. 2. Xpbutton. AboutBox () ; AboutBox () is actually one of the methods in OCX. If you want to know which methods in this OCX, the preferred is to find the OCX developer, in the case can not be found, through the Tstcon32 software, you may download here: Tstcon32 ActiveX container access password 904d. If not, according to the error prompts to download the appropriate DLL. (Study OCX left Sequela, always feel that some software will be missing DLL) 3. Why try catch OCX is cumbersome and confusing, it is important that even if the call fails it will not error. So, we have to manually eject the error message here in the try catch. But the problem is not so simple, even if there is an error message, the hint also let a person can not touch the mind.Note that you must remember try catch, or no error will be prompted. The following summarizes I encountered the error message and the true reason of the corresponding relationship, in order to prevent you all kinds of Baidu, Google finally found the wrong solution. OCX error, in general, are the local test passed, deployed to the server or use someone else's computer, found that the call failed.
1. [object ERROR], error: Member not found

If you encounter the above error: [Object ERROR], error: Cannot find member 1. First determine C:\Windows\ocx directory to download the OCX file (if you have downloaded the INF file in this tutorial, thenC:\Windows\ocx directory down), if there is no file in the directory or the directory, then against the above, to see if the INF file is writing a problem, or HTML introduced OCX object has an error, resulting in a download failure. 2. If the OCX file has been downloaded, it means that although the CAB file is not a problem, OCX is not registered or failed to register. The reason for the registration failure, we first need to confirm whether the DLL is missing. How to confirm that you can use the above mentionedDependency WalkerSoftware. If you find that the DLL is really missing, then we should repackage the cab and package the missing DLL files together in the Cab. Many times, we automatically register the OCX through the CAB, in most cases will not report any errors. Even because the missing DLL is not registered successfully, there is no hint. I test on a lot of computer results, there is only one Win7, there is a lack of DLL error shows, all other computers do not have any errors.In particular, it is important to note that after you have packaged a CAB file, test it with multiple computers, preferably test different systems, and try to pick a company's non-developer's computer. Do not test on your own computer, or some of the OCX developers on the computer after the test passed and think it is not a problem.
                     2. Object does not support this property or methodThis error, in the process of development, is bound to encounter. At first, it was thought that there was a problem with the calling method, which was found after trying all the other different calling methods that could be found on the Internet. If the correctness of the OCX can be guaranteed, the root cause of this problem is because the browser does not trust the OCX, to intercept the OCX inside the method called. the fundamental solution to this problem is to modify the browser settings.

     3. Modify browser security Settings                     Online can find a lot of browser modification points, in fact, most do not need to modify, modify the instead of reducing security, increase the risk.


Uncheck " require server Authentication (HTTPS) for all sites in this zone", fill in the server address, for example: http://192.168.0.61 and then add, after adding , tick "on the zone Server Authentication (HTTPS) is required for all sites in the This step is done to ensure that the browser trusts this site.





Here to modify the security level of the trust site, the figure on the two, from the original prohibition, modified to prompt or enable.in fact, before the first visit to the CAB Web page, we should first modify the browser security policy, modify the completion of the visit, there is generally no problem.

At this point, the relevant knowledge of OCX has been fully introduced, this tutorial should be able to help you take a few detours. This is the result of the last two weeks of research.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OCX Package CAB with JS call detailed tutorial

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.