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