• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

DownloadingC#ActiveXcomponentsthroughCABfile

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
转:http://www.codeproject.com/useritems/C__Deployment.asp
.

Introduction

This article describes how we solve the problem of registering the c# component after downloading it through CAB file and invoke it through script. Also gives the focus on how to update the latest version of component on Client machine when the component is getting installed through INF file execution.

Background

In case of client side components created in C++, COM and ATL, the procedure is well defined and convinient as we just need to create the ActiveX component which exposes the Interface and create the CAB file including the INF file which describes whether to register it and where to copy it. This is not the case with ActiveX components created in c# as the Registerserver=Yes in INF file simply calls the regsvr32 on given component, which is not useful to register te c# components.

Using the code

Here the main focus is on how to create the CAB file and deploy it on server, for that create the C# application which exposes the COM interface, folowing code demonstrates the simple C# component exposes the COM interface.

Collapse
using System;
            using System.Text;
            using System.Windows.Forms;
            using System.Collections.Generic;
            using System.Runtime.InteropServices;
            namespace DownloadedApp
            {
            [Guid("D1E40FCE-E964-4dfd-B07E-BDE49D3519A1")]
            interface IDownload
            {
            void InvokeMethod();
            }
            /// 
            /// COM exposed .Net class.
            ///
            [ClassInterface(ClassInterfaceType.AutoDual)]
            [Guid("A47C22B1-3CC3-45bc-801E-3FCC4FFD3E45")]
            public class Download : IDownload
            {
            ///
            /// COM exposed .Net Method.
            ///
            public void InvokeMethod()
            {
            MessageBox.Show("Clientside component invoked sucessfully!!", "Clientside component", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            }
            }
            

Now create the MSI Setup project in VS 2005 and give the output of this COM application to that project.

In the properties of Added output file in Setup project, make sure that the Register property is set to vsdrpCOM. Seting this property insures the MSI file to register the ActiveX component using regasm tool in DotNet. While installation, the installer itself takes care of registering this C# component to expose the COM interface.

Creating the CAB file


Using microsoft InetSDK CABARC tool, we can create the CAB file including this DownloadDeployer.msi and INF file. This tool is freely available on microsoft downloads website. For better execution and CAB download, it is good to sign the CAB file, we can get the code signing for our Component from microsoft site. It is beyond the scope of this atricle.

We also have another option to create the CAB file using VS 2005 Cab File Project which automatically includes the OSD file instead of INF file but to modify the OSD file is not the easy task as Visual Studio does not support extensiove tools to modify it. Below is the sample of INF file which will be helpful to execute the MSI installer and to install the component on Client machine.

[version]
            signature="$CHICAGO$"
            AdvancedINF=2.0
            [Setup Hooks]
            hook1=hook1
            [hook1]
            run=msiexec.exe /i "%EXTRACT_DIR%\DownloadDeployer.msi" /qn

The script will contain code to invoke the ActiveX component as the registration will already be done by MSI installer there will not be any problem while invoking the component through script. The script will be as mentioned below.

<html>
            <head>
            <script LANGUAGE="JavaScript1.2">
            function Scrape( )
            {
            var objDownload = new ActiveXObject( "DownloadedApp.Download" );
            try
            {
            objDownload.InvokeMethod( );
            }
            catch(exception) {
            alert( "Failed" );
            }
            }
            </script>
            </head>
            <body>
            <table align="center">
            <td>
            <a href="javascript:Scrape( )">Invoke Component</a>
            </td>
            </table>
            <p>Please Wait...Downloading components...</p>
            <object name="secondobj" style='display:none' id='TestActivex'  classid='CLSID:A47C22B1-3CC3-45bc-801E-3FCC4FFD3E45' codebase='DownloadDeployer.cab#version=1,0,0,0'></object>
            </body>
            </html>
            

Here the Version is the version of MSI file which we can modify by opening the vdproj file in notepad, search for Product Version and change the version for any new updates.as given below the view of vdproj file when opened in notepad.

Collapse
"ProductCode" = "8:{82CC6087-5430-4343-9F8B-207A889F92A1}"
            "PackageCode" = "8:{9D5DFC79-5E0F-4A6E-8796-10F08758FD2E}"
            "UpgradeCode" = "8:{21D008FF-E2BB-4C7B-A985-B20B9CB0C2D8}"
            "RestartWWWService" = "11:FALSE"
            "RemovePreviousVersions" = "11:TRUE"
            "DetectNewerInstalledVersion" = "11:TRUE"
            "InstallAllUsers" = "11:FALSE"
            "ProductVersion" = "8:1.0.0"
            "Manufacturer" = "8:Company Name"
            "ARPHELPTELEPHONE" = "8:"
            "ARPHELPLINK" = "8:"
            "Title" = "8:DownloadDeployer"
            "Subject" = "8:"
            "ARPCONTACT" = "8:Company Name"
            "Keywords" = "8:"
            "ARPCOMMENTS" = "8:"
            "ARPURLINFOABOUT" = "8:"
            "ARPPRODUCTICON" = "8:"
            "ARPIconIndex" = "3:0"
            "SearchPath" = "8:"
            "UseSystemSearchPath" = "11:TRUE"
            "TargetPlatform" = "3:0"
            "PreBuildEvent" = "8:"
            "PostBuildEvent" = "8:"
            "RunPostBuildEvent" = "3:0"
            }
            

In this file the two important fields are ProductVersion and ProductCode. When we update the version of our component the make sure you change the Version of MSI file also. Which we can do by increasing ProductVersion manually by opening the vdproj file. Here one important thing to keep in mind is to change the ProductCode CLSID every time you increment the Version, but the UpgradeCode should be same always to get the Component get updated automatically on client machine when we deploy the latest. Also make sure to change the RemovePreviousVersions to TRUE which wil avoid creating multiple copies of same component.

Points of Interest

The interesting point here is we have to increment the major Version in object tag every time we increment the versio of MSI file it is because we have only 3 digit ProductVersion in MSI file. One more thing is the ProductCode should contain capital alphabates in its CLSID other wise the VS 2005 will give compilation error while creating MSI file.

About Anand Todkar


I am Software Engineer, interested in research in Microsoft Technologies.

Click here to view Anand Todkar's online profile.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap