Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / user32 / exticon.c
bloba720cf325915fceb7b2e9e6ecb2310bb69cfe411
1 /*
2 * icon extracting
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
8 * Copyright 2000 Juergen Schmied
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.
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.
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
25 #include "config.h"
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h> /* abs() */
30 #include <sys/types.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winuser.h"
40 #include "wine/winbase16.h"
41 #include "user_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(icon);
46 #include "pshpack1.h"
48 typedef struct
50 BYTE bWidth; /* Width, in pixels, of the image */
51 BYTE bHeight; /* Height, in pixels, of the image */
52 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
53 BYTE bReserved; /* Reserved ( must be 0) */
54 WORD wPlanes; /* Color Planes */
55 WORD wBitCount; /* Bits per pixel */
56 DWORD dwBytesInRes; /* How many bytes in this resource? */
57 DWORD dwImageOffset; /* Where in the file is this image? */
58 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
60 typedef struct
62 WORD idReserved; /* Reserved (must be 0) */
63 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
64 WORD idCount; /* How many images */
65 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
66 } icoICONDIR, *LPicoICONDIR;
68 #include "poppack.h"
70 #if 0
71 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
73 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
74 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
75 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
76 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
78 static void dumpIcoDir ( LPicoICONDIR entry )
80 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
82 #endif
84 /**********************************************************************
85 * find_entry_by_id
87 * Find an entry by id in a resource directory
88 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
90 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
91 WORD id, const void *root )
93 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
94 int min, max, pos;
96 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
97 min = dir->NumberOfNamedEntries;
98 max = min + dir->NumberOfIdEntries - 1;
99 while (min <= max)
101 pos = (min + max) / 2;
102 if (entry[pos].u1.s2.Id == id)
103 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
104 if (entry[pos].u1.s2.Id > id) max = pos - 1;
105 else min = pos + 1;
107 return NULL;
110 /**********************************************************************
111 * find_entry_default
113 * Find a default entry in a resource directory
114 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
116 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
117 const void *root )
119 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
120 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
121 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
124 /*************************************************************************
125 * USER32_GetResourceTable
127 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
129 IMAGE_DOS_HEADER * mz_header;
131 TRACE("%p %p\n", peimage, retptr);
133 *retptr = NULL;
135 mz_header = (IMAGE_DOS_HEADER*) peimage;
137 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
139 if (mz_header->e_cblp == 1) /* .ICO file ? */
141 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
142 return 1;
144 else
145 return 0; /* failed */
147 if (mz_header->e_lfanew >= pesize) {
148 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
150 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
151 return IMAGE_NT_SIGNATURE;
153 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
155 IMAGE_OS2_HEADER * ne_header;
157 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
159 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
160 return 0;
162 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
163 *retptr = (LPBYTE)-1;
164 else
165 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
167 return IMAGE_OS2_SIGNATURE;
169 return 0; /* failed */
171 /*************************************************************************
172 * USER32_LoadResource
174 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
176 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
178 *uSize = (DWORD)pNInfo->length << sizeShift;
179 return peimage + ((DWORD)pNInfo->offset << sizeShift);
182 /*************************************************************************
183 * ICO_LoadIcon
185 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
187 TRACE("%p %p\n", peimage, lpiIDE);
189 *uSize = lpiIDE->dwBytesInRes;
190 return peimage + lpiIDE->dwImageOffset;
193 /*************************************************************************
194 * ICO_GetIconDirectory
196 * Reads .ico file and build phony ICONDIR struct
198 #define HEADER_SIZE (sizeof(CURSORICONDIR) - sizeof (CURSORICONDIRENTRY))
199 #define HEADER_SIZE_FILE (sizeof(icoICONDIR) - sizeof (icoICONDIRENTRY))
201 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
203 CURSORICONDIR * lpcid; /* icon resource in resource-dir format */
204 CURSORICONDIR * lpID; /* icon resource in resource format */
205 int i;
207 TRACE("%p %p\n", peimage, lplpiID);
209 lpcid = (CURSORICONDIR*)peimage;
211 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
212 return 0;
214 /* allocate the phony ICONDIR structure */
215 *uSize = lpcid->idCount * sizeof(CURSORICONDIRENTRY) + HEADER_SIZE;
216 if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
218 /* copy the header */
219 lpID->idReserved = lpcid->idReserved;
220 lpID->idType = lpcid->idType;
221 lpID->idCount = lpcid->idCount;
223 /* copy the entries */
224 for( i=0; i < lpcid->idCount; i++ )
226 memcpy(&lpID->idEntries[i], &lpcid->idEntries[i], sizeof(CURSORICONDIRENTRY) - 2);
227 lpID->idEntries[i].wResId = i;
230 *lplpiID = (LPicoICONDIR)peimage;
231 return (BYTE *)lpID;
233 return 0;
236 /*************************************************************************
237 * ICO_ExtractIconExW [internal]
239 * NOTES
240 * nIcons = 0: returns number of Icons in file
242 * returns
243 * invalid file: -1
244 * failure:0;
245 * success: number of icons in file (nIcons = 0) or nr of icons retrieved
247 static UINT ICO_ExtractIconExW(
248 LPCWSTR lpszExeFileName,
249 HICON * RetPtr,
250 INT nIconIndex,
251 UINT nIcons,
252 UINT cxDesired,
253 UINT cyDesired,
254 UINT *pIconId,
255 UINT flags)
257 UINT ret = 0;
258 UINT cx1, cx2, cy1, cy2;
259 LPBYTE pData;
260 DWORD sig;
261 HANDLE hFile;
262 UINT16 iconDirCount = 0,iconCount = 0;
263 LPBYTE peimage;
264 HANDLE fmapping;
265 DWORD fsizeh,fsizel;
266 WCHAR szExePath[MAX_PATH];
267 DWORD dwSearchReturn;
269 TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
271 dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, sizeof(szExePath) / sizeof(szExePath[0]), szExePath, NULL);
272 if ((dwSearchReturn == 0) || (dwSearchReturn > sizeof(szExePath) / sizeof(szExePath[0])))
274 WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
275 return -1;
278 hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
279 if (hFile == INVALID_HANDLE_VALUE) return ret;
280 fsizel = GetFileSize(hFile,&fsizeh);
282 /* Map the file */
283 fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
284 CloseHandle(hFile);
285 if (!fmapping)
287 WARN("CreateFileMapping error %d\n", GetLastError() );
288 return 0xFFFFFFFF;
291 if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
293 WARN("MapViewOfFile error %d\n", GetLastError() );
294 CloseHandle(fmapping);
295 return 0xFFFFFFFF;
297 CloseHandle(fmapping);
299 cx1 = LOWORD(cxDesired);
300 cx2 = HIWORD(cxDesired);
301 cy1 = LOWORD(cyDesired);
302 cy2 = HIWORD(cyDesired);
304 if (pIconId) /* Invalidate first icon identifier */
305 *pIconId = 0xFFFFFFFF;
307 if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
308 pIconId = (UINT*)RetPtr;
310 sig = USER32_GetResourceTable(peimage, fsizel, &pData);
312 /* ico file or NE exe/dll*/
313 if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
315 BYTE *pCIDir = 0;
316 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
317 NE_NAMEINFO *pIconStorage = NULL;
318 NE_NAMEINFO *pIconDir = NULL;
319 LPicoICONDIR lpiID = NULL;
320 ULONG uSize = 0;
322 TRACE("-- OS2/icon Signature (0x%08x)\n", sig);
324 if (pData == (BYTE*)-1)
326 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
327 if (pCIDir)
329 iconDirCount = 1; iconCount = lpiID->idCount;
330 TRACE("-- icon found %p 0x%08x 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
333 else while (pTInfo->type_id && !(pIconStorage && pIconDir))
335 if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON) /* find icon directory and icon repository */
337 iconDirCount = pTInfo->count;
338 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
339 TRACE("\tfound directory - %i icon families\n", iconDirCount);
341 if (pTInfo->type_id == NE_RSCTYPE_ICON)
343 iconCount = pTInfo->count;
344 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
345 TRACE("\ttotal icons - %i\n", iconCount);
347 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
350 if ((pIconStorage && pIconDir) || lpiID) /* load resources and create icons */
352 if (nIcons == 0)
354 ret = iconDirCount;
355 if (lpiID) /* *.ico file, deallocate heap pointer*/
356 HeapFree(GetProcessHeap(), 0, pCIDir);
358 else if (nIconIndex < iconDirCount)
360 UINT16 i, icon;
361 if (nIcons > iconDirCount - nIconIndex)
362 nIcons = iconDirCount - nIconIndex;
364 for (i = 0; i < nIcons; i++)
366 /* .ICO files have only one icon directory */
367 if (lpiID == NULL) /* not *.ico */
368 pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
369 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
370 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx2, cy2, flags);
372 if (lpiID) /* *.ico file, deallocate heap pointer*/
373 HeapFree(GetProcessHeap(), 0, pCIDir);
375 for (icon = 0; icon < nIcons; icon++)
377 pCIDir = NULL;
378 if (lpiID)
379 pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
380 else
381 for (i = 0; i < iconCount; i++)
382 if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
383 pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
385 if (pCIDir)
387 RetPtr[icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
388 cx1, cy1, flags);
389 if (cx2 && cy2)
390 RetPtr[++icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
391 cx2, cy2, flags);
393 else
394 RetPtr[icon] = 0;
396 ret = icon; /* return number of retrieved icons */
400 /* end ico file */
402 /* exe/dll */
403 else if( sig == IMAGE_NT_SIGNATURE )
405 LPBYTE idata,igdata;
406 PIMAGE_DOS_HEADER dheader;
407 PIMAGE_NT_HEADERS pe_header;
408 PIMAGE_SECTION_HEADER pe_sections;
409 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
410 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
411 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
412 UINT i, j;
414 dheader = (PIMAGE_DOS_HEADER)peimage;
415 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
416 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER)
417 + pe_header->FileHeader.SizeOfOptionalHeader);
418 rootresdir = NULL;
420 /* search for the root resource directory */
421 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
423 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
424 continue;
425 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
426 FIXME("File %s too short (section is at %d bytes, real size is %d)\n",
427 debugstr_w(lpszExeFileName),
428 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
429 fsizel
431 goto end;
433 /* FIXME: doesn't work when the resources are not in a separate section */
434 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
436 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
437 break;
441 if (!rootresdir)
443 WARN("haven't found section for resource directory.\n");
444 goto end; /* failure */
447 /* search for the group icon directory */
448 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
450 WARN("No Icongroupresourcedirectory!\n");
451 goto end; /* failure */
453 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
455 /* only number of icons requested */
456 if( !pIconId )
458 ret = iconDirCount;
459 goto end; /* success */
462 if( nIconIndex < 0 )
464 /* search resource id */
465 int n = 0;
466 int iId = abs(nIconIndex);
467 const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
469 while(n<iconDirCount && xprdeTmp)
471 if(xprdeTmp->u1.s2.Id == iId)
473 nIconIndex = n;
474 break;
476 n++;
477 xprdeTmp++;
479 if (nIconIndex < 0)
481 WARN("resource id %d not found\n", iId);
482 goto end; /* failure */
485 else
487 /* check nIconIndex to be in range */
488 if (nIconIndex >= iconDirCount)
490 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
491 goto end; /* failure */
495 /* assure we don't get too much */
496 if( nIcons > iconDirCount - nIconIndex )
497 nIcons = iconDirCount - nIconIndex;
499 /* starting from specified index */
500 xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
502 for (i=0; i < nIcons; i++,xresent++)
504 const IMAGE_RESOURCE_DIRECTORY *resdir;
506 /* go down this resource entry, name */
507 resdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)rootresdir + xresent->u2.s3.OffsetToDirectory);
509 /* default language (0) */
510 resdir = find_entry_default(resdir,rootresdir);
511 igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
513 /* lookup address in mapped image for virtual address */
514 igdata = NULL;
516 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
518 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
519 continue;
520 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
521 continue;
523 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
524 FIXME("overflow in PE lookup (%s has len %d, have offset %d), short file?\n", debugstr_w(lpszExeFileName), fsizel,
525 igdataent->OffsetToData - pe_sections[j].VirtualAddress + pe_sections[j].PointerToRawData + igdataent->Size);
526 goto end; /* failure */
528 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
531 if (!igdata)
533 FIXME("no matching real address for icongroup!\n");
534 goto end; /* failure */
536 pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
537 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
540 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
542 WARN("No Iconresourcedirectory!\n");
543 goto end; /* failure */
546 for (i=0; i<nIcons; i++)
548 const IMAGE_RESOURCE_DIRECTORY *xresdir;
549 xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
550 if( !xresdir )
552 WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
553 RetPtr[i]=0;
554 continue;
556 xresdir = find_entry_default(xresdir, rootresdir);
557 idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
558 idata = NULL;
560 /* map virtual to address in image */
561 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
563 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
564 continue;
565 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
566 continue;
567 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
569 if (!idata)
571 WARN("no matching real address found for icondata!\n");
572 RetPtr[i]=0;
573 continue;
575 RetPtr[i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx1, cy1, flags);
576 if (cx2 && cy2)
577 RetPtr[++i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx2, cy2, flags);
579 ret = i; /* return number of retrieved icons */
580 } /* if(sig == IMAGE_NT_SIGNATURE) */
582 end:
583 UnmapViewOfFile(peimage); /* success */
584 return ret;
587 /***********************************************************************
588 * PrivateExtractIconsW [USER32.@]
590 * NOTES
591 * If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
592 * returned, with the LOWORD size icon first and the HIWORD size icon
593 * second.
594 * Also the Windows equivalent does extract icons in a strange way if
595 * nIndex is negative. Our implementation treats a negative nIndex as
596 * looking for that resource identifier for the first icon to retrieve.
598 * FIXME:
599 * should also support 16 bit EXE + DLLs, cursor and animated cursor as
600 * well as bitmap files.
603 UINT WINAPI PrivateExtractIconsW (
604 LPCWSTR lpwstrFile,
605 int nIndex,
606 int sizeX,
607 int sizeY,
608 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
609 UINT* pIconId, /* [out] pointer to array of nIcons icon identifiers or NULL */
610 UINT nIcons, /* [in] number of icons to retrieve */
611 UINT flags ) /* [in] LR_* flags used by LoadImage */
613 TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
614 debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
616 if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
618 WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
620 return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
623 /***********************************************************************
624 * PrivateExtractIconsA [USER32.@]
627 UINT WINAPI PrivateExtractIconsA (
628 LPCSTR lpstrFile,
629 int nIndex,
630 int sizeX,
631 int sizeY,
632 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
633 UINT* piconid, /* [out] pointer to array of nIcons icon identifiers or NULL */
634 UINT nIcons, /* [in] number of icons to retrieve */
635 UINT flags ) /* [in] LR_* flags used by LoadImage */
637 UINT ret;
638 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
639 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
641 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
642 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
644 HeapFree(GetProcessHeap(), 0, lpwstrFile);
645 return ret;
648 /***********************************************************************
649 * PrivateExtractIconExW [USER32.@]
650 * NOTES
651 * if nIndex == -1 it returns the number of icons in any case !!!
653 UINT WINAPI PrivateExtractIconExW (
654 LPCWSTR lpwstrFile,
655 int nIndex,
656 HICON * phIconLarge,
657 HICON * phIconSmall,
658 UINT nIcons )
660 DWORD cyicon, cysmicon, cxicon, cxsmicon;
661 UINT ret = 0;
663 TRACE("%s %d %p %p %d\n",
664 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
666 if (nIndex == -1)
667 /* get the number of icons */
668 return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
670 if (nIcons == 1 && phIconSmall && phIconLarge)
672 HICON hIcon[2];
673 cxicon = GetSystemMetrics(SM_CXICON);
674 cyicon = GetSystemMetrics(SM_CYICON);
675 cxsmicon = GetSystemMetrics(SM_CXSMICON);
676 cysmicon = GetSystemMetrics(SM_CYSMICON);
678 ret = ICO_ExtractIconExW(lpwstrFile, hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
679 cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
680 *phIconLarge = hIcon[0];
681 *phIconSmall = hIcon[1];
682 return ret;
685 if (phIconSmall)
687 /* extract n small icons */
688 cxsmicon = GetSystemMetrics(SM_CXSMICON);
689 cysmicon = GetSystemMetrics(SM_CYSMICON);
690 ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
691 cysmicon, NULL, LR_DEFAULTCOLOR);
693 if (phIconLarge)
695 /* extract n large icons */
696 cxicon = GetSystemMetrics(SM_CXICON);
697 cyicon = GetSystemMetrics(SM_CYICON);
698 ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
699 cyicon, NULL, LR_DEFAULTCOLOR);
701 return ret;
704 /***********************************************************************
705 * PrivateExtractIconExA [USER32.@]
707 UINT WINAPI PrivateExtractIconExA (
708 LPCSTR lpstrFile,
709 int nIndex,
710 HICON * phIconLarge,
711 HICON * phIconSmall,
712 UINT nIcons )
714 UINT ret;
715 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
716 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
718 TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
720 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
721 ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
722 HeapFree(GetProcessHeap(), 0, lpwstrFile);
723 return ret;