private AssemblyNameExtension ConstructAssemblyName(IntPtr asmMetaPtr, char[] asmNameBuf, uint asmNameLength, IntPtr pubKeyPtr, uint pubKeyBytes, uint flags)
        {
            ASSEMBLYMETADATA assemblymetadata = (ASSEMBLYMETADATA)Marshal.PtrToStructure(asmMetaPtr, typeof(ASSEMBLYMETADATA));
            AssemblyName     assemblyName     = new AssemblyName {
                Name    = new string(asmNameBuf, 0, ((int)asmNameLength) - 1),
                Version = new Version(assemblymetadata.usMajorVersion, assemblymetadata.usMinorVersion, assemblymetadata.usBuildNumber, assemblymetadata.usRevisionNumber)
            };
            string name = Marshal.PtrToStringUni(assemblymetadata.rpLocale);

            if (name.Length > 0)
            {
                assemblyName.CultureInfo = CultureInfo.CreateSpecificCulture(name);
            }
            else
            {
                assemblyName.CultureInfo = CultureInfo.CreateSpecificCulture(string.Empty);
            }
            byte[] destination = new byte[pubKeyBytes];
            Marshal.Copy(pubKeyPtr, destination, 0, (int)pubKeyBytes);
            if ((flags & 1) != 0)
            {
                assemblyName.SetPublicKey(destination);
            }
            else
            {
                assemblyName.SetPublicKeyToken(destination);
            }
            assemblyName.Flags = (AssemblyNameFlags)flags;
            return(new AssemblyNameExtension(assemblyName));
        }
 private void FreeAsmMeta(IntPtr asmMetaPtr)
 {
     if (asmMetaPtr != IntPtr.Zero)
     {
         ASSEMBLYMETADATA assemblymetadata = (ASSEMBLYMETADATA)Marshal.PtrToStructure(asmMetaPtr, typeof(ASSEMBLYMETADATA));
         Marshal.FreeCoTaskMem(assemblymetadata.rpLocale);
         Marshal.DestroyStructure(asmMetaPtr, typeof(ASSEMBLYMETADATA));
         Marshal.FreeCoTaskMem(asmMetaPtr);
     }
 }
 /// <summary>
 /// Free the assembly metadata structure.
 /// </summary>
 /// <param name="asmMetaPtr">The pointer.</param>
 private void FreeAsmMeta(IntPtr asmMetaPtr)
 {
     if (asmMetaPtr != IntPtr.Zero)
     {
         // Marshal the assembly metadata back to a managed type.
         ASSEMBLYMETADATA asmMeta = (ASSEMBLYMETADATA)Marshal.PtrToStructure(asmMetaPtr, typeof(ASSEMBLYMETADATA));
         // Free unmanaged memory.
         Marshal.FreeCoTaskMem(asmMeta.rpLocale);
         Marshal.DestroyStructure(asmMetaPtr, typeof(ASSEMBLYMETADATA));
         Marshal.FreeCoTaskMem(asmMetaPtr);
     }
 }
示例#4
0
        /// <summary>
        /// Construct assembly name.
        /// </summary>
        /// <param name="asmMetaPtr">Assembly metadata structure</param>
        /// <param name="asmNameBuf">Buffer containing the name</param>
        /// <param name="asmNameLength">Length of that buffer</param>
        /// <param name="pubKeyPtr">Pointer to public key</param>
        /// <param name="pubKeyBytes">Count of bytes in public key.</param>
        /// <param name="flags">Extra flags</param>
        /// <returns>The assembly name.</returns>
        private static AssemblyNameExtension ConstructAssemblyName(IntPtr asmMetaPtr, char[] asmNameBuf, UInt32 asmNameLength, IntPtr pubKeyPtr, UInt32 pubKeyBytes, UInt32 flags)
        {
            // Marshal the assembly metadata back to a managed type.
            ASSEMBLYMETADATA asmMeta = (ASSEMBLYMETADATA)Marshal.PtrToStructure(asmMetaPtr, typeof(ASSEMBLYMETADATA));

            // Construct the assembly name. (Note asmNameLength should/must be > 0.)
            var assemblyName = new AssemblyName
            {
                Name    = new string(asmNameBuf, 0, (int)asmNameLength - 1),
                Version = new Version(
                    asmMeta.usMajorVersion,
                    asmMeta.usMinorVersion,
                    asmMeta.usBuildNumber,
                    asmMeta.usRevisionNumber)
            };

            // Set culture info.
            string locale = Marshal.PtrToStringUni(asmMeta.rpLocale);

            if (locale.Length > 0)
            {
                assemblyName.CultureInfo = CultureInfo.CreateSpecificCulture(locale);
            }
            else
            {
                assemblyName.CultureInfo = CultureInfo.CreateSpecificCulture(String.Empty);
            }

            // Set public key or PKT.
            var publicKey = new byte[pubKeyBytes];

            Marshal.Copy(pubKeyPtr, publicKey, 0, (int)pubKeyBytes);
            if ((flags & (uint)CorAssemblyFlags.afPublicKey) != 0)
            {
                assemblyName.SetPublicKey(publicKey);
            }
            else
            {
                assemblyName.SetPublicKeyToken(publicKey);
            }

            assemblyName.Flags = (AssemblyNameFlags)flags;
            return(new AssemblyNameExtension(assemblyName));
        }