The Labs \ Source Viewer \ SSCLI \ Microsoft.Win32 \ Fusion

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //-------------------------------------------------------------
  16. // FusionInterfaces.cs
  17. //
  18. // This implements wrappers to Fusion interfaces
  19. //-------------------------------------------------------------
  20. namespace Microsoft.Win32
  21. {
  22.     using System;
  23.     using System.IO;
  24.     using System.Collections;
  25.     using System.Runtime.InteropServices;
  26.     using System.Runtime.CompilerServices;
  27.     using System.Globalization;
  28.     using StringBuilder = System.Text.StringBuilder;
  29.     using Microsoft.Win32.SafeHandles;
  30.    
  31.    
  32.    
  33.     static internal class ASM_CACHE
  34.     {
  35.         public const uint ZAP = 1;
  36.         public const uint GAC = 2;
  37.         public const uint DOWNLOAD = 4;
  38.     }
  39.    
  40.     static internal class CANOF
  41.     {
  42.         public const uint PARSE_DISPLAY_NAME = 1;
  43.         public const uint SET_DEFAULT_VALUES = 2;
  44.     }
  45.    
  46.     static internal class ASM_NAME
  47.     {
  48.         public const uint PUBLIC_KEY = 0;
  49.         public const uint PUBLIC_KEY_TOKEN = PUBLIC_KEY + 1;
  50.         public const uint HASH_VALUE = PUBLIC_KEY_TOKEN + 1;
  51.         public const uint NAME = HASH_VALUE + 1;
  52.         public const uint MAJOR_VERSION = NAME + 1;
  53.         public const uint MINOR_VERSION = MAJOR_VERSION + 1;
  54.         public const uint BUILD_NUMBER = MINOR_VERSION + 1;
  55.         public const uint REVISION_NUMBER = BUILD_NUMBER + 1;
  56.         public const uint CULTURE = REVISION_NUMBER + 1;
  57.         public const uint PROCESSOR_ID_ARRAY = CULTURE + 1;
  58.         public const uint OSINFO_ARRAY = PROCESSOR_ID_ARRAY + 1;
  59.         public const uint HASH_ALGID = OSINFO_ARRAY + 1;
  60.         public const uint ALIAS = HASH_ALGID + 1;
  61.         public const uint CODEBASE_URL = ALIAS + 1;
  62.         public const uint CODEBASE_LASTMOD = CODEBASE_URL + 1;
  63.         public const uint NULL_PUBLIC_KEY = CODEBASE_LASTMOD + 1;
  64.         public const uint NULL_PUBLIC_KEY_TOKEN = NULL_PUBLIC_KEY + 1;
  65.         public const uint CUSTOM = NULL_PUBLIC_KEY_TOKEN + 1;
  66.         public const uint NULL_CUSTOM = CUSTOM + 1;
  67.         public const uint MVID = NULL_CUSTOM + 1;
  68.         public const uint _32_BIT_ONLY = MVID + 1;
  69.         public const uint MAX_PARAMS = _32_BIT_ONLY + 1;
  70.     }
  71.    
  72.     static internal class Fusion
  73.     {
  74.         public static void ReadCache(ArrayList alAssems, string name, uint nFlag)
  75.         {
  76.             SafeFusionHandle aEnum = null;
  77.             SafeFusionHandle aNameEnum = null;
  78.             SafeFusionHandle AppCtx = SafeFusionHandle.InvalidHandle;
  79.             int hr;
  80.            
  81.             if (name != null) {
  82.                 hr = Win32Native.CreateAssemblyNameObject(out aNameEnum, name, CANOF.PARSE_DISPLAY_NAME, IntPtr.Zero);
  83.                 if (hr != 0)
  84.                     Marshal.ThrowExceptionForHR(hr);
  85.             }
  86.            
  87.             using (aNameEnum) {
  88.                
  89.                 hr = Win32Native.CreateAssemblyEnum(out aEnum, AppCtx, aNameEnum, nFlag, IntPtr.Zero);
  90.                 if (hr != 0)
  91.                     Marshal.ThrowExceptionForHR(hr);
  92.                
  93.                 using (aEnum) {
  94.                    
  95.                     for (;;)
  96.                         using (SafeFusionHandle aAppCtx = new SafeFusionHandle()SafeFusionHandle aName = new SafeFusionHandle()) {
  97.                             if (!GetNextAssembly(aEnum, aAppCtx, aName, 0))
  98.                                 break;
  99.                            
  100.                             string sDisplayName = GetDisplayName(aName, 0);
  101.                             if (sDisplayName == null)
  102.                                 continue;
  103.                            
  104.                             alAssems.Add(sDisplayName);
  105.                         }
  106.                    
  107.                 }
  108.                 // using (aEnum)
  109.             }
  110.             // using (aNameEnum)
  111.         }
  112.        
  113.        
  114.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  115.         static extern bool GetNextAssembly(SafeFusionHandle pEnum, SafeFusionHandle pAppCtx, SafeFusionHandle pName, uint dwFlags);
  116.        
  117.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  118.         static extern string GetDisplayName(SafeFusionHandle pName, uint dwDisplayFlags);
  119.        
  120.     }
  121. }

Developer Fusion