An ICL file has no structure of its own. It is a Windows executable library whose only contents are icon resources, so reading one means reading the executable’s resources and picking out two resource types.
Which container
Bytes at the 0x3C pointer | Container | Resources |
|---|---|---|
NE | 16-bit New Executable, the original ICL | the NE resource table |
PE 00 00 | 32-bit Portable Executable, from newer editors | the PE resource directory |
Every ICL starts with an MS-DOS MZ header. The 32-bit value at offset
0x3C gives the offset of the Windows header.
About code: Axialis, the maker of IconWorkshop, describes an ICL as a Windows DLL
whose code portion is empty and whose resource portion holds only icons (“What is an
Icon Library?”, read as raw HTML). In the headers, an empty code portion of an NE
library looks like this in two files examined for this page: an .icl
file with six icons, and MORICONS.DLL, the icon-only library that ships
with Windows 3.1. Both have ne_cseg (offset 0x1C, the
number of segment table entries) and
ne_cmod (offset 0x1E, the number of module references)
equal to 0, so there are no code or data segments at all. That is what was
seen in those two files; no specification states that an ICL must be built this
way, and for a 32-bit PE library no file was examined, so how its code section looks
is not established. A reader that only extracts icons, such as Wine’s
exticon.c, looks at the resource table alone and does not check for
code.
Resource types
| Type | Name | NE type value | Contents |
|---|---|---|---|
| 14 | RT_GROUP_ICON | 0x800E | one per icon: the list of its sizes and colour depths |
| 3 | RT_ICON | 0x8003 | one per image: the pixels of one size of one icon |
In an NE resource table a numbered type has its top bit set, so type 14 is stored as
0x800E. A PE resource directory stores the plain numbers. An ICL holds
these two types and normally nothing else.
Group icon resource
Each RT_GROUP_ICON resource is a GRPICONDIR: the same 6-byte
header as an ICO file’s directory, then one
14-byte entry per image. Numbers are little-endian. There is no padding: the entries follow the
6-byte header one after the other, 14 bytes each (the offsets in the tables below). The Windows 3.1 SDK text
read for this page (chapters 1 and 7) states no packing rule; Wine’s exticon.c declares its
icon structures between #pragma pack(push,1) and #pragma pack(pop), that is,
packed on 1-byte boundaries, which gives the same layout.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 2 | u16 | idReserved | 0 |
| 2 | 2 | u16 | idType | 1 for icons |
| 4 | 2 | u16 | idCount | the number of images |
| 6 | 14 × idCount | entries | idEntries | below |
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | bWidth | width in pixels; 0 means 256 |
| 1 | 1 | u8 | bHeight | height in pixels; 0 means 256 |
| 2 | 1 | u8 | bColorCount | colours for paletted images; 0 for 256 or more |
| 3 | 1 | u8 | bReserved | 0 |
| 4 | 2 | u16 | wPlanes | colour planes, 1 |
| 6 | 2 | u16 | wBitCount | bits per pixel |
| 8 | 4 | u32 | dwBytesInRes | the size of the RT_ICON resource |
| 12 | 2 | u16 | nID | the resource ID of the RT_ICON that holds this image |
The entry is the ICO file’s 16-byte entry with its last field, a 32-bit file offset, replaced by the 16-bit resource ID.
Icon resource
Each RT_ICON resource holds exactly the bytes of one image of an ICO
file: a bitmap with its height doubled to cover
the colour image and the AND mask, or, in files
for Windows Vista and later, a PNG image, usually
for 256 × 256. A 16-bit NE library normally holds only bitmaps.
Icon numbers
Programs refer to an icon in a library as a file name and a number, as in the
Change Icon dialog or a shortcut’s icon setting. Microsoft’s
ExtractIcon page (nIconIndex, raw nf-shellapi-extracticonw.md) says
the following.
- The index is “zero-based”: 0 returns the first icon in the file.
- −1 returns the total number of icons in the file; for an executable file or DLL that is “the number of
RT_GROUP_ICONresources” (for an.icofile, 1). - A negative number other than −1 returns the icon whose resource identifier equals the absolute value
(−3 for resource identifier 3). The page adds that to extract the icon whose identifier is 1, the
ExtractIconExfunction must be used: −1 cannot stand for identifier 1, because it is the count request. So identifier 1 is not reachable by a negative number.
The page does not say in what order the zero-based index counts the groups; that is under the group order below, from Wine’s code.
A group icon resource may have a number or a name. In a PE file, the resource
directory lists named entries first, then numbered entries in ascending order. Microsoft’s PE text says
all entries of a table “are sorted in ascending order: the Name entries by case-sensitive string and the ID
entries by numeric value” (“PE Format”, resource directory entries, raw pe-format.md), so the
names are in case-sensitive string order, not a locale or case-folded alphabetical order.
In an NE library
Sources for this part. Microsoft’s text: Microsoft Windows Programmer’s
Reference, Volume 4: Resources (Windows 3.1 SDK, 1992, Microsoft part number
PC30211-0492), chapter 6 “Executable-File Header Format” (6.2.3 Resource Table) and
chapter 7 “Resource Formats Within Executable Files” (7.2 Icon-Directory Resource),
read as the OCR text of the Bitsavers scan on archive.org
(PC30211-0492_Programmers_Reference_Volume_4_199204_djvu.txt); and the
knowledge-base article “INF: Executable-File Header Format” (the Windows 3.00
Developer’s Notes, archived as EXEFMT, archive.org item
exefmt). Wine’s dlls/user32/exticon.c
(ICO_ExtractIconExW, raw text) is the cross-check, because it is code
that reads real files; the OCR text has recognition errors in places, so Wine settles
any doubt about a field. Where a file was examined, that is said.
What Microsoft’s text says, in the order of the table:
- The resource table is an alignment shift, an array of
TYPEINFOblocks, a zero word and then the names, ending with a zero byte. EachTYPEINFOhasrtTypeID,rtResourceCount, a reserved double word andrtResourceCountNAMEINFOrecords (rnOffset,rnLength,rnFlags,rnID,rnHandle,rnUsage, 12 bytes). The type isRT_GROUP_ICONorRT_ICONwhen its top bit is set (0x8000); the text lists the type names but its OCR does not preserve the numbers, so they are the ones in the table above, which Wine’sNE_RSCTYPE_GROUP_ICONandNE_RSCTYPE_ICONuse. - The icon directory resource is “nearly identical” to the directory in an ICO file, with the last member of the entry, the file offset, replaced by a 16-bit value. The text calls that value “the resource-table index of the corresponding icon-image resource. The index is 1-based. If an executable file contains multiple icon resources, the index must be unique across all directories.” (7.2). Each icon needs an
RT_ICONentry and each directory anRT_GROUP_ICONentry in the resource table.
nID is the RT_ICON’s resource ID
In practice nID is the integer resource ID of the RT_ICON (its
rnID without the top bit), not its position in the RT_ICON
array. Wine looks for the RT_ICON NAMEINFO whose
rnID equals nID | 0x8000
(pIconStorage[i].id == (pIconId[icon] | 0x8000)). Real files agree. In
WMAGIC.EXE (a Windows 3.x program), two groups have entries with
nID 3, 4 and 5; the three RT_ICON records have
rnID 0x8003, 0x8004, 0x8005 and are
the 1st, 2nd and 3rd of their type, so “position” would give 1, 2, 3. In
COFFEE.EXE the four icons have nID 2 to 5 and are positions 1
to 4. In 27 programs and MORICONS.DLL from a Windows 3.1 installation, and in the
six-icon .icl file, nID and position happen to be the same
because the icons were numbered from 1 in order. So the safe reading is the ID, with
the top bit added; the “index” in Microsoft’s sentence is not the position, and its
“1-based, unique” remark is true of the IDs a resource compiler hands out.
The group order is the NAMEINFO order
- Icon number n is the n-th
NAMEINFOof the group type, counting from 0 in the order the records are stored (pIconDir + i + nIconIndexin Wine). The count of icons the file holds is thertResourceCountof that type. Microsoft’s text does not say how the records of a type are ordered, so nothing in the format sorts the groups: the writer of the file chooses, and the file records the choice. InMORICONS.DLLand in the six-icon.iclfile the group IDs ascend (1, 2, 3, …). InPROGMAN.EXEthey do not: its groups come in the order ID 3, ID 6, ID 7,SHEETICON,DATAICON,COMMICONand so on, so icon number 0 is the group of ID 3 and number 3 isSHEETICON. - The numbering counts named groups too. In
PROGMAN.EXEthe groupsSHEETICON,DATAICON,COMMICONandMSDOSICONsit among the numbered ones and take their place in the count. - That the shell’s own Change Icon dialog numbers groups the same way is not shown by any source read here: Microsoft’s
ExtractIcondocumentation says what a number means but not how Windows orders the groups, so this rests on Wine’s code, which is a reimplementation. - The data is at
rnOffsetshifted left by the table’srscAlignShift, with lengthrnLengthshifted the same way. Microsoft’s text and the knowledge-base article both sayrnLengthis “in bytes” (the article also says the records are “8 bytes each”, though the fields it lists add up to 12). Real files show the shift applies to the length: inMORICONS.DLL(shift 2) an icon group with two entries hasrnLength9, 36 bytes, which holds its 6 + 2 × 14 = 34 bytes plus padding; inCOFFEE.EXE(shift 4) a one-entry group hasrnLength2, 32 bytes for 20. - Wine’s code does not handle a negative icon number for an NE file (the “resource ID” rule above is in its PE branch only).
Names as string offsets
Microsoft’s text defines the name rule: an rnID with the top bit set is an
integer ID; “otherwise, it is an offset to a resource string, relative to the beginning
of the resource table”. The strings are stored after the types, each a length byte and
that many characters, not null-terminated and case-sensitive, ending with a zero length
byte (knowledge-base article). The same rule applies to rtTypeID. A named
RT_GROUP_ICON is ordinary: PROGMAN.EXE has
SHEETICON and others, and COFFEE.EXE has four groups, all
named (MUG, MUG1, MUG2, MUG3), whose
RT_ICON images are numbered 2 to 5. A group’s name does not change its
icon number, which is its position, and the nID entries inside it still
hold numeric IDs. Wine’s exticon.c never reads names: it reaches a group by
position and an RT_ICON only by the 0x8000 comparison, so an
RT_ICON whose rnID is a string offset could not be found from a
numeric nID. Wine’s winedump (dump_ne_resources)
reads names as offsets, as does the site’s
NE resource page. The
RT_ICON records of every file examined had numeric IDs; whether a library
with a named RT_ICON can be used by a name in an nID is not
shown by any source, and a 16-bit nID cannot hold a name in any case.
In a PE library
A PE resource directory has three levels, Type, Name and Language (Microsoft, “PE Format”,
.rsrc section, raw pe-format.md). The icon groups are the
entries of the second-level table under type 14, the images those under type 3, and
below each Name entry is a Language level whose leaf is the data. “A leaf’s Type, Name,
and Language IDs are determined by the path that is taken through directory tables to
reach the leaf”, and each table lists its named entries first, then its ID entries
(pe-format.md). So an icon library, like any PE, can hold a group or an
image under several languages: the type-14 table has one Name entry per group (that
entry’s ID or name is the group’s resource ID or name) and below it one Language
entry per language version of that group, and likewise for each image under type 3.
Which language version is used is not set by the PE text, and nothing read for this
page says which one Windows’ own icon extraction takes. Wine
(exticon.c, raw text) takes the first directory entry of the Language
table (find_entry_default; its comment says “default language (0)”, but
the code takes the entry at the start of the table whatever its language ID). It looks
an image up by the numeric ID in the type-3 table, so the nID of a PE
GRPICONDIR is the Name-level ID of an RT_ICON, with no
0x8000 rule since a PE has no such bit. It counts the groups as the named
plus the numbered entries of the type-14 table, in the order the table stores them.
The one thing the Microsoft documentation does say about languages is for
FindResourceEx: a language of LANG_NEUTRAL means the
calling thread’s current language. A library that holds the same icon under several
languages is therefore read differently by different programs; which language Windows
itself picks in the Change Icon dialog is not documented in the sources read, and no
32-bit icon library was examined.
How a program picks one image
A group holds several images, and a program that wants one icon asks Windows for the best fit. Windows
does this with LookupIconIdFromDirectoryEx: it is given the group’s data (the
GRPICONDIR), a flag for icon or cursor, the wanted width and height, and flags
(LR_DEFAULTCOLOR or LR_MONOCHROME), and returns the resource ID of the
RT_ICON to load, that is, the nID of the best entry. Microsoft’s page
(nf-winuser-lookupiconidfromdirectoryex.md) gives these criteria: size and colour depth are
considered together, the image chosen being the one that most closely matches the requested width and
height along with the colour depth of the current display; an image smaller than the requested size is a
poorer match than a larger one by the same amount, so when two are equally close in size the larger is
preferred; when two match equally well the one with the greater colour depth is chosen. A wanted size of 0
means the system icon (or cursor) size. The function does not validate the data.
Wine’s ICO_ExtractIconExW (exticon.c) does the same for both containers: it
takes the group by position (or, for a negative number in a PE file, by resource ID), calls
LookupIconIdFromDirectoryEx with the size it was given, and then finds the RT_ICON
whose ID (NE: with 0x8000 added) equals the result. When a caller asks for two sizes at once, it
looks up the second size in the same group. So extraction yields one image per group; the
next section rebuilds all of a group’s images.
Getting an ICO file back
To turn one icon of an ICL back into a .ico file, read its group icon
resource, then:
- Write the 6-byte header unchanged.
- Write one 16-byte ICO entry per group entry: its first 12 bytes unchanged, then
the offset in the new file where that image will go, 6 + 16 ×
idCountfor the first image and each following one after the last. - Write the data of each
RT_ICONresource named bynID, in the same order.