ddc683e2c8798cbba18fd8ab55cfc61a10230cd2
[reactos.git] / reactos / 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 #ifdef WINE
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 #ifdef WINE
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 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
164 {
165 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
166 CURSORICONDIR * lpID; /* icon resource in resource format */
167 int i;
168
169 TRACE("%p %p\n", peimage, lplpiID);
170
171 lpcid = (CURSORICONDIR*)peimage;
172
173 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
174 return 0;
175
176 /* allocate the phony ICONDIR structure */
177 *uSize = FIELD_OFFSET(CURSORICONDIR, idEntries[lpcid->idCount]);
178 if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
179 {
180 /* copy the header */
181 lpID->idReserved = lpcid->idReserved;
182 lpID->idType = lpcid->idType;
183 lpID->idCount = lpcid->idCount;
184
185 /* copy the entries */
186 for( i=0; i < lpcid->idCount; i++ )
187 {
188 memcpy(&lpID->idEntries[i], &lpcid->idEntries[i], sizeof(CURSORICONDIRENTRY) - 2);
189 lpID->idEntries[i].wResId = i;
190 }
191
192 *lplpiID = (LPicoICONDIR)peimage;
193 return (BYTE *)lpID;
194 }
195 return 0;
196 }
197 #endif
198 /*************************************************************************
199 * ICO_ExtractIconExW [internal]
200 *
201 * NOTES
202 * nIcons = 0: returns number of Icons in file
203 *
204 * returns
205 * invalid file: -1
206 * failure:0;
207 * success: number of icons in file (nIcons = 0) or nr of icons retrieved
208 */
209 static UINT ICO_ExtractIconExW(
210 LPCWSTR lpszExeFileName,
211 HICON * RetPtr,
212 INT nIconIndex,
213 UINT nIcons,
214 UINT cxDesired,
215 UINT cyDesired,
216 UINT *pIconId,
217 UINT flags)
218 {
219 UINT ret = 0;
220 UINT cx1, cx2, cy1, cy2;
221 LPBYTE pData;
222 DWORD sig;
223 HANDLE hFile;
224 UINT16 iconDirCount = 0, iconCount = 0;
225 LPBYTE peimage;
226 HANDLE fmapping;
227 DWORD fsizeh,fsizel;
228 WCHAR szExePath[MAX_PATH];
229 DWORD dwSearchReturn;
230
231 TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
232
233 dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, sizeof(szExePath) / sizeof(szExePath[0]), szExePath, NULL);
234 if ((dwSearchReturn == 0) || (dwSearchReturn > sizeof(szExePath) / sizeof(szExePath[0])))
235 {
236 WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
237 return -1;
238 }
239
240 hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
241 if (hFile == INVALID_HANDLE_VALUE) return ret;
242 fsizel = GetFileSize(hFile,&fsizeh);
243
244 /* Map the file */
245 fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
246 CloseHandle(hFile);
247 if (!fmapping)
248 {
249 WARN("CreateFileMapping error %ld\n", GetLastError() );
250 return 0xFFFFFFFF;
251 }
252
253 if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
254 {
255 WARN("MapViewOfFile error %ld\n", GetLastError() );
256 CloseHandle(fmapping);
257 return 0xFFFFFFFF;
258 }
259 CloseHandle(fmapping);
260
261 cx1 = LOWORD(cxDesired);
262 cx2 = HIWORD(cxDesired);
263 cy1 = LOWORD(cyDesired);
264 cy2 = HIWORD(cyDesired);
265
266 if (pIconId) /* Invalidate first icon identifier */
267 *pIconId = 0xFFFFFFFF;
268
269 if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
270 pIconId = (UINT*)RetPtr;
271
272 sig = USER32_GetResourceTable(peimage, fsizel, &pData);
273
274 #ifdef WINE
275 /* ico file or NE exe/dll*/
276 if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
277 {
278 BYTE *pCIDir = 0;
279 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
280 NE_NAMEINFO *pIconStorage = NULL;
281 NE_NAMEINFO *pIconDir = NULL;
282 LPicoICONDIR lpiID = NULL;
283 ULONG uSize = 0;
284
285 TRACE("-- OS2/icon Signature (0x%08x)\n", sig);
286
287 if (pData == (BYTE*)-1)
288 {
289 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
290 if (pCIDir)
291 {
292 iconDirCount = 1; iconCount = lpiID->idCount;
293 TRACE("-- icon found %p 0x%08x 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
294 }
295 }
296 else while (pTInfo->type_id && !(pIconStorage && pIconDir))
297 {
298 if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON) /* find icon directory and icon repository */
299 {
300 iconDirCount = pTInfo->count;
301 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
302 TRACE("\tfound directory - %i icon families\n", iconDirCount);
303 }
304 if (pTInfo->type_id == NE_RSCTYPE_ICON)
305 {
306 iconCount = pTInfo->count;
307 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
308 TRACE("\ttotal icons - %i\n", iconCount);
309 }
310 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
311 }
312
313 if ((pIconStorage && pIconDir) || lpiID) /* load resources and create icons */
314 {
315 if (nIcons == 0)
316 {
317 ret = iconDirCount;
318 if (lpiID) /* *.ico file, deallocate heap pointer*/
319 HeapFree(GetProcessHeap(), 0, pCIDir);
320 }
321 else if (nIconIndex < iconDirCount)
322 {
323 UINT16 i, icon;
324 if (nIcons > iconDirCount - nIconIndex)
325 nIcons = iconDirCount - nIconIndex;
326
327 for (i = 0; i < nIcons; i++)
328 {
329 /* .ICO files have only one icon directory */
330 if (lpiID == NULL) /* not *.ico */
331 pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
332 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
333 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx2, cy2, flags);
334 }
335 if (lpiID) /* *.ico file, deallocate heap pointer*/
336 HeapFree(GetProcessHeap(), 0, pCIDir);
337
338 for (icon = 0; icon < nIcons; icon++)
339 {
340 pCIDir = NULL;
341 if (lpiID)
342 pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
343 else
344 for (i = 0; i < iconCount; i++)
345 if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
346 pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
347
348 if (pCIDir)
349 {
350 RetPtr[icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
351 cx1, cy1, flags);
352 if (cx2 && cy2)
353 RetPtr[++icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
354 cx2, cy2, flags);
355 }
356 else
357 RetPtr[icon] = 0;
358 }
359 ret = icon; /* return number of retrieved icons */
360 }
361 }
362 }
363 #else
364 if (sig == 1) /* .ICO file */
365 {
366 TRACE("-- icon Signature (0x%08x)\n", sig);
367
368 if (pData == (BYTE*)-1)
369 {
370 INT dataOffset;
371 LPICONIMAGE entry;
372 CURSORICONDIR *lpcid = (CURSORICONDIR*)peimage;
373 INT cx[2] = {cx1, cx2}, cy[2] = {cy1, cy2};
374 INT index;
375
376 if (lpcid->idType != 1)
377 return 0;
378
379 for(index = 0; index < 2; index++)
380 {
381 dataOffset = LookupIconIdFromDirectoryEx(peimage, TRUE, cx[index], cy[index], flags);
382
383 if (dataOffset)
384 {
385 HICON icon;
386 entry = (LPICONIMAGE)(peimage + dataOffset);
387 icon = CreateIconFromResourceEx(peimage + dataOffset, entry->icHeader.biSizeImage, TRUE, 0x00030000, cx[index], cy[index], flags);
388
389 if (icon)
390 {
391 RetPtr[index] = icon;
392 iconCount = 1;
393 }
394 }
395 }
396
397 }
398 ret = iconCount; /* return number of retrieved icons */
399 }
400 #endif
401 /* end ico file */
402
403 /* exe/dll */
404 else if( sig == IMAGE_NT_SIGNATURE )
405 {
406 BYTE *idata, *igdata;
407 const IMAGE_RESOURCE_DIRECTORY *rootresdir, *iconresdir, *icongroupresdir;
408 const IMAGE_RESOURCE_DATA_ENTRY *idataent, *igdataent;
409 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
410 ULONG size;
411 UINT i;
412
413 rootresdir = RtlImageDirectoryEntryToData((HMODULE)peimage, FALSE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size);
414 if (!rootresdir)
415 {
416 WARN("haven't found section for resource directory.\n");
417 goto end;
418 }
419
420 /* search for the group icon directory */
421 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
422 {
423 WARN("No Icongroupresourcedirectory!\n");
424 goto end; /* failure */
425 }
426 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
427
428 /* only number of icons requested */
429 if( !pIconId )
430 {
431 ret = iconDirCount;
432 goto end; /* success */
433 }
434
435 if( nIconIndex < 0 )
436 {
437 /* search resource id */
438 int n = 0;
439 int iId = abs(nIconIndex);
440 const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
441
442 while(n<iconDirCount && xprdeTmp)
443 {
444 if(xprdeTmp->Id == iId)
445 {
446 nIconIndex = n;
447 break;
448 }
449 n++;
450 xprdeTmp++;
451 }
452 if (nIconIndex < 0)
453 {
454 WARN("resource id %d not found\n", iId);
455 goto end; /* failure */
456 }
457 }
458 else
459 {
460 /* check nIconIndex to be in range */
461 if (nIconIndex >= iconDirCount)
462 {
463 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
464 goto end; /* failure */
465 }
466 }
467
468 /* assure we don't get too much */
469 if( nIcons > iconDirCount - nIconIndex )
470 nIcons = iconDirCount - nIconIndex;
471
472 /* starting from specified index */
473 xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
474
475 for (i=0; i < nIcons; i++,xresent++)
476 {
477 const IMAGE_RESOURCE_DIRECTORY *resdir;
478
479 /* go down this resource entry, name */
480 resdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)rootresdir + xresent->OffsetToDirectory);
481
482 /* default language (0) */
483 resdir = find_entry_default(resdir,rootresdir);
484 igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
485
486 /* lookup address in mapped image for virtual address */
487 igdata = RtlImageRvaToVa(RtlImageNtHeader((HMODULE)peimage), (HMODULE)peimage, igdataent->OffsetToData, NULL);
488 if (!igdata)
489 {
490 FIXME("no matching real address for icongroup!\n");
491 goto end; /* failure */
492 }
493 pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
494 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
495 }
496
497 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
498 {
499 WARN("No Iconresourcedirectory!\n");
500 goto end; /* failure */
501 }
502
503 for (i=0; i<nIcons; i++)
504 {
505 const IMAGE_RESOURCE_DIRECTORY *xresdir;
506 xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
507 if( !xresdir )
508 {
509 WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
510 RetPtr[i]=0;
511 continue;
512 }
513 xresdir = find_entry_default(xresdir, rootresdir);
514 idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
515
516 idata = RtlImageRvaToVa(RtlImageNtHeader((HMODULE)peimage), (HMODULE)peimage, idataent->OffsetToData, NULL);
517 if (!idata)
518 {
519 WARN("no matching real address found for icondata!\n");
520 RetPtr[i]=0;
521 continue;
522 }
523 RetPtr[i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx1, cy1, flags);
524 if (cx2 && cy2)
525 RetPtr[++i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx2, cy2, flags);
526 }
527 ret = i; /* return number of retrieved icons */
528 } /* if(sig == IMAGE_NT_SIGNATURE) */
529
530 end:
531 UnmapViewOfFile(peimage); /* success */
532 return ret;
533 }
534
535 /***********************************************************************
536 * PrivateExtractIconsW [USER32.@]
537 *
538 * NOTES
539 * If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
540 * returned, with the LOWORD size icon first and the HIWORD size icon
541 * second.
542 * Also the Windows equivalent does extract icons in a strange way if
543 * nIndex is negative. Our implementation treats a negative nIndex as
544 * looking for that resource identifier for the first icon to retrieve.
545 *
546 * FIXME:
547 * should also support 16 bit EXE + DLLs, cursor and animated cursor as
548 * well as bitmap files.
549 */
550
551 UINT WINAPI PrivateExtractIconsW (
552 LPCWSTR lpwstrFile,
553 int nIndex,
554 int sizeX,
555 int sizeY,
556 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
557 UINT* pIconId, /* [out] pointer to array of nIcons icon identifiers or NULL */
558 UINT nIcons, /* [in] number of icons to retrieve */
559 UINT flags ) /* [in] LR_* flags used by LoadImage */
560 {
561 TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
562 debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
563
564 if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
565 {
566 WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
567 }
568 return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
569 }
570
571 /***********************************************************************
572 * PrivateExtractIconsA [USER32.@]
573 */
574
575 UINT WINAPI PrivateExtractIconsA (
576 LPCSTR lpstrFile,
577 int nIndex,
578 int sizeX,
579 int sizeY,
580 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
581 UINT* piconid, /* [out] pointer to array of nIcons icon identifiers or NULL */
582 UINT nIcons, /* [in] number of icons to retrieve */
583 UINT flags ) /* [in] LR_* flags used by LoadImage */
584 {
585 UINT ret;
586 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
587 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
588 if (lpwstrFile == NULL)
589 return 0;
590
591 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
592 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
593
594 HeapFree(GetProcessHeap(), 0, lpwstrFile);
595 return ret;
596 }
597
598 /***********************************************************************
599 * PrivateExtractIconExW [USER32.@]
600 * NOTES
601 * if nIndex == -1 it returns the number of icons in any case !!!
602 */
603 UINT WINAPI PrivateExtractIconExW (
604 LPCWSTR lpwstrFile,
605 int nIndex,
606 HICON * phIconLarge,
607 HICON * phIconSmall,
608 UINT nIcons )
609 {
610 DWORD cyicon, cysmicon, cxicon, cxsmicon;
611 UINT ret = 0;
612
613 TRACE("%s %d %p %p %d\n",
614 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
615
616 if (nIndex == -1)
617 /* get the number of icons */
618 return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
619
620 if (nIcons == 1 && phIconSmall && phIconLarge)
621 {
622 HICON hIcon[2];
623 cxicon = GetSystemMetrics(SM_CXICON);
624 cyicon = GetSystemMetrics(SM_CYICON);
625 cxsmicon = GetSystemMetrics(SM_CXSMICON);
626 cysmicon = GetSystemMetrics(SM_CYSMICON);
627
628 ret = ICO_ExtractIconExW(lpwstrFile, hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
629 cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
630 *phIconLarge = hIcon[0];
631 *phIconSmall = hIcon[1];
632 return ret;
633 }
634
635 if (phIconSmall)
636 {
637 /* extract n small icons */
638 cxsmicon = GetSystemMetrics(SM_CXSMICON);
639 cysmicon = GetSystemMetrics(SM_CYSMICON);
640 ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
641 cysmicon, NULL, LR_DEFAULTCOLOR);
642 }
643 if (phIconLarge)
644 {
645 /* extract n large icons */
646 cxicon = GetSystemMetrics(SM_CXICON);
647 cyicon = GetSystemMetrics(SM_CYICON);
648 ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
649 cyicon, NULL, LR_DEFAULTCOLOR);
650 }
651 return ret;
652 }
653
654 /***********************************************************************
655 * PrivateExtractIconExA [USER32.@]
656 */
657 UINT WINAPI PrivateExtractIconExA (
658 LPCSTR lpstrFile,
659 int nIndex,
660 HICON * phIconLarge,
661 HICON * phIconSmall,
662 UINT nIcons )
663 {
664 UINT ret;
665 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
666 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
667 if (lpwstrFile == NULL)
668 return 0;
669
670 TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
671
672 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
673 ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
674 HeapFree(GetProcessHeap(), 0, lpwstrFile);
675 return ret;
676 }