Using VS2010 C # to develop an ActiveX control (top) _c# tutorial

Source: Internet
Author: User
Tags guid generator
to developWebVersion of the card reader program, the general idea is as follows:

1. use C # to encapsulate the original DLL , here to encapsulate two parts of the content, part of the function of serial communication, part of the card read and write function.

2. Develop ActiveX controls to invoke encapsulated DLLs, and use serial communication to read and write cards.

3. Package and publish ActiveX controls.

4. Use ActiveX controls.

Idea 1 Package code has 2 class Serialinterfacehelper, serial communication Help class, Mifareonehelper,M1 card Read-write Help class, we put in the project in Cardreader.library.

Since the focus of this article is to use VS(C #) for the development of ActiveX controls, the contents of the idea 1 are not detailed, and will be given directly to the class library for reference. The example in this article demonstrates the C # Development

An ActiveX Reader control that implements the ability to read card numbers and display card numbers or exception information, and is divided into three major steps: Developing ActiveX controls, packaging and publishing ActiveX controls, and using

ActiveX Control.

Developing ActiveX Controls
Some of the most common ActiveX is the use of VB, Delphi, C + + development, using C # to develop ActiveX to solve the following three problems:
(1) make. NET component can be called by com
(3) When registered on the client, the installation package can be authenticated by IE's signature
To develop ActiveX steps:
1. to create the Windows Forms Control Library project Cardreader.controls, set project properties to be callable by COM. 
Right-click Cardreader.controls, select Properties, and set assembly properties for the project, as shown in Figure 1 below:
  
     
  
Tick the Make Assembly com-visible option.
Set the compilation options for the project, as shown in Figure 2:
Figure 2
The Register for COM interop is checked in Figure 2 for registering the COM component. (Note that if you modify it here in Debug state, you will need to set it again in the release state.) )

Set the application's AssemblyInfo property, right-click the item's Properties, open the AssemblyInfo file, Assembly: AllowPartiallyTrustedCallers, Note Add Reference system.security, the code is as follows:

Copy Code code as follows:

Using System.Reflection;
Using System.Runtime.CompilerServices;
Using System.Runtime.InteropServices;
Using System.Security;
General information about a assembly is controlled through the following
Set of attributes. Change this attribute values to modify the information
associated with a assembly.
[Assembly:assemblytitle ("Cardreader.controls")]
[Assembly:assemblydescription ("")]
[Assembly:assemblyconfiguration ("")]
[Assembly:assemblycompany ("Microsoft")]
[Assembly:assemblyproduct ("Cardreader.controls")]
[Assembly:assemblycopyright ("Copyright©microsoft 2011")]
[Assembly:assemblytrademark ("")]
[Assembly:assemblyculture ("")]

[Assembly:allowpartiallytrustedcallers ()]
Setting ComVisible to False makes "types in" assembly not visible
to COM components. If you are need to access a type of assembly from
COM, set the ComVisible to true on that type.
[Assembly:comvisible (True)]

The following GUID is for the ID of the typelib if this project was exposed to COM
[Assembly:guid ("15493d85-ec9e-4c75-a237-9009a997b780")]

Version information for a assembly consists of the following four values:
//
Major Version
Minor Version
Build number
Revision
//
Can specify all the values or your can default the build and Revision Numbers
By using the ' * ' as shown below:
[Assembly:assemblyversion ("1.0.*")]
[Assembly:assemblyversion ("1.0.0.0")]
[Assembly:assemblyfileversion ("1.0.0.0")]

2. develop the reader user control, which contains three parts:
A textbox to display the card number read out
A button, read the card
A label that displays an error message
To write the code for the Read-card button event, after the control is developed, the following modifications are required to make the user control work as an ActiveX control:

First, create a unique GUID for the control class, which will be used in the client invocation of the B/s system, noting that the GUID here cannot be the same as the GUID in Assemblyinf, and the method for generating the GUID is as follows.

Open Microsoft Windows SDK Tools in the Start-program, as shown in Figure 3 below:

Figure 3

Click on the GUID generator to generate the GUID, as shown in Figure 4 below:

Figure 4

Copy generates the GUID to Notepad, and then copies the GUID's string to the control class, as shown in the following code:[Guid ("e395359c-86f2-4d7b-a91a-5a64b9e3ba6c")]
Public partial class Readcardcontrol: UserControl

Second, in order for the ActiveX control to gain trust from the client, the control class also needs to implement an interface named "IObjectSafety" (Note that the GUID value of the interface cannot be modified).
The IObjectSafety code is as follows:

