Develop a simple ActiveX Control Based on MFC following the steps in browser plug-in ActiveX Development (I. However, some problems still occur in actual operations. I have little knowledge about COM programming, and I have not found a good solution to some problems.
1. ActiveX problems that need to be referenced by other DLL
Our ActiveX needs to read and write the IC card device, so we need to call the interface provided by the device. The device manufacturer provides interface files such as "mwhrf_bj.lib", "mwhrf_bj.dll", and "mwrf32.h. Add "mwhrf_bj.lib" and "mwrf32.h" to the project. ActiveX interface methods can call methods in the interface file. However, during compilation, "Project: Error prj0050: failed to register the output. Please try to enable "redirection for each user", or register this component from the command prompt window with Elevation of Privilege "or" Project: Error prj0050: failed to register output. please ensure you have the appropriate permissions to modify the Registry "error.
In fact, this error does not appear in the editing stage, but occurs when the compiled ocx file is registered. By default, vs.net 2008 automatically registers the compiled ocx file after compilation. Right-click the project name and select "properties". In the displayed dialog box "deployments properties-> linker-> General ",Register outputYou can configure whether to automatically register OCX after compilation, as shown in:
An error occurred while registering OCX because the called "mwhrf_bj.dll" file was not found during registration. Put the called "mwhrf_bj.dll" file in the same directory of the ocx file or other % PATH % paths (such as the Windows folder or system32 folder), no error will be reported when you register OCX. In the vs.net development environment, copy the external DLL file to be called to the debug or release directory.PrebuildIn the script, copy the external DLL file to the compilation target folder, for example:
Note:See "http://www.cnblogs.com/? AB /archive/2012/07/16/2593604.html"Article.
Ii. ActiveX debugging method
You can debug ActiveX in vs.net 2008 as follows:
1、prepare the demo.html file and write the test.ProgramYou must use <object/> to reference the OCX control to be tested on the page (for details about how to call the control on the HTML page, we will discuss it later ).
2. In vs.net 2008, right-click the project name and select properties. In the displayed dialog boxDebuggingConfigured on the configuration pageCommandAndCommandargsParameters:
Command: The path of the local IE browser, such as"C: \ Program Files \ Internet Explorer \ iw.e. exe"
Command ARGs: The created HTML file used for testing ocx(the demo.html file path mentioned above)
3. Set breakpoints where debugging is required in the program. After pressing F5, vs.net will automatically start IE and open the corresponding HTML test file. The breakpoint will interrupt the running and enter the debugging status.
Iii. ActiveX interface Implementation of out/Ref parameters and returned user-defined struct data
Sometimes the ActiveX interface method returns only one data and cannot meet our actual requirements. For example, if you use the getpersons () method of ActiveX to return a bunch of personnel information, it must be a list or array, and each person also contains various information such as name and gender, at this time, the return value is quite complicated.
For the sake of simplicity, we have used ActiveX as an example to read the card number. Generally, this plug-in can meet the following requirements as long as it provides the following interfaces:
BSTR readcardno ();
In this way, the ActiveX readcardno () method can be called in JavaScript to return a string containing the card number.
However, how can I identify exceptions during card reading by providing this interface? If the card reading operation is normal, returning a card number string is certainly not a problem. However, if a card reading device is incorrectly connected or the card cannot be identified during the process, what if the exception information is fed back to the caller?
1. The first thing I think of is to use the ref or out parameter to solve the problem. The corresponding parameter modifier in C ++ is such as out/retval.
The interface defined in. IDL is:
[ID (1), helpstring ("method readcardno")] Long getsheetname ([Out] BSTR * cardno);
The corresponding interface prototype is:
Long readcardno (BSTR * cardno );
In this way, the return status is identified by the return value of the long type. For example, the following conventions can be applied:
0-card reading successful
1-The card reading device is not connected
2-unrecognized card not found
......
If the returned value is 0, the card reading is successful. The card number is passed to the caller through the cardno parameter of the out type.
However, scripting languages such as JavaScript do not support parameters of the out/Ref type, and function parameters cannot be transferred. Therefore, this solution cannot solve my actual problems.
2. If the ActiveX interface can return a custom struct type data, it can meet our needs. For example, we define a struct:
Typedef struct
{
Long resultstatus, // return status 0-card reading successful 1-card reading device not connected 3-unrecognized card found
BSTR cardno // when the card reading succeeds, save the read card number
} Aopresult;
If the corresponding interface can be implemented as follows, we can solve our problem:
Aopresult readcardno ();
However, custom data types cannot be directly used in the interface definition of MFC ActiveX.VariantType. The following references describe this:
A) http://bbs.csdn.net/topics/320146859
B) http://bbs.csdn.net/topics/20064135
C) http://www.codeproject.com/Articles/916/Using-User-Defined-Types-in-COM-ATL
D) Standard MFC Winsock ActiveX Control Development Instance (ii) Advanced
However, it is not that easy to implement. In view of the time issue and the imurgency of our actual needs, I have not tried this too much. If you have a molding instance, please kindly advise.
3. Since ActiveX interfaces are generally called by JS in Web application scenarios, we can return a JSON type of data, such as "{status: 0, cardno: 234234344634 }". In this way, the ActiveX interface only needs to return a BSTR parameter, but the meaning of the returned value has changed, not a simple card number, the readcardno interface that requires ActiveX needs to encapsulate the return value into a JSON string for internal processing and return it to the caller for resolution. However, special characters such as {,}, and: must be processed when the JSON string is encapsulated.
4. For simple application scenarios, we can also use ActiveX attributes to resolve such problems. For example, we define a property cardno in ActiveX. In this case, the provided interface simply returns a status:
Long readcardno ()
The Return Value of the interface still indicates the status. For example, 0 indicates that the card reading is successful, and 1 indicates that no card reading device is found. When 0 is returned, the card reading succeeds. The corresponding card number can be obtained from the cardno attribute.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
References:
ActiveX Controls (MFC)