internal static unsafe byte[] GetPropertyBytes(IAssemblyName nameObject, PropertyId propertyId)
        {
            int  hr;
            uint size = 0;

            hr = nameObject.GetProperty(propertyId, null, ref size);
            if (hr == 0)
            {
                return(null);
            }

            if (hr != ERROR_INSUFFICIENT_BUFFER)
            {
                throw Marshal.GetExceptionForHR(hr);
            }

            byte[] data = new byte[(int)size];
            fixed(byte *p = data)
            {
                hr = nameObject.GetProperty(propertyId, p, ref size);
                if (hr != 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }

            return(data);
        }
        internal static unsafe bool IsKeyOrTokenEmpty(IAssemblyName nameObject, PropertyId propertyId)
        {
            Debug.Assert(propertyId == PropertyId.NULL_PUBLIC_KEY_TOKEN || propertyId == PropertyId.NULL_PUBLIC_KEY);
            uint size = 0;
            int  hr   = nameObject.GetProperty(propertyId, null, ref size);

            return(hr == 0);
        }
Beispiel #3
0
        internal string GetStringProperty(IAssemblyName name, ASM_NAME propertyName)
        {
            uint   bufferSize = BufferLength;
            IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

            name.GetProperty(propertyName, buffer, ref bufferSize);
            var stringVaule = Marshal.PtrToStringUni(buffer, (int)bufferSize);

            Marshal.FreeHGlobal(buffer);
            return(stringVaule);
        }
Beispiel #4
0
            static CultureInfo GetCulture(IAssemblyName name)
            {
                uint   bufferSize = 255;
                IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

                name.GetProperty(ASM_NAME.ASM_NAME_CULTURE, buffer, ref bufferSize);
                string result = Marshal.PtrToStringAuto(buffer);

                Marshal.FreeHGlobal(buffer);
                return(new CultureInfo(result));
            }
Beispiel #5
0
        public static CultureInfo GetCulture([NotNull] IAssemblyName name)
        {
            uint   bufferSize = 255;
            IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

            name.GetProperty(AsmName.AsmNameCulture, buffer, ref bufferSize);
            string result = Marshal.PtrToStringAuto(buffer);

            Marshal.FreeHGlobal(buffer);
            return(result == null ? CultureInfoHelper.Default : new CultureInfo(result));
        }
Beispiel #6
0
        internal UInt16 GetShortProperty(IAssemblyName name, ASM_NAME propertyName)
        {
            uint   bufferSize = 512;
            IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

            name.GetProperty(propertyName, buffer, ref bufferSize);
            byte low  = Marshal.ReadByte(buffer);
            byte high = Marshal.ReadByte(buffer, 1);

            Marshal.FreeHGlobal(buffer);
            return((UInt16)(low + (high << 8)));
        }
Beispiel #7
0
        /// <summary>
        /// Converts the value of this instance to its equivalent <see cref="AssemblyName"/>.
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <returns></returns>
        public static AssemblyName ToAssemblyName(this IAssemblyName assemblyName)
        {
            var result = new AssemblyName();

            result.Name        = assemblyName.GetName();
            result.Version     = assemblyName.GetVersion();
            result.CultureInfo = new CultureInfo(assemblyName.GetProperty <string>(AssemblyNamePropertyId.Culture));
            result.CodeBase    = assemblyName.GetProperty <string>(AssemblyNamePropertyId.CodebaseUrl);
            result.SetPublicKey(assemblyName.GetProperty <byte[]>(AssemblyNamePropertyId.PublicKey));
            result.SetPublicKeyToken(assemblyName.GetProperty <byte[]>(AssemblyNamePropertyId.PublicKeyToken));
            // Bug: The following line will always return null, why? And how to fix this?
            //  assemblyName.GetProperty<object>(AssemblyNamePropertyId.ProcessorIdArray);
            // A workaround is available by using the displayname of the IAssemblyName
            var tmp = assemblyName.GetDisplayName(DisplayNameFlags.ProcessArchitecture);

            tmp = tmp.Substring(tmp.LastIndexOf('=') + 1);
            if (Enum.IsDefined(typeof(ProcessorArchitecture), tmp))
            {
                result.ProcessorArchitecture = (ProcessorArchitecture)Enum.Parse(typeof(ProcessorArchitecture), tmp);
            }
            return(result);
        }
Beispiel #8
0
        internal byte[] GetByteArrayProperty(IAssemblyName name, ASM_NAME propertyName)
        {
            uint   bufferSize = 512;
            IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

            name.GetProperty(propertyName, buffer, ref bufferSize);
            byte[] result = new byte[bufferSize];
            for (int i = 0; i < bufferSize; i++)
            {
                result[i] = Marshal.ReadByte(buffer, i);
            }
            Marshal.FreeHGlobal(buffer);
            return(result);
        }
Beispiel #9
0
        internal UInt32 GetDwordProperty(IAssemblyName name, ASM_NAME propertyName)
        {
            uint   bufferSize = 512;
            IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

            name.GetProperty(propertyName, buffer, ref bufferSize);
            byte a = Marshal.ReadByte(buffer);
            byte b = Marshal.ReadByte(buffer, 1);
            byte c = Marshal.ReadByte(buffer);
            byte d = Marshal.ReadByte(buffer, 1);

            Marshal.FreeHGlobal(buffer);
            return((UInt32)(a + (b << 8) + (c << 16) + (d << 24)));
        }
            protected string getProperty(ASM_NAME id)
            {
                uint len = 0;

                int hr = _name.GetProperty(id, IntPtr.Zero, ref len);

                if (hr == ComUtil.ERROR_INSUFFICIENT_BUFFER && len > 0)
                {
                    IntPtr buf = Marshal.AllocCoTaskMem((int)len);

                    ComUtil.ComCheck(_name.GetProperty(id, buf, ref len));

                    string str = Marshal.PtrToStringUni(buf);

                    Marshal.FreeCoTaskMem(buf);

                    return(str);
                }
                else
                {
                    return("");
                }
            }
Beispiel #11
0
        public static byte[] GetPublicKeyToken([NotNull] IAssemblyName name)
        {
            byte[] result     = new byte[8];
            uint   bufferSize = 8;
            IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

            name.GetProperty(AsmName.AsmNamePublicKeyToken, buffer, ref bufferSize);
            for (int i = 0; i < 8; i++)
            {
                result[i] = Marshal.ReadByte(buffer, i);
            }
            Marshal.FreeHGlobal(buffer);
            return(result);
        }
Beispiel #12
0
            static byte[] GetPublicKey(IAssemblyName name)
            {
                uint   bufferSize = 512;
                IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

                name.GetProperty(ASM_NAME.ASM_NAME_PUBLIC_KEY, buffer, ref bufferSize);
                byte[] result = new byte[bufferSize];
                for (int i = 0; i < bufferSize; i++)
                {
                    result[i] = Marshal.ReadByte(buffer, i);
                }
                Marshal.FreeHGlobal(buffer);
                return(result);
            }
Beispiel #13
0
            static byte[] GetPublicKeyToken(IAssemblyName name)
            {
                byte[] result     = new byte[8];
                uint   bufferSize = 8;
                IntPtr buffer     = Marshal.AllocHGlobal((int)bufferSize);

                name.GetProperty(ASM_NAME.ASM_NAME_PUBLIC_KEY_TOKEN, buffer, ref bufferSize);
                for (int i = 0; i < 8; i++)
                {
                    result[i] = Marshal.ReadByte(buffer, i);
                }
                Marshal.FreeHGlobal(buffer);
                return(result);
            }
Beispiel #14
0
        internal static unsafe uint?GetPropertyWord(IAssemblyName nameObject, PropertyId propertyId)
        {
            uint result;
            uint size = sizeof(uint);
            int  hr   = nameObject.GetProperty(propertyId, &result, ref size);

            Marshal.ThrowExceptionForHR(hr);

            if (size == 0)
            {
                return(null);
            }

            return(result);
        }
Beispiel #15
0
        private static T GetProperty <T>(this IAssemblyName name, AssemblyNamePropertyId propertyId)
        {
            uint bufferSize    = 512;
            var  bufferPointer = Marshal.AllocHGlobal((int)bufferSize);

            try
            {
                Marshal.ThrowExceptionForHR(name.GetProperty(propertyId, bufferPointer, ref bufferSize));
                return(bufferSize > 0 // IAssemblyName.GetProperty() will always return a bufferSize greater than 0
                 ? bufferPointer.Read <T>(bufferSize)
                 : default(T));
            }
            finally
            {
                Marshal.FreeHGlobal(bufferPointer);
            }
        }
Beispiel #16
0
        /// <summary>
        ///     Gets the culture.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static CultureInfo GetCulture(IAssemblyName name)
        {
            uint bufferSize = 255;
            IntPtr buffer = Marshal.AllocHGlobal((int) bufferSize);
            name.GetProperty(ASM_NAME.ASM_NAME_CULTURE, buffer, ref bufferSize);
            string result = Marshal.PtrToStringAuto(buffer);
            Marshal.FreeHGlobal(buffer);
            if (result != null)
            {
                return new CultureInfo(result);
            }

            return null;
        }
Beispiel #17
0
 /// <summary>
 ///     Gets the public key.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <returns></returns>
 public static byte[] GetPublicKey(IAssemblyName name)
 {
     uint bufferSize = 512;
     IntPtr buffer = Marshal.AllocHGlobal((int) bufferSize);
     name.GetProperty(ASM_NAME.ASM_NAME_PUBLIC_KEY, buffer, ref bufferSize);
     var result = new byte[bufferSize];
     for (int i = 0; i < bufferSize; i++)
         result[i] = Marshal.ReadByte(buffer, i);
     Marshal.FreeHGlobal(buffer);
     return result;
 }
Beispiel #18
0
 /// <summary>
 ///     Gets the public key token.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <returns></returns>
 public static byte[] GetPublicKeyToken(IAssemblyName name)
 {
     var result = new byte[8];
     uint bufferSize = 8;
     IntPtr buffer = Marshal.AllocHGlobal((int) bufferSize);
     name.GetProperty(ASM_NAME.ASM_NAME_PUBLIC_KEY_TOKEN, buffer, ref bufferSize);
     for (int i = 0; i < 8; i++)
         result[i] = Marshal.ReadByte(buffer, i);
     Marshal.FreeHGlobal(buffer);
     return result;
 }
        internal static unsafe uint? GetPropertyWord(IAssemblyName nameObject, PropertyId propertyId)
        {
            uint result;
            uint size = sizeof(uint);
            int hr = nameObject.GetProperty(propertyId, &result, ref size);
            if (hr != 0)
            {
                throw Marshal.GetExceptionForHR(hr);
            }

            if (size == 0)
            {
                return null;
            }

            return result;
        }
Beispiel #20
0
 internal byte[] GetByteArrayProperty(IAssemblyName name, ASM_NAME propertyName)
 {
     uint bufferSize = 512;
     IntPtr buffer = Marshal.AllocHGlobal((int)bufferSize);
     name.GetProperty(propertyName, buffer, ref bufferSize);
     byte[] result = new byte[bufferSize];
     for (int i = 0; i < bufferSize; i++)
         result[i] = Marshal.ReadByte(buffer, i);
     Marshal.FreeHGlobal(buffer);
     return result;
 }
Beispiel #21
0
 internal UInt16 GetShortProperty(IAssemblyName name, ASM_NAME propertyName)
 {
     uint bufferSize = 512;
     IntPtr buffer = Marshal.AllocHGlobal((int)bufferSize);
     name.GetProperty(propertyName, buffer, ref bufferSize);
     byte low = Marshal.ReadByte(buffer);
     byte high = Marshal.ReadByte(buffer, 1);
     Marshal.FreeHGlobal(buffer);
     return (UInt16)(low + (high << 8));
 }
Beispiel #22
0
 internal UInt32 GetDwordProperty(IAssemblyName name, ASM_NAME propertyName)
 {
     uint bufferSize = 512;
     IntPtr buffer = Marshal.AllocHGlobal((int)bufferSize);
     name.GetProperty(propertyName, buffer, ref bufferSize);
     byte a = Marshal.ReadByte(buffer);
     byte b = Marshal.ReadByte(buffer, 1);
     byte c = Marshal.ReadByte(buffer);
     byte d = Marshal.ReadByte(buffer, 1);
     Marshal.FreeHGlobal(buffer);
     return (UInt32)(a + (b << 8) + (c << 16) + (d << 24));
 }
        internal static unsafe byte[] GetPropertyBytes(IAssemblyName nameObject, PropertyId propertyId)
        {
            int hr;
            uint size = 0;

            hr = nameObject.GetProperty(propertyId, null, ref size);
            if (hr == 0)
            {
                return null;
            }

            if (hr != ERROR_INSUFFICIENT_BUFFER)
            {
                throw Marshal.GetExceptionForHR(hr);
            }

            byte[] data = new byte[(int)size];
            fixed (byte* p = data)
            {
                hr = nameObject.GetProperty(propertyId, p, ref size);
                if (hr != 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }

            return data;
        }
Beispiel #24
0
 internal string GetStringProperty(IAssemblyName name, ASM_NAME propertyName)
 {
     uint bufferSize = BufferLength;
     IntPtr buffer = Marshal.AllocHGlobal((int)bufferSize);
     name.GetProperty(propertyName, buffer, ref bufferSize);
     var stringVaule = Marshal.PtrToStringUni(buffer, (int)bufferSize);
     Marshal.FreeHGlobal(buffer);
     return stringVaule;
 }
 internal static unsafe bool IsKeyOrTokenEmpty(IAssemblyName nameObject, PropertyId propertyId)
 {
     Debug.Assert(propertyId == PropertyId.NULL_PUBLIC_KEY_TOKEN || propertyId == PropertyId.NULL_PUBLIC_KEY);
     uint size = 0;
     int hr = nameObject.GetProperty(propertyId, null, ref size);
     return hr == 0;
 }