reshuffling of dlls
[reactos.git] / reactos / dll / win32 / user32 / misc / exticon.c
1 /*
2 * icon extracting
3 *
4 * taken and slightly changed from shell
5 * this should replace the icon extraction code in shell32 and shell16 once
6 * it needs a serious test for compliance with the native API
7 *
8 * Copyright 2000 Juergen Schmied
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <user32.h>
26 #define NDEBUG
27 #include <debug.h>
28
29 /* Start of Hack section */
30
31 WINE_DEFAULT_DEBUG_CHANNEL(icon);
32
33 #include "pshpack1.h"
34
35 typedef struct
36 {
37 BYTE bWidth; /* Width, in pixels, of the image */
38 BYTE bHeight; /* Height, in pixels, of the image */
39 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
40 BYTE bReserved; /* Reserved ( must be 0) */
41 WORD wPlanes; /* Color Planes */
42 WORD wBitCount; /* Bits per pixel */
43 DWORD dwBytesInRes; /* How many bytes in this resource? */
44 DWORD dwImageOffset; /* Where in the file is this image? */
45 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
46
47 typedef struct
48 {
49 WORD idReserved; /* Reserved (must be 0) */
50 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
51 WORD idCount; /* How many images */
52 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
53 } icoICONDIR, *LPicoICONDIR;
54
55 #include "poppack.h"
56 #if 0
57 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
58 {
59 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
60 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
61 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
62 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
63 }
64 static void dumpIcoDir ( LPicoICONDIR entry )
65 {
66 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
67 }
68 #endif
69
70 /**********************************************************************
71 * find_entry_by_id
72 *
73 * Find an entry by id in a resource directory
74 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
75 */
76 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
77 WORD id, const void *root )
78 {
79 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
80 int min, max, pos;
81
82 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
83 min = dir->NumberOfNamedEntries;
84 max = min + dir->NumberOfIdEntries - 1;
85 while (min <= max)
86 {
87 pos = (min + max) / 2;
88 if (entry[pos].Id == id)
89 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].OffsetToDirectory);
90 if (entry[pos].Id > id) max = pos - 1;
91 else min = pos + 1;
92 }
93 return NULL;
94 }
95
96 /**********************************************************************
97 * find_entry_default
98 *
99 * Find a default entry in a resource directory
100 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
101 */
102 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
103 const void *root )
104 {
105 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
106 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
107 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->OffsetToDirectory);
108 }
109
110 /*************************************************************************
111 * USER32_GetResourceTable
112 */
113 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
114 {
115 IMAGE_DOS_HEADER * mz_header;
116
117 TRACE("%p %p\n", peimage, retptr);
118
119 *retptr = NULL;
120
121 mz_header = (IMAGE_DOS_HEADER*) peimage;
122
123 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
124 {
125 if (mz_header->e_cblp == 1) /* .ICO file ? */
126 {
127 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
128 return 1;
129 }
130 else
131 return 0; /* failed */
132 }
133 if (mz_header->e_lfanew >= pesize) {
134 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
135 }
136 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
137 return IMAGE_NT_SIGNATURE;
138 #if 0
139 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
140 {
141 IMAGE_OS2_HEADER * ne_header;
142
143 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
144
145 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
146 return 0;
147
148 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
149 *retptr = (LPBYTE)-1;
150 else
151 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
152
153 return IMAGE_OS2_SIGNATURE;
154 }
155 #endif
156 return 0; /* failed */
157 }
158 #if 0
159 /*************************************************************************
160 * USER32_LoadResource
161 */
162 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
163 {
164 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
165
166 *uSize = (DWORD)pNInfo->length << sizeShift;
167 return peimage + ((DWORD)pNInfo->offset << sizeShift);
168 }
169
170 /*************************************************************************
171 * ICO_LoadIcon
172 */
173 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
174 {
175 TRACE("%p %p\n", peimage, lpiIDE);
176
177 *uSize = lpiIDE->dwBytesInRes;
178 return peimage + lpiIDE->dwImageOffset;
179 }
180
181 /*************************************************************************
182 * ICO_GetIconDirectory
183 *
184 * Reads .ico file and build phony ICONDIR struct
185 * see http://www.microsoft.com/win32dev/ui/icons.htm
186 */
187 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
188 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
189
190 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
191 {
192 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
193 CURSORICONDIR * lpID; /* icon resource in resource format */
194 int i;
195
196 TRACE("%p %p\n", peimage, lplpiID);
197
198 lpcid = (CURSORICONDIR*)peimage;
199
200 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
201 return 0;
202
203 /* allocate the phony ICONDIR structure */
204 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
205 if( (lpID = (CURSORICONDIR*)HeapAlloc(GetProcessHeap(),0, *uSize) ))
206 {
207 /* copy the header */
208 lpID->idReserved = lpcid->idReserved;
209 lpID->idType = lpcid->idType;
210 lpID->idCount = lpcid->idCount;
211
212 /* copy the entries */
213 for( i=0; i < lpcid->idCount; i++ )
214 {
215 memcpy((void*)&(lpID->idEntries[i]),(void*)&(lpcid->idEntries[i]), sizeof(CURSORICONDIRENTRY) - 2);
216 lpID->idEntries[i].wResId = i;
217 }
218
219 *lplpiID = (LPicoICONDIR)peimage;
220 return (BYTE *)lpID;
221 }
222 return 0;
223 }
224 #endif
225 /*************************************************************************
226 * ICO_ExtractIconExW [internal]
227 *
228 * NOTES
229 * nIcons = 0: returns number of Icons in file
230 *
231 * returns
232 * invalid file: -1
233 * failure:0;
234 * success: number of icons in file (nIcons = 0) or nr of icons retrieved
235 */
236 static UINT ICO_ExtractIconExW(
237 LPCWSTR lpszExeFileName,
238 HICON * RetPtr,
239 INT nIconIndex,
240 UINT nIcons,
241 UINT cxDesired,
242 UINT cyDesired,
243 UINT *pIconId,
244 UINT flags)
245 {
246 UINT ret = 0;
247 UINT cx1, cx2, cy1, cy2;
248 LPBYTE pData;
249 DWORD sig;
250 HANDLE hFile;
251 UINT16 iconDirCount = 0; //,iconCount = 0;
252 LPBYTE peimage;
253 HANDLE fmapping;
254 //ULONG uSize;
255 DWORD fsizeh,fsizel;
256 WCHAR szExePath[MAX_PATH];
257 DWORD dwSearchReturn;
258
259 TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
260
261 dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, sizeof(szExePath) / sizeof(szExePath[0]), szExePath, NULL);
262 if ((dwSearchReturn == 0) || (dwSearchReturn > sizeof(szExePath) / sizeof(szExePath[0])))
263 {
264 WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
265 return -1;
266 }
267
268 hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
269 if (hFile == INVALID_HANDLE_VALUE) return ret;
270 fsizel = GetFileSize(hFile,&fsizeh);
271
272 /* Map the file */
273 fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
274 CloseHandle(hFile);
275 if (!fmapping)
276 {
277 WARN("CreateFileMapping error %ld\n", GetLastError() );
278 return 0xFFFFFFFF;
279 }
280
281 if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
282 {
283 WARN("MapViewOfFile error %ld\n", GetLastError() );
284 CloseHandle(fmapping);
285 return 0xFFFFFFFF;
286 }
287 CloseHandle(fmapping);
288
289 cx1 = LOWORD(cxDesired);
290 cx2 = HIWORD(cxDesired) ? HIWORD(cxDesired) : cx1;
291 cy1 = LOWORD(cyDesired);
292 cy2 = HIWORD(cyDesired) ? HIWORD(cyDesired) : cy1;
293
294 if (pIconId) /* Invalidate first icon identifier */
295 *pIconId = 0xFFFFFFFF;
296
297 if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
298 pIconId = (UINT*)RetPtr;
299
300 sig = USER32_GetResourceTable(peimage, fsizel, &pData);
301
302 /* ico file or NE exe/dll*/
303 #if 0
304 if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
305 {
306 BYTE *pCIDir = 0;
307 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
308 NE_NAMEINFO *pIconStorage = NULL;
309 NE_NAMEINFO *pIconDir = NULL;
310 LPicoICONDIR lpiID = NULL;
311
312 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
313
314 if (pData == (BYTE*)-1)
315 {
316 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
317 if (pCIDir)
318 {
319 iconDirCount = 1; iconCount = lpiID->idCount;
320 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
321 }
322 }
323 else while (pTInfo->type_id && !(pIconStorage && pIconDir))
324 {
325 if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON) /* find icon directory and icon repository */
326 {
327 iconDirCount = pTInfo->count;
328 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
329 TRACE("\tfound directory - %i icon families\n", iconDirCount);
330 }
331 if (pTInfo->type_id == NE_RSCTYPE_ICON)
332 {
333 iconCount = pTInfo->count;
334 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
335 TRACE("\ttotal icons - %i\n", iconCount);
336 }
337 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
338 }
339
340 if ((pIconStorage && pIconDir) || lpiID) /* load resources and create icons */
341 {
342 if (nIcons == 0)
343 {
344 ret = iconDirCount;
345 if (lpiID && pCIDir) /* *.ico file, deallocate heap pointer*/
346 HeapFree(GetProcessHeap(), 0, pCIDir);
347 }
348 else if (nIconIndex < iconDirCount)
349 {
350 UINT16 i, icon;
351 if (nIcons > iconDirCount - nIconIndex)
352 nIcons = iconDirCount - nIconIndex;
353
354 for (i = 0; i < nIcons; i++)
355 {
356 /* .ICO files have only one icon directory */
357 if (lpiID == NULL) /* not *.ico */
358 pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
359 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, (i & 1) ? cx2 : cx1, (i & 1) ? cy2 : cy1, flags);
360 }
361 if (lpiID && pCIDir) /* *.ico file, deallocate heap pointer*/
362 HeapFree(GetProcessHeap(), 0, pCIDir);
363
364 for (icon = 0; icon < nIcons; icon++)
365 {
366 pCIDir = NULL;
367 if (lpiID)
368 pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
369 else
370 for (i = 0; i < iconCount; i++)
371 if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
372 pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
373
374 if (pCIDir)
375 RetPtr[icon] = (HICON)CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
376 (icon & 1) ? cx2 : cx1, (icon & 1) ? cy2 : cy1, flags);
377 else
378 RetPtr[icon] = 0;
379 }
380 ret = icon; /* return number of retrieved icons */
381 }
382 }
383 }
384 /* end ico file */
385
386 /* exe/dll */
387 else if( sig == IMAGE_NT_SIGNATURE )
388 #endif
389 if( sig == IMAGE_NT_SIGNATURE )
390 {
391 LPBYTE idata,igdata;
392 PIMAGE_DOS_HEADER dheader;
393 PIMAGE_NT_HEADERS pe_header;
394 PIMAGE_SECTION_HEADER pe_sections;
395 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
396 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
397 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
398 UINT i, j;
399
400 dheader = (PIMAGE_DOS_HEADER)peimage;
401 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
402 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER)
403 + pe_header->FileHeader.SizeOfOptionalHeader);
404 rootresdir = NULL;
405
406 /* search for the root resource directory */
407 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
408 {
409 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
410 continue;
411 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
412 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
413 debugstr_w(lpszExeFileName),
414 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
415 fsizel
416 );
417 goto end;
418 }
419 /* FIXME: doesn't work when the resources are not in a separate section */
420 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
421 {
422 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
423 break;
424 }
425 }
426
427 if (!rootresdir)
428 {
429 WARN("haven't found section for resource directory.\n");
430 goto end; /* failure */
431 }
432
433 /* search for the group icon directory */
434 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
435 {
436 WARN("No Icongroupresourcedirectory!\n");
437 goto end; /* failure */
438 }
439 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
440
441 /* only number of icons requested */
442 if( nIcons == 0 )
443 {
444 ret = iconDirCount;
445 goto end; /* success */
446 }
447
448 if( nIconIndex < 0 )
449 {
450 /* search resource id */
451 int n = 0;
452 int iId = abs(nIconIndex);
453 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
454
455 while(n<iconDirCount && xprdeTmp)
456 {
457 if(xprdeTmp->Id == iId)
458 {
459 nIconIndex = n;
460 break;
461 }
462 n++;
463 xprdeTmp++;
464 }
465 if (nIconIndex < 0)
466 {
467 WARN("resource id %d not found\n", iId);
468 goto end; /* failure */
469 }
470 }
471 else
472 {
473 /* check nIconIndex to be in range */
474 if (nIconIndex >= iconDirCount)
475 {
476 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
477 goto end; /* failure */
478 }
479 }
480
481 /* assure we don't get too much */
482 if( nIcons / 2 > iconDirCount - nIconIndex )
483 nIcons = 2 * (iconDirCount - nIconIndex);
484
485 /* starting from specified index */
486 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
487
488 for (i=0; i < nIcons; i++)
489 {
490 const IMAGE_RESOURCE_DIRECTORY *resdir;
491
492 /* go down this resource entry, name */
493 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->OffsetToDirectory));
494
495 /* default language (0) */
496 resdir = find_entry_default(resdir,rootresdir);
497 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
498
499 /* lookup address in mapped image for virtual address */
500 igdata = NULL;
501
502 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
503 {
504 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
505 continue;
506 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
507 continue;
508
509 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
510 FIXME("overflow in PE lookup (%s has len %ld, have offset %ld), short file?\n", debugstr_w(lpszExeFileName), fsizel,
511 igdataent->OffsetToData - pe_sections[j].VirtualAddress + pe_sections[j].PointerToRawData + igdataent->Size);
512 goto end; /* failure */
513 }
514 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
515 }
516
517 if (!igdata)
518 {
519 FIXME("no matching real address for icongroup!\n");
520 goto end; /* failure */
521 }
522 pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, (i & 1) ? cx2 : cx1, (i & 1) ? cy2 : cy1, flags);
523 if (i & 1) xresent++;
524 }
525
526 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
527 {
528 WARN("No Iconresourcedirectory!\n");
529 goto end; /* failure */
530 }
531
532 for (i=0; i<nIcons; i++)
533 {
534 const IMAGE_RESOURCE_DIRECTORY *xresdir;
535 xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
536 xresdir = find_entry_default(xresdir, rootresdir);
537 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
538 idata = NULL;
539
540 /* map virtual to address in image */
541 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
542 {
543 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
544 continue;
545 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
546 continue;
547 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
548 }
549 if (!idata)
550 {
551 WARN("no matching real address found for icondata!\n");
552 RetPtr[i]=0;
553 continue;
554 }
555 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,
556 (i & 1) ? cx2 : cx1, (i & 1) ? cy2 : cy1, flags);
557 }
558 ret = i; /* return number of retrieved icons */
559 } /* if(sig == IMAGE_NT_SIGNATURE) */
560
561 end:
562 UnmapViewOfFile(peimage); /* success */
563 return ret;
564 }
565
566 /***********************************************************************
567 * PrivateExtractIconsW [USER32.@]
568 * @implemented
569 *
570 * NOTES
571 * If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
572 * returned, with the LOWORD size icon first and the HIWORD size icon
573 * second.
574 * Also the Windows equivalent does extract icons in a strange way if
575 * nIndex is negative. Our implementation treats a negative nIndex as
576 * looking for that resource identifier for the first icon to retrieve.
577 *
578 * FIXME:
579 * should also support 16 bit EXE + DLLs, cursor and animated cursor as
580 * well as bitmap files.
581 */
582
583 UINT WINAPI PrivateExtractIconsW (
584 LPCWSTR lpwstrFile,
585 int nIndex,
586 int sizeX,
587 int sizeY,
588 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
589 UINT* pIconId, /* [out] pointer to array of nIcons icon identifiers or NULL */
590 UINT nIcons, /* [in] number of icons to retrieve */
591 UINT flags ) /* [in] LR_* flags used by LoadImage */
592 {
593 TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
594 debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
595
596 if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
597 {
598 WARN("Uneven number %d of icons requested for small and large icons!", nIcons);
599 }
600 return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
601 }
602
603 /***********************************************************************
604 * PrivateExtractIconsA [USER32.@]
605 * @implemented
606 */
607
608 UINT WINAPI PrivateExtractIconsA (
609 LPCSTR lpstrFile,
610 int nIndex,
611 int sizeX,
612 int sizeY,
613 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
614 UINT* piconid, /* [out] pointer to array of nIcons icon identifiers or NULL */
615 UINT nIcons, /* [in] number of icons to retrieve */
616 UINT flags ) /* [in] LR_* flags used by LoadImage */
617 {
618 UINT ret;
619 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
620 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
621
622 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
623 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
624
625 HeapFree(GetProcessHeap(), 0, lpwstrFile);
626 return ret;
627 }
628
629 /***********************************************************************
630 * PrivateExtractIconExW [USER32.@]
631 * @implemented
632 * NOTES
633 * if nIndex == -1 it returns the number of icons in any case !!!
634 */
635 UINT WINAPI PrivateExtractIconExW (
636 LPCWSTR lpwstrFile,
637 int nIndex,
638 HICON * phIconLarge,
639 HICON * phIconSmall,
640 UINT nIcons )
641 {
642 DWORD cyicon, cysmicon, cxicon, cxsmicon;
643 INT ret = 0;
644
645 TRACE("%s %d %p %p %d\n",
646 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
647
648 if (nIndex == -1)
649 /* get the number of icons */
650 return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
651
652 if (nIcons == 1 && phIconSmall && phIconLarge)
653 {
654 HICON hIcon[2];
655 cxicon = GetSystemMetrics(SM_CXICON);
656 cyicon = GetSystemMetrics(SM_CYICON);
657 cxsmicon = GetSystemMetrics(SM_CXSMICON);
658 cysmicon = GetSystemMetrics(SM_CYSMICON);
659
660 ret = ICO_ExtractIconExW(lpwstrFile, (HICON*) &hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
661 cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
662 *phIconLarge = (1 <= ret ? hIcon[0] : NULL);
663 *phIconSmall = (2 <= ret ? hIcon[1] : NULL);
664 return ret;
665 }
666
667 if (phIconSmall)
668 {
669 /* extract n small icons */
670 cxsmicon = GetSystemMetrics(SM_CXSMICON);
671 cysmicon = GetSystemMetrics(SM_CYSMICON);
672 ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
673 cysmicon, NULL, LR_DEFAULTCOLOR);
674 }
675 if (phIconLarge)
676 {
677 /* extract n large icons */
678 cxicon = GetSystemMetrics(SM_CXICON);
679 cyicon = GetSystemMetrics(SM_CYICON);
680 ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
681 cyicon, NULL, LR_DEFAULTCOLOR);
682 }
683 return ret;
684 }
685
686 /***********************************************************************
687 * PrivateExtractIconExA [USER32.@]
688 * @implemented
689 */
690 UINT WINAPI PrivateExtractIconExA (
691 LPCSTR lpstrFile,
692 int nIndex,
693 HICON * phIconLarge,
694 HICON * phIconSmall,
695 UINT nIcons )
696 {
697 UINT ret;
698 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
699 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
700
701 TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
702
703 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
704 ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
705 HeapFree(GetProcessHeap(), 0, lpwstrFile);
706 return ret;
707 }