GetVersionInfoForCodePage() private method

private GetVersionInfoForCodePage ( IntPtr memIntPtr, string codepage ) : bool
memIntPtr IntPtr
codepage string
return bool
Esempio n. 1
0
        /// <summary>
        /// Returns a System.Windows.Forms.FileVersionInfo representing the version information associated with the specified file.
        /// </summary>
        public unsafe static FileVersionInfo GetVersionInfo(string fileName)
        {
            // Check for the existence of the file. File.Exists returns false
            // if Read permission is denied.
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }

            uint handle;  // This variable is not used, but we need an out variable.
            uint infoSize = Interop.mincore.GetFileVersionInfoSizeEx(
                (uint)Interop.Constants.FileVerGetLocalised, fileName, out handle);
            FileVersionInfo versionInfo = new FileVersionInfo(fileName);

            if (infoSize != 0)
            {
                byte[] mem = new byte[infoSize];
                fixed(byte *memPtr = mem)
                {
                    IntPtr memIntPtr = new IntPtr((void *)memPtr);

                    if (Interop.mincore.GetFileVersionInfoEx(
                            (uint)Interop.Constants.FileVerGetLocalised | (uint)Interop.Constants.FileVerGetNeutral,
                            fileName,
                            0U,
                            infoSize,
                            memIntPtr))
                    {
                        uint langid = GetVarEntry(memIntPtr);
                        if (!versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(langid)))
                        {
                            // Some dlls might not contain correct codepage information. In this case we will fail during lookup.
                            // Explorer will take a few shots in dark by trying following ID:
                            //
                            // 040904B0 // US English + CP_UNICODE
                            // 040904E4 // US English + CP_USASCII
                            // 04090000 // US English + unknown codepage
                            // Explorer also randomly guess 041D04B0=Swedish+CP_UNICODE and 040704B0=German+CP_UNICODE) sometimes.
                            // We will try to simulate similiar behavior here.
                            uint[] ids = new uint[] { 0x040904B0, 0x040904E4, 0x04090000 };
                            foreach (uint id in ids)
                            {
                                if (id != langid)
                                {
                                    if (versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(id)))
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(versionInfo);
        }
        public static unsafe FileVersionInfo GetVersionInfo(string fileName)
        {
            int num;

            if (!File.Exists(fileName))
            {
                string fullPathWithAssert = GetFullPathWithAssert(fileName);
                new FileIOPermission(FileIOPermissionAccess.Read, fullPathWithAssert).Demand();
                throw new FileNotFoundException(fileName);
            }
            int             fileVersionInfoSize = Microsoft.Win32.UnsafeNativeMethods.GetFileVersionInfoSize(fileName, out num);
            FileVersionInfo info = new FileVersionInfo(fileName);

            if (fileVersionInfoSize != 0)
            {
                byte[] buffer = new byte[fileVersionInfoSize];
                fixed(byte *numRef = buffer)
                {
                    IntPtr handle = new IntPtr((void *)numRef);

                    if (!Microsoft.Win32.UnsafeNativeMethods.GetFileVersionInfo(fileName, 0, fileVersionInfoSize, new HandleRef(null, handle)))
                    {
                        return(info);
                    }
                    int varEntry = GetVarEntry(handle);

                    if (!info.GetVersionInfoForCodePage(handle, ConvertTo8DigitHex(varEntry)))
                    {
                        int[] numArray = new int[] { 0x40904b0, 0x40904e4, 0x4090000 };
                        foreach (int num4 in numArray)
                        {
                            if ((num4 != varEntry) && info.GetVersionInfoForCodePage(handle, ConvertTo8DigitHex(num4)))
                            {
                                return(info);
                            }
                        }
                    }
                }
            }
            return(info);
        }
Esempio n. 3
0
        /// <summary>
        /// Returns a System.Windows.Forms.FileVersionInfo representing the version information associated with the specified file.
        /// </summary>
        public unsafe static FileVersionInfo GetVersionInfo(string fileName)
        {
            // Check for the existence of the file. File.Exists returns false
            // if Read permission is denied.
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }

            uint handle;  // This variable is not used, but we need an out variable.
            uint infoSize = Interop.mincore.GetFileVersionInfoSizeEx(
                (uint)Interop.Constants.FileVerGetLocalised, fileName, out handle);
            FileVersionInfo versionInfo = new FileVersionInfo(fileName);

            if (infoSize != 0)
            {
                byte[] mem = new byte[infoSize];
                fixed (byte* memPtr = mem)
                {
                    IntPtr memIntPtr = new IntPtr((void*)memPtr);
                    if (Interop.mincore.GetFileVersionInfoEx(
                            (uint)Interop.Constants.FileVerGetLocalised | (uint)Interop.Constants.FileVerGetNeutral,
                            fileName,
                            0U,
                            infoSize,
                            memIntPtr))
                    {
                        uint langid = GetVarEntry(memIntPtr);
                        if (!versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(langid)))
                        {
                            // Some dlls might not contain correct codepage information. In this case we will fail during lookup.
                            // Explorer will take a few shots in dark by trying following lang-codepages:
                            //
                            // 040904B0 // US English + CP_UNICODE
                            // 040904E4 // US English + CP_USASCII
                            // 04090000 // US English + unknown codepage
                            // Explorer also randomly guesses 041D04B0=Swedish+CP_UNICODE and 040704B0=German+CP_UNICODE sometimes.
                            // We will try to simulate similar behavior here.
                            foreach (uint id in s_fallbackLanguageCodePages)
                            {
                                if (id != langid)
                                {
                                    if (versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(id)))
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return versionInfo;
        }
        public unsafe static FileVersionInfo GetVersionInfo(string fileName) {
            // Check for the existence of the file. File.Exists returns false
            // if Read permission is denied.
            if (!File.Exists(fileName)) {
                // 
                // The previous version of this code in the success case would require 
                // one imperative Assert for PathDiscovery permission, one Demand for 
                // PathDiscovery permission (blocked by the Assert), and 2 demands for
                // Read permission. It turns out that File.Exists does a demand for 
                // Read permission, so in the success case, we only need to do a single Demand. 
                // In the success case, this change increases the performance of this
                // function dramatically.
                // 
                // In the failure case, we want to remain backwardly compatible by throwing 
                // a SecurityException in the case where Read access is denied 
                // (it can be argued that this is less secure than throwing a FileNotFoundException, 
                // but perhaps not so much as to be worth a breaking change).
                // File.Exists eats a SecurityException, so we need to Demand for it
                // here. Since performance in the failure case is not crucial, as an
                // exception will be thrown anyway, we do a Demand for Read access.
                // If that does not throw an exception, then we will throw a FileNotFoundException.
                //
                // We also change the code to do a declarative Assert for PathDiscovery,
                // as that performs much better than an imperative Assert.
                //
                string fullPath = GetFullPathWithAssert(fileName);
                new FileIOPermission(FileIOPermissionAccess.Read, fullPath).Demand();
                throw new FileNotFoundException(fileName);
            }

            int handle;  // This variable is not used, but we need an out variable.
            int infoSize = UnsafeNativeMethods.GetFileVersionInfoSize(fileName, out handle);
            FileVersionInfo versionInfo = new FileVersionInfo(fileName);

            if (infoSize != 0) {
                byte[] mem = new byte[infoSize];
                fixed (byte* memPtr = mem) {
                    IntPtr memIntPtr = new IntPtr((void*) memPtr);                    
                    if (UnsafeNativeMethods.GetFileVersionInfo(fileName, 0, infoSize, new HandleRef(null, memIntPtr))) {
                        int langid = GetVarEntry(memIntPtr);
                        if( !versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(langid))) {
                            // Some dlls might not contain correct codepage information. In this case we will fail during lookup. 
                            // Explorer will take a few shots in dark by trying following ID:
                            //
                            // 040904B0 // US English + CP_UNICODE
                            // 040904E4 // US English + CP_USASCII
                            // 04090000 // US English + unknown codepage
                            // Explorer also randomly guess 041D04B0=Swedish+CP_UNICODE and 040704B0=German+CP_UNICODE) sometimes.
                            // We will try to simulate similiar behavior here.            
                            int[] ids = new int[] {0x040904B0, 0x040904E4, 0x04090000};
                            foreach( int id in ids) {
                                if( id != langid) { 
                                    if(versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(id))) {
                                        break;
                                    }
                                }                               
                            }
                        }

                    }
                }
            }
            return versionInfo;         
        }
Esempio n. 5
0
        public unsafe static FileVersionInfo GetVersionInfo(string fileName)
        {
            // Check for the existence of the file. File.Exists returns false
            // if Read permission is denied.
            if (!File.Exists(fileName))
            {
                //
                // The previous version of this code in the success case would require
                // one imperative Assert for PathDiscovery permission, one Demand for
                // PathDiscovery permission (blocked by the Assert), and 2 demands for
                // Read permission. It turns out that File.Exists does a demand for
                // Read permission, so in the success case, we only need to do a single Demand.
                // In the success case, this change increases the performance of this
                // function dramatically.
                //
                // In the failure case, we want to remain backwardly compatible by throwing
                // a SecurityException in the case where Read access is denied
                // (it can be argued that this is less secure than throwing a FileNotFoundException,
                // but perhaps not so much as to be worth a breaking change).
                // File.Exists eats a SecurityException, so we need to Demand for it
                // here. Since performance in the failure case is not crucial, as an
                // exception will be thrown anyway, we do a Demand for Read access.
                // If that does not throw an exception, then we will throw a FileNotFoundException.
                //
                // We also change the code to do a declarative Assert for PathDiscovery,
                // as that performs much better than an imperative Assert.
                //
                string fullPath = GetFullPathWithAssert(fileName);
                new FileIOPermission(FileIOPermissionAccess.Read, fullPath).Demand();
                throw new FileNotFoundException(fileName);
            }

            int             handle; // This variable is not used, but we need an out variable.
            int             infoSize    = UnsafeNativeMethods.GetFileVersionInfoSize(fileName, out handle);
            FileVersionInfo versionInfo = new FileVersionInfo(fileName);

            if (infoSize != 0)
            {
                byte[] mem = new byte[infoSize];
                fixed(byte *memPtr = mem)
                {
                    IntPtr memIntPtr = new IntPtr((void *)memPtr);

                    if (UnsafeNativeMethods.GetFileVersionInfo(fileName, 0, infoSize, new HandleRef(null, memIntPtr)))
                    {
                        int langid = GetVarEntry(memIntPtr);
                        if (!versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(langid)))
                        {
                            // Some dlls might not contain correct codepage information. In this case we will fail during lookup.
                            // Explorer will take a few shots in dark by trying following ID:
                            //
                            // 040904B0 // US English + CP_UNICODE
                            // 040904E4 // US English + CP_USASCII
                            // 04090000 // US English + unknown codepage
                            // Explorer also randomly guess 041D04B0=Swedish+CP_UNICODE and 040704B0=German+CP_UNICODE) sometimes.
                            // We will try to simulate similiar behavior here.
                            int[] ids = new int[] { 0x040904B0, 0x040904E4, 0x04090000 };
                            foreach (int id in ids)
                            {
                                if (id != langid)
                                {
                                    if (versionInfo.GetVersionInfoForCodePage(memIntPtr, ConvertTo8DigitHex(id)))
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(versionInfo);
        }
 public static unsafe FileVersionInfo GetVersionInfo(string fileName)
 {
     int num;
     if (!File.Exists(fileName))
     {
         string fullPathWithAssert = GetFullPathWithAssert(fileName);
         new FileIOPermission(FileIOPermissionAccess.Read, fullPathWithAssert).Demand();
         throw new FileNotFoundException(fileName);
     }
     int fileVersionInfoSize = Microsoft.Win32.UnsafeNativeMethods.GetFileVersionInfoSize(fileName, out num);
     FileVersionInfo info = new FileVersionInfo(fileName);
     if (fileVersionInfoSize != 0)
     {
         byte[] buffer = new byte[fileVersionInfoSize];
         fixed (byte* numRef = buffer)
         {
             IntPtr handle = new IntPtr((void*) numRef);
             if (!Microsoft.Win32.UnsafeNativeMethods.GetFileVersionInfo(fileName, 0, fileVersionInfoSize, new HandleRef(null, handle)))
             {
                 return info;
             }
             int varEntry = GetVarEntry(handle);
             if (!info.GetVersionInfoForCodePage(handle, ConvertTo8DigitHex(varEntry)))
             {
                 int[] numArray = new int[] { 0x40904b0, 0x40904e4, 0x4090000 };
                 foreach (int num4 in numArray)
                 {
                     if ((num4 != varEntry) && info.GetVersionInfoForCodePage(handle, ConvertTo8DigitHex(num4)))
                     {
                         return info;
                     }
                 }
             }
         }
     }
     return info;
 }