1 //------------------------------------------------------------------------------
2 // <copyright from='2003' to='2004' company='Microsoft Corporation'>
3 // Copyright (c) Microsoft Corporation, All rights reserved.
4 // This code sample is provided "AS IS" without warranty of any kind,
5 // it is not recommended for use in a production environment.
7 //------------------------------------------------------------------------------
10 using System
.Globalization
;
12 namespace Microsoft
.VisualStudio
.Shell
16 /// This attribute registers an 'installed product' for your package.
17 /// This enables your package to present information on the VS
18 /// Splash Screen or Help About.
20 [AttributeUsage(AttributeTargets
.Class
, Inherited
=false)]
21 public sealed class InstalledProductRegistrationAttribute
: RegistrationAttribute
24 private string _productName
;
26 private string _productId
;
27 private string _productDetails
;
28 private string _iconResourceId
;
29 private bool _useInterface
= false;
30 private bool _usePackage
= false;
33 /// Creates a new InstalledProductRegistrationAttribute to register your package with
34 /// Help/About and enables it to present informations on the VS Splash Screen.
36 public InstalledProductRegistrationAttribute (bool useInterface
, string productName
, string productDetails
, string productId
)
39 _useInterface
= useInterface
;
42 // If the object uses the interface in order to expose its informations to the
43 // Help/About page, then the registration must use the package, otherwise there
44 // is no way to know where to find such interface.
49 // We are not using the interface, so we need the other parameters.
50 // Let's check that they are not null or empty.
51 if ((null == productName
) || (productName
.Trim().Length
== 0))
52 throw new ArgumentNullException("productName");
53 productName
= productName
.Trim();
55 if ((null == productDetails
) || (productDetails
.Trim().Length
== 0))
56 throw new ArgumentNullException("productDetails");
57 productDetails
= productDetails
.Trim();
59 if ((null == productId
) || (productId
.Trim().Length
== 0))
60 throw new ArgumentNullException("productId");
61 productId
= productId
.Trim();
63 // Assign the values to the member variables
64 _productName
= productName
;
65 _productDetails
= productDetails
;
66 _productId
= productId
;
68 // Now that we know that the parameters are not empty, let's do a final
69 // validation: the Help/About code assumes that if a registration is made
70 // using a package, then both the product name and the product details are
71 // id of resources (so they are supposed to be in the form "#nnn"), but if
72 // they are strings, then they both must be string.
73 if (((ProductNameResourceID
!= 0) && (ProductDetailsResourceID
== 0)) ||
74 ((ProductNameResourceID
== 0) && (ProductDetailsResourceID
!= 0)))
76 // If we are here we have the resource id for only one entry and,
77 // according with the Help/About code, this is bad, so we have to
78 // throw an exception.
79 string errorMessage
= string.Format(Resources
.Culture
, Resources
.Reg_ErrorIncompatibleParametersTypes
, "productName", "productDetails");
80 throw new ArgumentException(errorMessage
);
83 // Now check if the name and details are resource id because in this case
84 // we have to use the package to register. Actually we check only the name
85 // because of the previous test.
86 _usePackage
= (ProductNameResourceID
!= 0) ;
92 /// Resource ID corresponding to the product name.
94 public int ProductNameResourceID
98 if (_productName
== null || _productName
.Length
< 2 || _productName
[0]!='#' || !char.IsDigit(_productName
[1]))
100 return int.Parse( _productName
.Substring(1), CultureInfo
.InvariantCulture
);
104 /// Resource ID for the details.
106 public int ProductDetailsResourceID
110 if (_productDetails
== null || _productDetails
.Length
< 2 || _productDetails
[0]!='#' || !char.IsDigit(_productDetails
[1]))
112 return int.Parse( _productDetails
.Substring(1), CultureInfo
.InvariantCulture
);
116 /// Resource ID of the icon.
118 public int IconResourceID
122 if (String
.IsNullOrEmpty(_iconResourceId
) || _iconResourceId
.Length
< 2)
124 return int.Parse( _iconResourceId
.Substring(1), CultureInfo
.InvariantCulture
);
128 _iconResourceId
= @"#" + value.ToString(CultureInfo
.InvariantCulture
);
134 public string ProductId
136 get {return _productId;}
139 /// The name of your product.
141 public string ProductName
143 get { return _productName; }
147 /// The name of your product.
149 public string LanguageIndependentName
151 get { return _name; }
152 set { _name = value; }
155 private string GetNonEmptyName(RegistrationContext context
)
157 string product
= LanguageIndependentName
;
159 product
= product
.Trim();
160 if (String
.IsNullOrEmpty(product
))
161 product
= context
.ComponentType
.Name
;
165 /// Detailed description of your product.
167 public string ProductDetails
169 get {return _productDetails;}
172 /// Use IVsInstalledProduct to fill in the Help about dialog.
173 /// The package must implement IVsInstalledProduct.
175 public bool UseInterface
177 get {return _useInterface;}
180 /// True is the product installation will use the package ID
182 public bool UsePackage
{
190 private string RegKeyName (RegistrationContext context
)
192 return string.Format(CultureInfo
.InvariantCulture
, "InstalledProducts\\{0}", GetNonEmptyName(context
));
198 /// Called to register this attribute with the given context.
200 /// <param name="context">
201 /// Contains the location where the registration information should be placed.
202 /// It also contains other information such as the type being registered and path information.
204 public override void Register(RegistrationContext context
)
208 context
.Log
.WriteLine(Resources
.Reg_NotifyInstalledProductInterface
);
212 context
.Log
.WriteLine(Resources
.Reg_NotifyInstalledProduct
, GetNonEmptyName(context
), ProductId
);
215 using (Key packageKey
= context
.CreateKey(RegKeyName(context
)))
218 // Set the 'Package' value if necessary
221 packageKey
.SetValue("Package", context
.ComponentType
.GUID
.ToString("B"));
224 // Set the 'UseRegNameAsSplashName' flag if the user provided a short name that should be used on the splash screen
225 if (!String
.IsNullOrEmpty(_name
))
227 packageKey
.SetValue("UseRegNameAsSplashName", 1);
230 // Set the 'UseInterface' value if necessary
233 packageKey
.SetValue("UseInterface", 1);
237 // If UseInterface is 0, then the following are required for HelpAbout
238 packageKey
.SetValue("", ProductName
);
239 packageKey
.SetValue("ProductDetails", ProductDetails
);
240 packageKey
.SetValue("PID", ProductId
);
242 // The icon resource id reg entry is only valid if there
243 // is a package satellite and not using the interface
244 if (UsePackage
&& !String
.IsNullOrEmpty(_iconResourceId
))
246 packageKey
.SetValue("LogoID", _iconResourceId
);
253 /// Unregister this InstalledProducts entry.
255 /// <param name="context"></param>
256 public override void Unregister(RegistrationContext context
)
258 context
.RemoveKey(RegKeyName(context
));