Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

473 lines
11 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. assemblyreference.cpp
  5. Abstract:
  6. Class the contains all the attributes of an assembly reference.
  7. Author:
  8. Michael J. Grier (MGrier) 10-May-2000
  9. Revision History:
  10. xiaoyuw 09/2000 revise the code using Assembly Identity
  11. A couple of APIs in this class are kind of out of date, such as GetXXX, SetXXX, XXXSpecified : they
  12. are not called at all.
  13. --*/
  14. #include "stdinc.h"
  15. #include <windows.h>
  16. #include "sxsapi.h"
  17. #include "sxsp.h"
  18. #include "assemblyreference.h"
  19. #include "sxsid.h"
  20. #include "sxsidp.h"
  21. #include "fusionparser.h"
  22. #include "fusionheap.h"
  23. #include "SxsExceptionHandling.h"
  24. CAssemblyReference::CAssemblyReference()
  25. : m_pAssemblyIdentity(NULL)
  26. {
  27. }
  28. CAssemblyReference::~CAssemblyReference()
  29. {
  30. CSxsPreserveLastError ple;
  31. if (m_pAssemblyIdentity != NULL)
  32. ::SxsDestroyAssemblyIdentity(m_pAssemblyIdentity);
  33. ple.Restore();
  34. }
  35. BOOL
  36. CAssemblyReference::Initialize()
  37. {
  38. BOOL fSuccess = FALSE;
  39. FN_TRACE_WIN32(fSuccess);
  40. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity == NULL);
  41. IFW32FALSE_EXIT(::SxsCreateAssemblyIdentity(0, ASSEMBLY_IDENTITY_TYPE_REFERENCE, &m_pAssemblyIdentity, 0, NULL));
  42. fSuccess = TRUE;
  43. Exit:
  44. return fSuccess;
  45. }
  46. BOOL
  47. CAssemblyReference::Initialize(
  48. PCASSEMBLY_IDENTITY Identity
  49. )
  50. {
  51. BOOL fSuccess = FALSE;
  52. FN_TRACE_WIN32(fSuccess);
  53. PARAMETER_CHECK(Identity != NULL);
  54. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity == NULL);
  55. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(0, Identity, &m_pAssemblyIdentity));
  56. fSuccess = TRUE;
  57. Exit:
  58. return fSuccess;
  59. }
  60. BOOL
  61. CAssemblyReference::Initialize(
  62. const CAssemblyReference &r
  63. )
  64. {
  65. BOOL fSuccess = FALSE;
  66. FN_TRACE_WIN32(fSuccess);
  67. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity == NULL);
  68. INTERNAL_ERROR_CHECK(r.m_pAssemblyIdentity != NULL);
  69. IFW32FALSE_EXIT(
  70. ::SxsDuplicateAssemblyIdentity(
  71. 0, // DWORD Flags,
  72. r.m_pAssemblyIdentity, // PCASSEMBLY_IDENTITY Source,
  73. &m_pAssemblyIdentity)); // PASSEMBLY_IDENTITY *Destination
  74. fSuccess = TRUE;
  75. Exit:
  76. return fSuccess;
  77. }
  78. BOOL
  79. CAssemblyReference::Hash(
  80. ULONG &rulPseudoKey
  81. ) const
  82. {
  83. BOOL fSuccess = FALSE;
  84. FN_TRACE_WIN32(fSuccess);
  85. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  86. IFW32FALSE_EXIT(
  87. ::SxsHashAssemblyIdentity(
  88. 0, // DWORD dwFlags,
  89. m_pAssemblyIdentity, // ASSEMBLY_IDENTITY pAssemblyIdentity,
  90. &rulPseudoKey)); // ULONG & rfulPseudoKey
  91. fSuccess = TRUE;
  92. Exit:
  93. return fSuccess;
  94. }
  95. BOOL
  96. CAssemblyReference::SetAssemblyName(
  97. PCWSTR AssemblyNameValue,
  98. SIZE_T AssemblyNameValueCch
  99. )
  100. {
  101. BOOL fSuccess = FALSE;
  102. FN_TRACE_WIN32(fSuccess);
  103. PARAMETER_CHECK(AssemblyNameValue != NULL);
  104. PARAMETER_CHECK(AssemblyNameValueCch != 0);
  105. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  106. IFW32FALSE_EXIT(
  107. ::SxspSetAssemblyIdentityAttributeValue(
  108. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  109. m_pAssemblyIdentity,
  110. &s_IdentityAttribute_name,
  111. AssemblyNameValue,
  112. AssemblyNameValueCch));
  113. fSuccess = TRUE;
  114. Exit:
  115. return fSuccess;
  116. }
  117. // if m_pAssemblyIdentity is NULL, return TRUE with Cch == 0
  118. BOOL
  119. CAssemblyReference::GetAssemblyName(
  120. PCWSTR *pAssemblyName,
  121. SIZE_T *Cch
  122. ) const
  123. {
  124. BOOL fSuccess = FALSE;
  125. FN_TRACE_WIN32(fSuccess);
  126. SIZE_T CchTemp;
  127. if (Cch != NULL)
  128. *Cch = 0;
  129. if (pAssemblyName != NULL)
  130. *pAssemblyName = NULL;
  131. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  132. IFW32FALSE_EXIT(
  133. ::SxspGetAssemblyIdentityAttributeValue(
  134. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  135. m_pAssemblyIdentity,
  136. &s_IdentityAttribute_name,
  137. pAssemblyName,
  138. &CchTemp));
  139. if (Cch != NULL)
  140. *Cch = CchTemp;
  141. fSuccess = TRUE;
  142. Exit:
  143. return fSuccess;
  144. }
  145. BOOL
  146. CAssemblyReference::TakeValue(
  147. const CAssemblyReference &r
  148. )
  149. {
  150. BOOL fSuccess = FALSE;
  151. FN_TRACE_WIN32(fSuccess);
  152. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity == NULL);
  153. INTERNAL_ERROR_CHECK(r.m_pAssemblyIdentity != NULL);
  154. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(0, r.m_pAssemblyIdentity, &m_pAssemblyIdentity));
  155. fSuccess = TRUE;
  156. Exit:
  157. return fSuccess;
  158. }
  159. BOOL CAssemblyReference::ClearAssemblyName()
  160. {
  161. BOOL fSuccess = FALSE;
  162. FN_TRACE_WIN32(fSuccess);
  163. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  164. IFW32FALSE_EXIT(::SxspRemoveAssemblyIdentityAttribute(0, m_pAssemblyIdentity, &s_IdentityAttribute_name));
  165. fSuccess = TRUE;
  166. Exit:
  167. return fSuccess;
  168. }
  169. BOOL
  170. CAssemblyReference::SetLanguage(
  171. const CBaseStringBuffer &rbuff
  172. )
  173. {
  174. BOOL fSuccess = FALSE;
  175. FN_TRACE_WIN32(fSuccess);
  176. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  177. IFW32FALSE_EXIT(
  178. ::SxspSetAssemblyIdentityAttributeValue(
  179. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  180. m_pAssemblyIdentity,
  181. &s_IdentityAttribute_language,
  182. rbuff,
  183. rbuff.Cch()));
  184. fSuccess = TRUE;
  185. Exit:
  186. return fSuccess;
  187. }
  188. BOOL
  189. CAssemblyReference::ClearLanguage()
  190. {
  191. BOOL fSuccess = FALSE;
  192. FN_TRACE_WIN32(fSuccess);
  193. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  194. IFW32FALSE_EXIT(::SxspRemoveAssemblyIdentityAttribute(SXSP_REMOVE_ASSEMBLY_IDENTITY_ATTRIBUTE_FLAG_NOT_FOUND_SUCCEEDS, m_pAssemblyIdentity, &s_IdentityAttribute_language));
  195. fSuccess = TRUE;
  196. Exit:
  197. return fSuccess;
  198. }
  199. BOOL
  200. CAssemblyReference::IsLanguageWildcarded(
  201. bool &rfWildcarded
  202. ) const
  203. {
  204. BOOL fSuccess = FALSE;
  205. FN_TRACE_WIN32(fSuccess);
  206. const WCHAR *Value = NULL;
  207. SIZE_T Cch = 0;
  208. rfWildcarded = false;
  209. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  210. IFW32FALSE_EXIT(::SxspGetAssemblyIdentityAttributeValue(SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL, m_pAssemblyIdentity, &s_IdentityAttribute_language, &Value, &Cch));
  211. if (Cch == 1)
  212. {
  213. INTERNAL_ERROR_CHECK(Value != NULL);
  214. if (Value[0] == L'*')
  215. rfWildcarded = true;
  216. }
  217. fSuccess = TRUE;
  218. Exit:
  219. return fSuccess;
  220. }
  221. BOOL
  222. CAssemblyReference::IsProcessorArchitectureWildcarded(
  223. bool &rfWildcarded
  224. ) const
  225. {
  226. BOOL fSuccess = FALSE;
  227. FN_TRACE_WIN32(fSuccess);
  228. const WCHAR *Value = NULL;
  229. SIZE_T Cch = 0;
  230. rfWildcarded = false;
  231. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  232. IFW32FALSE_EXIT(::SxspGetAssemblyIdentityAttributeValue(SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL, m_pAssemblyIdentity, &s_IdentityAttribute_processorArchitecture, &Value, &Cch));
  233. if (Cch == 1)
  234. {
  235. INTERNAL_ERROR_CHECK(Value != NULL);
  236. if (Value[0] == L'*')
  237. rfWildcarded = true;
  238. }
  239. fSuccess = TRUE;
  240. Exit:
  241. return fSuccess;
  242. }
  243. BOOL
  244. CAssemblyReference::IsProcessorArchitectureX86(
  245. bool &rfX86
  246. ) const
  247. {
  248. BOOL fSuccess = FALSE;
  249. FN_TRACE_WIN32(fSuccess);
  250. const WCHAR *Value = NULL;
  251. SIZE_T Cch = 0;
  252. rfX86 = false;
  253. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  254. IFW32FALSE_EXIT(::SxspGetAssemblyIdentityAttributeValue(SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL, m_pAssemblyIdentity, &s_IdentityAttribute_processorArchitecture, &Value, &Cch));
  255. if (Cch == 3)
  256. {
  257. INTERNAL_ERROR_CHECK(Value != NULL);
  258. if (((Value[0] == L'x') || (Value[0] == L'X')) &&
  259. (Value[1] == L'8') &&
  260. (Value[2] == L'6'))
  261. rfX86 = true;
  262. }
  263. fSuccess = TRUE;
  264. Exit:
  265. return fSuccess;
  266. }
  267. BOOL
  268. CAssemblyReference::SetProcessorArchitecture(
  269. const WCHAR *String,
  270. SIZE_T Cch
  271. )
  272. {
  273. BOOL fSuccess = FALSE;
  274. FN_TRACE_WIN32(fSuccess);
  275. PARAMETER_CHECK((String != NULL) || (Cch == 0));
  276. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  277. IFW32FALSE_EXIT(
  278. ::SxspSetAssemblyIdentityAttributeValue(
  279. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  280. m_pAssemblyIdentity,
  281. &s_IdentityAttribute_processorArchitecture,
  282. String,
  283. Cch));
  284. fSuccess = TRUE;
  285. Exit:
  286. return fSuccess;
  287. }
  288. BOOL
  289. CAssemblyReference::Assign(
  290. const CAssemblyReference &r
  291. )
  292. {
  293. BOOL fSuccess = FALSE;
  294. FN_TRACE_WIN32(fSuccess);
  295. PASSEMBLY_IDENTITY IdentityCopy = NULL;
  296. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  297. INTERNAL_ERROR_CHECK(r.m_pAssemblyIdentity != NULL);
  298. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(0, r.m_pAssemblyIdentity, &IdentityCopy));
  299. ::SxsDestroyAssemblyIdentity(m_pAssemblyIdentity);
  300. m_pAssemblyIdentity = IdentityCopy;
  301. fSuccess = TRUE;
  302. Exit:
  303. return fSuccess;
  304. }
  305. //dupilicate the input parameter
  306. BOOL
  307. CAssemblyReference::SetAssemblyIdentity(
  308. PCASSEMBLY_IDENTITY pAssemblyIdentitySource
  309. )
  310. {
  311. BOOL fSuccess = FALSE;
  312. FN_TRACE_WIN32(fSuccess);
  313. PASSEMBLY_IDENTITY TempAssemblyIdentity = NULL;
  314. PARAMETER_CHECK(pAssemblyIdentitySource != NULL);
  315. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL); // you should have initialized to start with...
  316. IFW32FALSE_EXIT(::SxsDuplicateAssemblyIdentity(
  317. 0,
  318. pAssemblyIdentitySource,
  319. &TempAssemblyIdentity));
  320. ::SxsDestroyAssemblyIdentity(m_pAssemblyIdentity);
  321. m_pAssemblyIdentity = TempAssemblyIdentity;
  322. fSuccess = TRUE;
  323. Exit:
  324. return fSuccess;
  325. }
  326. BOOL
  327. CAssemblyReference::GetPublicKeyToken(
  328. CBaseStringBuffer *pbuffPublicKeyToken,
  329. BOOL &rfHasPublicKeyToken
  330. ) const
  331. {
  332. FN_PROLOG_WIN32
  333. PCWSTR wchString = NULL;
  334. SIZE_T cchString = NULL;
  335. rfHasPublicKeyToken = FALSE;
  336. if (pbuffPublicKeyToken != NULL)
  337. pbuffPublicKeyToken->Clear();
  338. IFW32FALSE_EXIT(
  339. ::SxspGetAssemblyIdentityAttributeValue(
  340. SXSP_GET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_NOT_FOUND_RETURNS_NULL,
  341. m_pAssemblyIdentity,
  342. &s_IdentityAttribute_publicKeyToken,
  343. &wchString,
  344. &cchString));
  345. if (cchString != 0)
  346. {
  347. rfHasPublicKeyToken = TRUE;
  348. if (pbuffPublicKeyToken != NULL)
  349. IFW32FALSE_EXIT(pbuffPublicKeyToken->Win32Assign(wchString, cchString));
  350. }
  351. FN_EPILOG
  352. }
  353. BOOL CAssemblyReference::SetPublicKeyToken(
  354. const CBaseStringBuffer &rbuffPublicKeyToken
  355. )
  356. {
  357. return this->SetPublicKeyToken(rbuffPublicKeyToken, rbuffPublicKeyToken.Cch());
  358. }
  359. BOOL CAssemblyReference::SetPublicKeyToken(
  360. PCWSTR pszPublicKeyToken,
  361. SIZE_T cchPublicKeyToken
  362. )
  363. {
  364. BOOL bSuccess = FALSE;
  365. FN_TRACE_WIN32( bSuccess );
  366. PARAMETER_CHECK( (pszPublicKeyToken != NULL ) || ( cchPublicKeyToken == 0 ) );
  367. INTERNAL_ERROR_CHECK(m_pAssemblyIdentity != NULL);
  368. IFW32FALSE_EXIT(
  369. ::SxspSetAssemblyIdentityAttributeValue(
  370. SXSP_SET_ASSEMBLY_IDENTITY_ATTRIBUTE_VALUE_FLAG_OVERWRITE_EXISTING,
  371. m_pAssemblyIdentity,
  372. &s_IdentityAttribute_publicKeyToken,
  373. pszPublicKeyToken,
  374. cchPublicKeyToken));
  375. bSuccess = TRUE;
  376. Exit:
  377. return bSuccess;
  378. }