IconExtractor.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * IconExtractor/IconUtil for .NET
  3. * Copyright (C) 2014 Tsuda Kageyu. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
  19. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.IO;
  32. using System.Runtime.InteropServices;
  33. using System.Text;
  34. namespace Aio1Ef.Packer.Common.ExternalCodes
  35. {
  36. public class IconExtractor
  37. {
  38. ////////////////////////////////////////////////////////////////////////
  39. // Constants
  40. // Flags for LoadLibraryEx().
  41. private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
  42. // Resource types for EnumResourceNames().
  43. private readonly static IntPtr RT_ICON = (IntPtr)3;
  44. private readonly static IntPtr RT_GROUP_ICON = (IntPtr)14;
  45. private const int MAX_PATH = 260;
  46. ////////////////////////////////////////////////////////////////////////
  47. // Fields
  48. private byte[][] iconData = null; // Binary data of each icon.
  49. ////////////////////////////////////////////////////////////////////////
  50. // Public properties
  51. /// <summary>
  52. /// Gets the full path of the associated file.
  53. /// </summary>
  54. public string FileName
  55. {
  56. get;
  57. private set;
  58. }
  59. /// <summary>
  60. /// Gets the count of the icons in the associated file.
  61. /// </summary>
  62. public int Count
  63. {
  64. get { return iconData.Length; }
  65. }
  66. /// <summary>
  67. /// Initializes a new instance of the IconExtractor class from the specified file name.
  68. /// </summary>
  69. /// <param name="fileName">The file to extract icons from.</param>
  70. public IconExtractor(string fileName)
  71. {
  72. Initialize(fileName);
  73. }
  74. /// <summary>
  75. /// Extracts an icon from the file.
  76. /// </summary>
  77. /// <param name="index">Zero based index of the icon to be extracted.</param>
  78. /// <returns>A System.Drawing.Icon object.</returns>
  79. /// <remarks>Always returns new copy of the Icon. It should be disposed by the user.</remarks>
  80. public Icon GetIcon(int index)
  81. {
  82. if (index < 0 || Count <= index)
  83. throw new ArgumentOutOfRangeException("index");
  84. // Create an Icon based on a .ico file in memory.
  85. using (var ms = new MemoryStream(iconData[index]))
  86. {
  87. return new Icon(ms);
  88. }
  89. }
  90. /// <summary>
  91. /// Extracts all the icons from the file.
  92. /// </summary>
  93. /// <returns>An array of System.Drawing.Icon objects.</returns>
  94. /// <remarks>Always returns new copies of the Icons. They should be disposed by the user.</remarks>
  95. public Icon[] GetAllIcons()
  96. {
  97. var icons = new List<Icon>();
  98. for (int i = 0; i < Count; ++i)
  99. icons.Add(GetIcon(i));
  100. return icons.ToArray();
  101. }
  102. private void Initialize(string fileName)
  103. {
  104. if (fileName == null)
  105. throw new ArgumentNullException("fileName");
  106. IntPtr hModule = IntPtr.Zero;
  107. try
  108. {
  109. hModule = NativeMethods.LoadLibraryEx(fileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
  110. if (hModule == IntPtr.Zero)
  111. throw new Win32Exception();
  112. FileName = GetFileName(hModule);
  113. // Enumerate the icon resource and build .ico files in memory.
  114. var tmpData = new List<byte[]>();
  115. ENUMRESNAMEPROC callback = (h, t, name, l) =>
  116. {
  117. // Refer the following URL for the data structures used here:
  118. // http://msdn.microsoft.com/en-us/library/ms997538.aspx
  119. // RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's.
  120. var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name);
  121. // Calculate the size of an entire .icon file.
  122. int count = BitConverter.ToUInt16(dir, 4); // GRPICONDIR.idCount
  123. int len = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
  124. for (int i = 0; i < count; ++i)
  125. len += BitConverter.ToInt32(dir, 6 + 14 * i + 8); // GRPICONDIRENTRY.dwBytesInRes
  126. using (var dst = new BinaryWriter(new MemoryStream(len)))
  127. {
  128. // Copy GRPICONDIR to ICONDIR.
  129. dst.Write(dir, 0, 6);
  130. int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
  131. for (int i = 0; i < count; ++i)
  132. {
  133. // Load the picture.
  134. ushort id = BitConverter.ToUInt16(dir, 6 + 14 * i + 12); // GRPICONDIRENTRY.nID
  135. var pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id);
  136. // Copy GRPICONDIRENTRY to ICONDIRENTRY.
  137. dst.Seek(6 + 16 * i, SeekOrigin.Begin);
  138. dst.Write(dir, 6 + 14 * i, 8); // First 8bytes are identical.
  139. dst.Write(pic.Length); // ICONDIRENTRY.dwBytesInRes
  140. dst.Write(picOffset); // ICONDIRENTRY.dwImageOffset
  141. // Copy a picture.
  142. dst.Seek(picOffset, SeekOrigin.Begin);
  143. dst.Write(pic, 0, pic.Length);
  144. picOffset += pic.Length;
  145. }
  146. tmpData.Add(((MemoryStream)dst.BaseStream).ToArray());
  147. }
  148. return true;
  149. };
  150. NativeMethods.EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero);
  151. iconData = tmpData.ToArray();
  152. }
  153. finally
  154. {
  155. if (hModule != IntPtr.Zero)
  156. NativeMethods.FreeLibrary(hModule);
  157. }
  158. }
  159. private byte[] GetDataFromResource(IntPtr hModule, IntPtr type, IntPtr name)
  160. {
  161. // Load the binary data from the specified resource.
  162. IntPtr hResInfo = NativeMethods.FindResource(hModule, name, type);
  163. if (hResInfo == IntPtr.Zero)
  164. throw new Win32Exception();
  165. IntPtr hResData = NativeMethods.LoadResource(hModule, hResInfo);
  166. if (hResData == IntPtr.Zero)
  167. throw new Win32Exception();
  168. IntPtr pResData = NativeMethods.LockResource(hResData);
  169. if (pResData == IntPtr.Zero)
  170. throw new Win32Exception();
  171. uint size = NativeMethods.SizeofResource(hModule, hResInfo);
  172. if (size == 0)
  173. throw new Win32Exception();
  174. byte[] buf = new byte[size];
  175. Marshal.Copy(pResData, buf, 0, buf.Length);
  176. return buf;
  177. }
  178. private string GetFileName(IntPtr hModule)
  179. {
  180. // Alternative to GetModuleFileName() for the module loaded with
  181. // LOAD_LIBRARY_AS_DATAFILE option.
  182. // Get the file name in the format like:
  183. // "\\Device\\HarddiskVolume2\\Windows\\System32\\shell32.dll"
  184. string fileName;
  185. {
  186. var buf = new StringBuilder(MAX_PATH);
  187. int len = NativeMethods.GetMappedFileName(
  188. NativeMethods.GetCurrentProcess(), hModule, buf, buf.Capacity);
  189. if (len == 0)
  190. throw new Win32Exception();
  191. fileName = buf.ToString();
  192. }
  193. // Convert the device name to drive name like:
  194. // "C:\\Windows\\System32\\shell32.dll"
  195. for (char c = 'A'; c <= 'Z'; ++c)
  196. {
  197. var drive = c + ":";
  198. var buf = new StringBuilder(MAX_PATH);
  199. int len = NativeMethods.QueryDosDevice(drive, buf, buf.Capacity);
  200. if (len == 0)
  201. continue;
  202. var devPath = buf.ToString();
  203. if (fileName.StartsWith(devPath))
  204. return (drive + fileName.Substring(devPath.Length));
  205. }
  206. return fileName;
  207. }
  208. }
  209. }