コード例 #1
0
ファイル: IconExtractor.cs プロジェクト: ficnar/TcBuild
        public static bool ExtractIconFromExecutable(FileInfo sourceFile, FileInfo targetFile)
        {
            IntPtr hModule = LoadLibraryEx(sourceFile.FullName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);

            if (hModule == IntPtr.Zero)
            {
                throw new FileNotFoundException($"Library {sourceFile.Name} not found.", sourceFile.FullName);
            }

            ENUMRESNAMEPROC callback = (h, t, name, l) =>
            {
                var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name);

                // Calculate the size of an entire .icon file.

                int count = BitConverter.ToUInt16(dir, 4);  // GRPICONDIR.idCount
                int len   = 6 + 16 * count;                 // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
                for (int i = 0; i < count; ++i)
                {
                    len += BitConverter.ToInt32(dir, 6 + 14 * i + 8);   // GRPICONDIRENTRY.dwBytesInRes
                }
                using (FileStream targetStream = targetFile.Create())
                    using (var dst = new BinaryWriter(targetStream))
                    {
                        // Copy GRPICONDIR to ICONDIR.

                        dst.Write(dir, 0, 6);

                        int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count

                        for (int i = 0; i < count; ++i)
                        {
                            // Load the picture.

                            ushort id  = BitConverter.ToUInt16(dir, 6 + 14 * i + 12); // GRPICONDIRENTRY.nID
                            var    pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id);

                            // Copy GRPICONDIRENTRY to ICONDIRENTRY.

                            dst.Seek(6 + 16 * i, 0);

                            dst.Write(dir, 6 + 14 * i, 8); // First 8bytes are identical.
                            dst.Write(pic.Length);         // ICONDIRENTRY.dwBytesInRes
                            dst.Write(picOffset);          // ICONDIRENTRY.dwImageOffset

                            // Copy a picture.

                            dst.Seek(picOffset, 0);
                            dst.Write(pic, 0, pic.Length);

                            picOffset += pic.Length;
                        }
                    }
                return(true);
            };
            bool ok = EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero);

            FreeLibrary(hModule);
            return(ok);
        }
コード例 #2
0
        private static void Load(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            Icons = null;
            IntPtr hModule = IntPtr.Zero;

            try
            {
                hModule = LoadLibraryEx(fileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
                if (hModule == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }

                List <byte[]> tmpData = new List <byte[]>();

                ENUMRESNAMEPROC callback = (h, t, name, l) =>
                {
                    byte[] iconBuffer = GetDataFromResource(hModule, RT_GROUP_ICON, name);
                    tmpData.Add(iconBuffer);
                    return(true);
                };
                EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero);
                Icons = new IconBitmapDecoder[tmpData.Count];
                for (int i = 0; i < tmpData.Count; i++)
                {
                    MemoryStream mem = new MemoryStream(tmpData[i]);
                    Icons[i] = new IconBitmapDecoder();
                    Icons[i].Open(mem);
                }
            }
            finally
            {
                if (hModule != IntPtr.Zero)
                {
                    FreeLibrary(hModule);
                }
            }
        }
コード例 #3
0
ファイル: Methods.cs プロジェクト: shijiaxing/Win32Interop
 public static extern bool EnumResourceNames(
     [In] IntPtr hModule, [In] [MarshalAs(UnmanagedType.LPTStr)] string lpType, ENUMRESNAMEPROC lpEnumFunc, [MarshalAs(UnmanagedType.SysInt)] int lParam);