[ComImport,Guid("1d9ad540-f2c9-4368-8697-c4aafcce9c55")]
[InterfaceType(ComInterfaceType. Interfaceisiunknown)]
PublicInterfaceIObjectSafety
{
[ PreserveSig]
         void getinterfaccesafyoptions (
          int riid,
         out  int pdwsupportedoptions,
         out  int pdwenabledoptions);

        [ PreserveSig]
          void setinterfacesafetyoptions (
          int riid,
         Int dwoptionssetmask,
         int dwenabledoptions);
    

Note here to add a reference:using System.Runtime.InteropServices; 
3. modify the control class so that it inherits the IObjectSafety interface, and the code list is as follows:

[Guid("E395359C-86F2-4D7B-A91A-5A64B9E3BA6C")]
PublicPartialClassReadcardcontrol:UserControl,IObjectSafety
{
PublicIntIcdev;//Communication device Identifier
PublicInt16St
PublicIntSec

PublicReadcardcontrol ()
{
InitializeComponent ();
}
#regionIObjectSafetyMembers

PublicvoidGetinterfaccesafyoptions (IntriidOutIntPdwsupportedoptions, out  int pdwenabledoptions)
        {
            pdwSupportedOptions = 1;
            pdwEnabledOptions = 2;
        }

         public  void  SetInterfaceSafetyOptions ( int riid,  int dwoptionssetmask,  int  dwenabledoptions)
        {
              throw  new  notimplementedexception ();
       &NBSP}

          #endregion
}

Packaging and Publishing ActiveX

after ActiveX control development is complete, we'll talk about ActiveX control packaging and publishing. ActiveX controls can be deployed using the VS + installation project, and the installation works for creating Windows Form with VS can package ActiveX DLLs . When packaging, note that the ActiveX control project is the primary output project and set its register property to vsdrpCOM, creating a packaged project as shown in Figure 5:

 
 
Figure 5
Create a Windows Setup project and add project output to the project as shown in Figure 6 below:
 
 
Figure 6
As we add the project output, we add the ActiveX project, select the ActiveX control Project (Cardreader.controls) in the project, Primary out (basic output), as shown in Figure 7 below:
 
 
Figure 7

After adding project output, there are already three files in application Folder: CardReader.Controls.tlb, CardReader.Libary.dll, Primary output From Cardreader.controls, Mwrf32.dll also into the installation package, right click to add files, browse to Mwrf32.dll add in. Note to first copy the Mwrf32.dll to the ActiveX control

In the bin in the project , browse to the Mwrf32.dll in the \\CardReader\CardReader.Controls\bin\Debug when you add the file , Otherwise, there will be no mwrf32.dll errors found.

After the file is added, set the Register property for the primary Output from Cardreader.controls to vsdrpcom. Right-click to Install engineering Setupcardreader when Setup completes,

Modify its properties as shown in Figure 8 below :

 
 
Figure 8

In the figure above, you can set the file name of the output, which is the file name after the package installation. the file name of the MSI. Set package file, compression mode, CAB size, all three select default values.

Finally, set the installation URL, where the installation URL is used to publish or test the URL address of the ActiveX. In the image above we will test under the I port, so the URL is set to:

Http://localhost:89/CardReader

So the package file is generated, we can open \\CardReader\SetupCardReader\Debug See generated 2 files, one is Setup.exe, One is Setupcardreader.msi,

The Setup.exe here is the codebase file we used when we were using ActiveX .

After packaging into an EXE file, we can further package the installation file into a. cab file, install the MSI install hidden interface, similar to the effect of cabarc packing OCX

(Click Install after the others have done backstage), this article is not discussed, interested readers can use the CAB SDK tools CABARC.EXE ( download Address http://support.microsoft.com/ kb/310618) to carry out.

Using ActiveX
Once the package is complete, we can use the packaged ActiveX control in the application, create a Web project (cardreader.web), add the ActiveX control to the HTML code of the test page
Reference, Modify the Default.aspx code as follows:

<object id= "cardReader1" classid= "clsid:e395359c-86f2-4d7b-a91a-5a64b9e3ba6c"
width= "500"
height= "100"
codebase= "Cardreader/setupcardreader/debug/setup.exe" >
</Object>

Note that the value of the clsid:e395359c-86f2-4d7b-a91a-5a64b9e3ba6c here is the GUID we were developing when we developed  the ActiveX control . 
The effect diagram that runs is shown in Figure 9 : 
  
     
  
Figure 9

in Figure 9 , we demonstrate the effect of not calling the serial port communication and reading card program, as for calling the serial port communication and the card reader program, I will explain in another post in detail.

At this point, most of the functionality to develop ActiveX controls with VS is complete, and it is not difficult to develop ActiveX controls using C # in the VS environment, but requires clients to install. NET Framework. Since ActiveX controls are generally a simple single feature, it is recommended that you use the. NET Framework 2.0/.net Framework 4.0 development,

In this example we use the. NET Framework 4.0.

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.