Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Article

Downloading C# ActiveX Components through CAB File

Rate me:
Please Sign up or sign in to vote.
4.57/5 (18 votes)
24 Jul 2007CPOL3 min read 277.7K   2.3K   58   84
This article describes the procedure to create an ActiveX Component in C#, download it from server and execute it on client side in Web based application.

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. It also gives the focus on how to update the latest version of component on the 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 convenient 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 a given component, which is not useful to register the C# components.

Using the Code

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

C#
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 successfully!!", 
		"Clientside component", MessageBoxButtons.OK, 
		MessageBoxIcon.Information);
        }
    }
}

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

Image 1

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

Image 2

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 the Microsoft site. It is beyond the scope of this article.

Image 3

We also have another option to create the CAB file using Visual Studio 2005 Cab File Project which automatically includes the OSD file instead of INF file but to modify the OSD file is not an easy task as Visual Studio does not support extensive 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 the 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 the MSI installer, there will not be any problem while invoking the component through script. The script will be as mentioned below:

ASP.NET
<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:

"Product"
{
   "Name" = "8:Microsoft Visual Studio"
   "ProductName" = "8:DownloadDeployer"
   "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, 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 the same always to get the Component updated automatically on the client machine when we deploy the latest. Also make sure to change the RemovePreviousVersions to TRUE which will avoid creating multiple copies of the 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 version of MSI file. It is because we have only 3 digit ProductVersion in the MSI file. One more thing is that the ProductCode should contain capital alphabets in its CLSID otherwise Visual Studio 2005 will give a compilation error while creating the MSI file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
IUnknown.

Comments and Discussions

 
QuestionError in project build 'DownloadDeployer' Pin
Pedro Palma19-Dec-12 1:48
Pedro Palma19-Dec-12 1:48 
GeneralMy vote of 4 Pin
Amitvvaidya14-May-12 2:22
Amitvvaidya14-May-12 2:22 
GeneralMy vote of 5 Pin
GLLNS21-Dec-10 4:01
GLLNS21-Dec-10 4:01 
GeneralGreat Pin
davidlg_77712-Dec-10 9:19
davidlg_77712-Dec-10 9:19 
GeneralProblem with Windows 7 Pin
sadik12-Oct-10 5:43
sadik12-Oct-10 5:43 
GeneralRe: Problem with Windows 7 Pin
Anand Todkar12-Oct-10 20:06
Anand Todkar12-Oct-10 20:06 
GeneralRe: Problem with Windows 7 Pin
sadik14-Oct-10 3:55
sadik14-Oct-10 3:55 
GeneralMy vote of 5 Pin
Larry Spencer17-Aug-10 10:26
Larry Spencer17-Aug-10 10:26 
GeneralProblem of version update Pin
sychenzn6-Jul-10 17:19
sychenzn6-Jul-10 17:19 
GeneralRe: Problem of version update Pin
Member 24543674-Oct-10 2:17
Member 24543674-Oct-10 2:17 
GeneralRe: Problem of version update Pin
wusl9206-Nov-12 22:05
wusl9206-Nov-12 22:05 
GeneralRe: Problem of version update Pin
wusl9206-Nov-12 22:06
wusl9206-Nov-12 22:06 
GeneralAutomation server can't create object Pin
sandeepwebnet26-Mar-10 5:16
sandeepwebnet26-Mar-10 5:16 
GeneralRe: Automation server can't create object Pin
RogueCoder9-Jun-10 12:33
RogueCoder9-Jun-10 12:33 
GeneralgetElementByID Pin
Tacotortilla99523-Feb-10 5:04
Tacotortilla99523-Feb-10 5:04 
GeneralRe: getElementByID Pin
RogueCoder9-Jun-10 12:27
RogueCoder9-Jun-10 12:27 
GeneralThis website wants to run the following add-on: 'Control name is not available' from 'Unknown Publisher error comes when we use activex Pin
jomygeorge20-Jan-10 18:06
jomygeorge20-Jan-10 18:06 
GeneralRe: This website wants to run the following add-on: 'Control name is not available' from 'Unknown Publisher error comes when we use activex Pin
Anand Todkar20-Jan-10 18:09
Anand Todkar20-Jan-10 18:09 
GeneralRe: This website wants to run the following add-on: 'Control name is not available' from 'Unknown Publisher error comes when we use activex Pin
Tacotortilla99523-Feb-10 4:19
Tacotortilla99523-Feb-10 4:19 
GeneralRe: This website wants to run the following add-on: 'Control name is not available' from 'Unknown Publisher error comes when we use activex Pin
RogueCoder9-Jun-10 12:31
RogueCoder9-Jun-10 12:31 
GeneralRe: This website wants to run the following add-on: 'Control name is not available' from 'Unknown Publisher error comes when we use activex Pin
Andrewiski11-Aug-10 18:46
Andrewiski11-Aug-10 18:46 
QuestionNot working when hosted in IIS Pin
Member 430193922-Oct-09 3:00
Member 430193922-Oct-09 3:00 
QuestionvsdrpCOM doesnt work in Vista 64 bit environment Pin
shadysaim31-Mar-09 22:06
shadysaim31-Mar-09 22:06 
GeneralActivex Version Pin
dwipayan17-Mar-09 18:59
dwipayan17-Mar-09 18:59 
Generalinstalation issues [modified] Pin
karoliskk2-Feb-09 2:29
karoliskk2-Feb-09 2:29 
hi, I have 2 problems:

I get "automation server can't create an object" error. I implemented example above and I'm pretty sure I copied all this correctly. The only thing is that I use Visual Studio 2008, but I doubt it that its the problem because everything looks the same as in this tutorial. I enabled all activex options in IE security options and added server to trusted sites. I viewed error log in temporary files and found this:
*** Code Download Log entry (02 Feb 2009 @ 12:37:48) ***
Code Download Error: (hr = 800b0004) The subject is not trusted for the specified action.

Operation failed. Detailed Information:
     CodeBase: http://10.15.11.6/activex/DownloadDeployer.cab
     CLSID: {A47C22B1-3CC3-45BC-801E-3FCC4FFD3E45}
     Extension: 
     Type: 

LOG: Reporting Code Download Completion: (hr:800b0004 (FAILED), CLASSID: a47c22b1..., szCODE:(http://10.15.11.6/activex/DownloadDeployer.cab), MainType:(null), MainExt:(null))
--- Detailed Error Log Follows ---
LOG: Download OnStopBinding called (hrStatus = 0 / hrResponseHdr = 0).
LOG: URL Download Complete: hrStatus:0, hrOSB:800b0004, hrResponseHdr:0, URL:(http://10.15.11.6/activex/DownloadDeployer.cab)
LOG: Reporting Code Download Completion: (hr:800b0004 (FAILED), CLASSID: a47c22b1..., szCODE:(http://10.15.11.6/activex/DownloadDeployer.cab), MainType:(null), MainExt:(null))

(However it is not the not the biggest problem because I have another C# example which is working when I create cab.)

So other problem is that CAB is being installed every time. I noticed that file version in "program files/<company>/Setup3" is always 1.0.0.0 (EDIT: ok, I learned how to fix it - need to change version number in "assembly information", but it doesn't help) even if I change it in HTML file to 1,0,0,1 and in project to 1.0.1. Here I also get an error:

*** Code Download Log entry (02 Feb 2009 @ 13:08:34) ***
Code Download Error: (hr = 80040154) Class not registered

Operation failed. Detailed Information:
     CodeBase: http://10.15.11.6/activex/ac.cab
     CLSID: {E86A9038-368D-4E8F-B389-FDEF38935B2F}
     Extension: 
     Type: 

LOG: Setup Hook hook1 was executed successfully.
--- Detailed Error Log Follows ---
LOG: Download OnStopBinding called (hrStatus = 0 / hrResponseHdr = 0).
LOG: URL Download Complete: hrStatus:0, hrOSB:0, hrResponseHdr:0, URL:(http://10.15.11.6/activex/ac.cab)
LOG: Setup Hook hook1 was executed successfully.
LOG: Setup successful installing: d.inf to (null) destination code(0)
LOG: Reporting Code Download Completion: (hr:80040154, CLASSID: e86a9038..., szCODE:(http://10.15.11.6/activex/ac.cab), MainType:(null), MainExt:(null))

I use IE7.

EDIT: when I go C:\WINDOWS\Downloaded Program Files I see that my activex file's name is same as CLSID and its status is unknown (should be installed, I guess..).

Any ideas?

thanks,
Karolis

modified on Tuesday, February 3, 2009 5:13 AM

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.