コード例 #4
0
ファイル: IconExtractor.cs プロジェクト: wuhan890809/Playnite
        private void Initialize(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            IntPtr hModule = IntPtr.Zero;

            try
            {
                hModule = Kernel32.LoadLibraryEx(fileName, IntPtr.Zero, LOAD_LIBRARY_AS_IMAGE_RESOURCE);
                if (hModule == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }

                FileName = GetFileName(hModule);

                // Enumerate the icon resource and build .ico files in memory.

                var tmpData = new List <byte[]>();

                ENUMRESNAMEPROC callback = (h, t, name, l) =>
                {
                    // Refer the following URL for the data structures used here:
                    // http://msdn.microsoft.com/en-us/library/ms997538.aspx

                    // RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's.

                    var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name);

                    // Calculate the size of an entire .icon file.

                    int count = BitConverter.ToUInt16(dir, 4);  // GRPICONDIR.idCount
                    int len   = 6 + 16 * count;                 // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
                    for (int i = 0; i < count; ++i)
                    {
                        len += BitConverter.ToInt32(dir, 6 + 14 * i + 8);   // GRPICONDIRENTRY.dwBytesInRes
                    }
                    using (var dst = new BinaryWriter(new MemoryStream(len)))
                    {
                        // Copy GRPICONDIR to ICONDIR.

                        dst.Write(dir, 0, 6);

                        int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count

                        for (int i = 0; i < count; ++i)
                        {
                            // Load the picture.

                            ushort id  = BitConverter.ToUInt16(dir, 6 + 14 * i + 12);   // GRPICONDIRENTRY.nID
                            var    pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id);

                            // Copy GRPICONDIRENTRY to ICONDIRENTRY.

                            dst.Seek(6 + 16 * i, SeekOrigin.Begin);

                            dst.Write(dir, 6 + 14 * i, 8);  // First 8bytes are identical.
                            dst.Write(pic.Length);          // ICONDIRENTRY.dwBytesInRes
                            dst.Write(picOffset);           // ICONDIRENTRY.dwImageOffset

                            // Copy a picture.

                            dst.Seek(picOffset, SeekOrigin.Begin);
                            dst.Write(pic, 0, pic.Length);

                            picOffset += pic.Length;
                        }

                        tmpData.Add(((MemoryStream)dst.BaseStream).ToArray());
                    }

                    return(true);
                };
                Kernel32.EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero);

                iconData = tmpData.ToArray();
            }
            finally
            {
                if (hModule != IntPtr.Zero)
                {
                    Kernel32.FreeLibrary(hModule);
                }
            }
        }
コード例 #5
0
 public static extern bool EnumResourceNames(IntPtr hinst, IntPtr nType, ENUMRESNAMEPROC callback, IntPtr lParam);
コード例 #6
0
 public static extern bool EnumResourceNames(IntPtr hModule, IntPtr lpszType, ENUMRESNAMEPROC lpEnumFunc, IntPtr lParam);
コード例 #7
0
ファイル: NativeMethods.cs プロジェクト: johnx7271/OpenGAX
 public static extern int EnumResourceNames(IntPtr hModule, RT rtType, ENUMRESNAMEPROC lpEnumFunc, IntPtr lParam);
コード例 #8
0
ファイル: NativeMethods.cs プロジェクト: cems1111/NRegFreeCom
 public static extern bool EnumResourceNames(
   IntPtr hModule,
   uint lpszType,
   ENUMRESNAMEPROC lpEnumFunc,
   IntPtr lParam);
