[C #]-Develop ActiveX controls and create cab packages

Source: Internet
Author: User

Source: http://www.fenbi360.net/content.aspx? Id = 1113 & t = CT

 

ActiveX controls are used in a project. by referring to the numerous examples on the internet, ActiveX controls are easily implemented;
Then I made a [installation project], which can be used, but I only need to manually click Install On the client. Next, next... I think it is not very nice;
I tried the [cab project] Again. It seems that no one uses vs2008. After a little effort, I finally found a solution.
We will share this experience with you.

Development tools: Visual Studio 2008
. Net: 2.0
Development language: C #

I. Compile ActiveX Controls
1. Create a Windows Forms control library project]

 

2. Add a [user control]

 

3. Drag two label controls to the interface

 

4. Add a method for the control to display the current time in the label.

  ///     <Summary> 
/// Set current time
/// </Summary>
/// <Param name = "timestr"> </param>
Public Void Settime ( String Timestr)
{
This . Label2.text = Timestr;
}

 

 

5. Create an interface:
It is said that the purpose of this interface is to improveProgramSo that the client Ie can pre-execute the ActiveX control without changing the settings.
About this articleArticle, We recommend that you look:

Http://blog.csdn.net/optman/archive/2007/07/18/1698070.aspx

  Using  System; 
Using System. Collections. Generic;
Using System. text;
Using System. runtime. interopservices;

Namespace Hellobossma
{
[Comimport, guidattribute ( " CB5BDC81-93C1-11CF-8F20-00805F2CD064 " )]
[Interfacetypeattribute (cominterfacetype. interfaceisiunknown)]
Public Interface IObjectSafety
{
[Preservesig]
Int Getinterfacesafetyoptions ( Ref Guid riid, [financialas (unmanagedtype. U4)] Ref Int Pdwsupportedoptions, [financialas (unmanagedtype. U4)] Ref Int Pdwenabledoptions );

[Preservesig ()]
Int Setinterfacesafetyoptions ( Ref Guid riid, [financialas (unmanagedtype. U4)] Int Dwoptionsetmask, [financialas (unmanagedtype. U4)] Int Dwenabledoptions );
}
}

     

    6. Let our controls implement this interface.
    For a long time to implement this interface, you can find it in this article:

    Http://www.pinvoke.net/default.aspx/Interfaces/IObjectSafety.html

     

    7. Add the guid, progid, and comvisible attributes for our controls.

     Namespace  Hellobossma
    {
    [GUID ( " 636b8ce1-7512-464c-b63c-fc75bdca21db " ), Progid ( " Hellobossma. hellobossmaactivex " ), Comvisible ( True )]
    Public Partial Class Hellobossmaactivex: usercontrol, IObjectSafety
    {
    ...
    }
    }

       

      8. Open the project properties, and check [Register for com interoperability] In the generate process]

       

      9. Generate a project and go to the bin \ release directory.
      Hellobossma. dll
      Hellobossma. TLB
      These two files are required; otherwise, they are not generated successfully.

       

       

      **************************************** **************************************** **********

      2. Create an ActiveX setup Installation File
      1. Create an installation project

       

      2. Right-click the project, choose add> project output, and select the project above.

       

      3. Open the properties page of the installation project and set the installation URL.
      Set http: // localhost/hellobossma
      This directory must actually exist to store the generated installation files for the client to download and install.

       

      4. Generate a project
      Two files, one EXE file and one MSI file are generated.

       

      5. Create a website and add the followingCodeTo the web page.
      Classid is the guid of the control.

        <ObjectClassid= "CLSID: 636b8ce1-7512-464c-b63c-fc75bdca21db"Codebase= "Hellobossma/setup.exe # version ="Width= "200"Height= "40"ID= "Hellobossma">
      </Object>

       

       

        6. Copy the EXE and MSI files generated by the installation project to the hellobossma directory of the website.
        Hellobossmasetup. MSI
        Setup.exe

         

        7. Start the website and ensure that the directory of setup.exe is consistent with the installation URL in the installation project.

         

        8. In general, a prompt is displayed on the webpage indicating whether to install ActiveX controls. If prompted, the page cannot be installed. You need to add the website to the trusted site, and allows downloading insecure and unsigned controls. This article does not describe the signature and authentication of controls. You can search for them if you need them.

        This installation method will pop up the software installation interface on the client, which is not friendly to the customer.

         

         

        **************************************** **************************************** **********

        3. Create an ActiveX cab installation package
        I tried the [cab project] template in Vs, but unfortunately, the control could not be installed and gave up.
        On the Internet, I saw a lot of people using cabarc. EXE. After trying it out, the process was successfully recorded.

         

        1. Download this package from Microsoft:
        Http://download.microsoft.com/download/platformsdk/cab/2.0/w98nt42kmexp/en-us/cabsdk.exe
        Decompress this file and provide detailed instructions for use.

         

        2. [start]-[run], enter "cmd", and enter the bin directory of the decompressed file in the command prompt interface that is opened,

        Two files need to be added here:
        Hellobossmasetup. MSI
        Install. inf

        Hellobossmasetup. MSI is the installation program generated by the above installation project.
        Install. inf is a text file that specifies the files to be downloaded or submitted to run the control.

        I will not introduce the INF file writing specifications here. You can search for them if necessary.
        In this example, the content of the install. inf file

          [  Version  ]  
        Signature = " $ Chicago $ "
        Advancedinf = 2.0

        [ Setup hooks ]
        Hook1 = Hook1

        [ Hook1 ]
        Run = Msiexec.exe/I " % Extract_dir % \ hellobossmasetup. MSI " /Qn

         

        Run the following command to generate the hellobossma. cab file in the current directory.

         

        3. Place the file in the control download directory and modify the calling method on the webpage.

          <  Object  Classid  = "CLSID: 636b8ce1-7512-464c-b63c-fc75bdca21db"  Codebase  = "Hellobossma/hellobossma. Cab"  Width = "442"  Height  = "49"  ID  = "Hellobossma"  >  
        </ Object >
        < Input Type = "Button" Value = "Click" Onclick = "Hellobossma. settime (new date (). gettime ())" />

         

        A button is also added here to trigger an event when you click it, call the display time method of the control, and click the button, you will find that the time on the control changes.

        OK. The text here is over.
        I just want to know how to use C # To develop ActiveX controls.

        Related Article

        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.