Download demo project - 65.3 KB
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 R e g i s t e r s e r v e r=Y e s 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.
Collapse | Copy Code u s i n g S y s t e m;
u s i n g S y s t e m.T e x t;
u s i n g S y s t e m.W i n d o w s.F o r m s;
u s i n g S y s t e m.C o l l e c t i o n s.G e n e r i c;
u s i n g S y s t e m.R u n t i m e.I n t e r o p S e r v i c e s;
n a m e s p a c e D o w n l o a d e d A p p
{
[G u i d("D1E40F C E-E964-4d f d-B07E-B D E49D3519A1")]
i n t e r f a c e I D o w n l o a d
{
v o i d I n v o k e M e t h o d();
}
///
///C O M e x p o s e d.N E T c l a s s.
///
[C l a s s I n t e r f a c e(C l a s s I n t e r f a c e T y p e.A u t o D u a l)]
[G u i d("A47C22B1-3C C3-45b c-801E-3F C C4F F D3E45")]
p u b l i c c l a s s D o w n l o a d:I D o w n l o a d
{
///
/
//C O M e x p o s e d.N E T M e t h o d.
///
p u b l i c v o i d I n v o k e M e t h o d()
{
M e s s a g e B o x.S h o w("C l i e n t s i d e c o m p o n e n t i n v o k e d s u c c e s s f u l l y!!",
"C l i e n t s i d e c o m p o n e n t",M e s s a g e B o x B u t t o n s.O K,
M e s s a g e B o x I c o n.I n f o r m a t i o n);
}
}
}
Now create the MSI Setup project in Visual Studio 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. 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.
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.
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.
Collapse | Copy Code [v e r s i o n]
s i g n a t u r e="$C H I C A G O$"
A d v a n c e d I N F=2.0
[S e t u p H o o k s]
h o o k1=h o o k1
[h o o k1]
r u n=m s i e x e c.e x e/i"%E X T R A C T_D I R%\D o w n l o a d D e p l o y e r.m s i"/q n
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:
Collapse | Copy Code <h t m l>
<h e a d>
<s c r i p t L A N G U A G E="J a v a S c r i p t1.2">
f u n c t i o n S c r a p e()
{
v a r o b j D o w n l o a d=n e w A c t i v e X O b j e c t("D o w n l o a d e d A p p.D o w n l o a d");
t r y
{
o b j D o w n l o a d.I n v o k e M e t h o d();
}
c a t c h(e x c e p t i o n){
a l e r t("F a i l e d");
}
}
</s c r i p t>
</h e a d>
<b o d y>
<t a b l e a l i g n="c e n t e r">
<t d>
<a h r e f="j a v a s c r i p t:S c r a p e()">I n v o k e C o m p o n e n t</a>
</t d>
</t a b l e>
<p>P l e a s e W a D o w n l o a d i n g c o m p o n e n </p>
<o b j e c t n a m e="s e c o n d o b j"s t y l e='d i s p l a y:n o n e'i d='T e s t A c t i v e x'
c l a s s i d='C L S I D:A47C22B1-3C C3-45b c-801E-3F C C4F F D3E45'
c o
d
e b a s e='D o w n l o a d D e p l o y e r.c a b#v e r s i o n=1,0,0,0'></o b j e c t>
</b o d y>
</h t m l>
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 | Copy Code "P r o d u c t"
{
"N a m e"="8:M i c r o s o f t V i s u a l S t u d i o"
"P r o d u c t N a m e"="8:D o w n l o a d D e p l o y e r"
"P r o d u c t C o d e"="8:{82C C6087-5430-4343-9F8B-207A889F92A1}"
activex 控件"P a c k a g e C o d e"="8:{9D5D F C79-5E0F-4A6E-8796-10F08758F D2E}"
"U p g r a d e C o d e"="8:{21D008F F-E2B B-4C7B-A985-B20B9C B0C2D8}"
"R e s t a r t W W W S e r v i c e"="11:F A L S E"
"R e m o v e P r e v i o u s V e r s i o n s"="11:T R U E"
"D e t e c t N e w e r I n s t a l l e d V e r s i o n"="11:T R U E"
"I n s t a l l A l l U s e r s"="11:F A L S E"
"P r o d u c t V e r s i o n"="8:1.0.0"
"M a n u f a c t u r e r"="8:C o m p a n y N a m e"
"A R P H E L P T E L E P H O N E"="8:"
"A R P H E L P L I N K"="8:"
"T i t l e"="8:D o w n l o a d D e p l o y e r"
"S u b j e c t"="8:"
"A R P C O N T A C T"="8:C o m p a n y N a m e"
"K e y w o r d s"="8:"
"A R P C O M M E N T S"="8:"
"A R P U R L I N F O A B O U T"="8:"
"A R P P R O D U C T I C O N"="8:"
"A R P I c o n I n d e x"="3:0"
"S e a r c h P a t h"="8:"
"U s e S y s t e m S e a r c h P a t h"="11:T R U E"
"T a r g e t P l a t f o r m"="3:0"
"P r e B u i l d E v e n t"="8:"
"P o s t B u i l d E v e n t"="8:"
"R u n P o s t B u i l d E v e n t"="3:0"
}
In this file, the two important fields are P r o d u c t V e r s i o n and P r o d u c t C o d e. When we update the version of our component, make sure you change the Version of MSI file also. Which we can do by increasing P r o d u c t V e r s i o n manually by opening the *.vdproj file. Here one important thing to keep in mind is to change the P r o d u c t C o d e C L S I D every time you increment the Version, but
the U p g r a d e C o d e 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 R e m o v e P r e v i o u s V e r s i o n s to T R U E 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 P r o d u c t V e r s i o n in the M
SI file. One more thing is that the P r o d u c t C o d e should contain capital alphabets in its C L S I D 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)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论