コード例 #9
0
        public static Stream ExtractIcon(string FileName, int Index)
        {
            MemoryStream output = null;

            byte[] iconBuffer = null;
            if (FileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            Icons = null;
            IntPtr hModule = IntPtr.Zero;

            try
            {
                hModule = LoadLibraryEx(FileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
                if (hModule == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                int             counter  = 0;
                ENUMRESNAMEPROC callback = (h, t, name, l) =>
                {
                    if (counter == Index)
                    {
                        byte[] data  = GetDataFromResource(hModule, RT_GROUP_ICON, name);
                        int    count = BitConverter.ToUInt16(data, 4); // GRPICONDIR.idCount
                        int    len   = 6 + 16 * count;                 // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
                        for (int i = 0; i < count; ++i)
                        {
                            len += BitConverter.ToInt32(data, 6 + 14 * i + 8);   // GRPICONDIRENTRY.dwBytesInRes
                        }
                        using (var dst = new BinaryWriter(new MemoryStream(len)))
                        {
                            // Copy GRPICONDIR to ICONDIR.
                            dst.Write(data, 0, 6);
                            int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
                            for (int i = 0; i < count; ++i)
                            {
                                // Load the picture.
                                ushort id  = BitConverter.ToUInt16(data, 6 + 14 * i + 12);   // GRPICONDIRENTRY.nID
                                var    pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id);
                                // Copy GRPICONDIRENTRY to ICONDIRENTRY.
                                dst.Seek(6 + 16 * i, SeekOrigin.Begin);
                                dst.Write(data, 6 + 14 * i, 8); // First 8bytes are identical.
                                dst.Write(pic.Length);          // ICONDIRENTRY.dwBytesInRes
                                dst.Write(picOffset);           // ICONDIRENTRY.dwImageOffset
                                // Copy a picture.
                                dst.Seek(picOffset, SeekOrigin.Begin);
                                dst.Write(pic, 0, pic.Length);
                                picOffset += pic.Length;
                            }
                            iconBuffer = (((MemoryStream)dst.BaseStream).ToArray());
                        }
                    }
                    counter += 1;
                    return(true);
                };
                EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero);
            }
            finally
            {
                if (hModule != IntPtr.Zero)
                {
                    FreeLibrary(hModule);
                }
            }
            if (iconBuffer != null)
            {
                MemoryStream iconStream = new MemoryStream(iconBuffer);
                output = new MemoryStream(iconBuffer);
            }
            return(output);
        }
コード例 #10
0
        public static List <Icon> LoadIcon(string fileName)
        {
            IntPtr hModule   = IntPtr.Zero;
            var    iconDatas = new List <byte[]>();

            byte[] GetDataFromResource(IntPtr hmodule, IntPtr type, IntPtr name)
            {
                IntPtr hResInfo = FindResource(hmodule, name, type);
                IntPtr hResData = LoadResource(hmodule, hResInfo);
                IntPtr pResData = LockResource(hResData);
                uint   size     = SizeofResource(hmodule, hResInfo);
                var    bytes    = new byte[size];

                Marshal.Copy(pResData, bytes, 0, bytes.Length);
                return(bytes);
            }

            try
            {
                hModule = LoadLibraryEx(fileName, IntPtr.Zero, 0x00000002);
                ENUMRESNAMEPROC callback = (h, t, name, l) => {
                    var dir   = GetDataFromResource(hModule, (IntPtr)14, name);
                    int count = BitConverter.ToUInt16(dir, 4);
                    int len   = 6 + 16 * count;
                    for (int i = 0; i < count; ++i)
                    {
                        len += BitConverter.ToInt32(dir, 6 + 14 * i + 8);
                    }

                    using (var dst = new BinaryWriter(new MemoryStream(len)))
                    {
                        dst.Write(dir, 0, 6);
                        int picOffset = 6 + 16 * count;
                        for (int i = 0; i < count; ++i)
                        {
                            ushort id  = BitConverter.ToUInt16(dir, 6 + 14 * i + 12);
                            var    pic = GetDataFromResource(hModule, (IntPtr)3, (IntPtr)id);
                            dst.Seek(6 + 16 * i, SeekOrigin.Begin);
                            dst.Write(dir, 6 + 14 * i, 8);
                            dst.Write(pic.Length);
                            dst.Write(picOffset);
                            dst.Seek(picOffset, SeekOrigin.Begin);
                            dst.Write(pic, 0, pic.Length);
                            picOffset += pic.Length;
                        }
                        iconDatas.Add(((MemoryStream)dst.BaseStream).ToArray());
                    }
                    return(true);
                };
                EnumResourceNames(hModule, (IntPtr)14, callback, IntPtr.Zero);
            }
            finally
            {
                if (hModule != IntPtr.Zero)
                {
                    FreeLibrary(hModule);
                }
            }

            var icons = new List <Icon>();

            for (int i = 0; i < iconDatas.Count; i++)
            {
                using (var ms = new MemoryStream(iconDatas[i]))
                {
                    icons.Add(new Icon(ms));
                }
            }
            return(icons);
        }