kernelbase: Check pointer validity in unsafe_ptr_from_HLOCAL.
[wine.git] / dlls / user32 / exticon.c
bloba9bc922a7824d3fbfdff690d2477ec35e2115f4f
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 <stdarg.h>
26 #include <string.h>
27 #include <stdlib.h> /* abs() */
28 #include <sys/types.h>
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "user_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(icon);
42 #include "pshpack1.h"
44 typedef struct
46 BYTE bWidth; /* Width, in pixels, of the image */
47 BYTE bHeight; /* Height, in pixels, of the image */
48 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
49 BYTE bReserved; /* Reserved ( must be 0) */
50 WORD wPlanes; /* Color Planes */
51 WORD wBitCount; /* Bits per pixel */
52 DWORD dwBytesInRes; /* How many bytes in this resource? */
53 DWORD dwImageOffset; /* Where in the file is this image? */
54 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
56 typedef struct
58 WORD idReserved; /* Reserved (must be 0) */
59 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
60 WORD idCount; /* How many images */
61 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
62 } icoICONDIR, *LPicoICONDIR;
64 typedef struct
66 WORD offset;
67 WORD length;
68 WORD flags;
69 WORD id;
70 WORD handle;
71 WORD usage;
72 } NE_NAMEINFO;
74 typedef struct
76 WORD type_id;
77 WORD count;
78 DWORD resloader;
79 } NE_TYPEINFO;
81 #define NE_RSCTYPE_ICON 0x8003
82 #define NE_RSCTYPE_GROUP_ICON 0x800e
84 #include "poppack.h"
86 #if 0
87 static void dumpIcoDirEntry ( LPicoICONDIRENTRY entry )
89 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
90 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
91 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
92 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
94 static void dumpIcoDir ( LPicoICONDIR entry )
96 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
98 #endif
100 /**********************************************************************
101 * find_entry_by_id
103 * Find an entry by id in a resource directory
104 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
106 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
107 WORD id, const void *root )
109 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
110 int min, max, pos;
112 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
113 min = dir->NumberOfNamedEntries;
114 max = min + dir->NumberOfIdEntries - 1;
115 while (min <= max)
117 pos = (min + max) / 2;
118 if (entry[pos].u.Id == id)
119 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
120 if (entry[pos].u.Id > id) max = pos - 1;
121 else min = pos + 1;
123 return NULL;
126 /**********************************************************************
127 * find_entry_default
129 * Find a default entry in a resource directory
130 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
132 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
133 const void *root )
135 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
136 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
137 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s2.OffsetToDirectory);
140 /*************************************************************************
141 * USER32_GetResourceTable
143 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
145 IMAGE_DOS_HEADER * mz_header;
147 TRACE("%p %p\n", peimage, retptr);
149 *retptr = NULL;
151 mz_header = (IMAGE_DOS_HEADER*) peimage;
153 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
155 if (mz_header->e_cblp == 1) /* .ICO file ? */
157 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
158 return 1;
160 else
161 return 0; /* failed */
163 if (mz_header->e_lfanew >= pesize) {
164 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
166 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
167 return IMAGE_NT_SIGNATURE;
169 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
171 IMAGE_OS2_HEADER * ne_header;
173 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
175 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
176 return 0;
178 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
179 *retptr = (LPBYTE)-1;
180 else
181 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
183 return IMAGE_OS2_SIGNATURE;
185 return 0; /* failed */
187 /*************************************************************************
188 * USER32_LoadResource
190 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
192 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
194 *uSize = (DWORD)pNInfo->length << sizeShift;
195 return peimage + ((DWORD)pNInfo->offset << sizeShift);
198 /*************************************************************************
199 * ICO_LoadIcon
201 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
203 TRACE("%p %p\n", peimage, lpiIDE);
205 *uSize = lpiIDE->dwBytesInRes;
206 return peimage + lpiIDE->dwImageOffset;
209 /*************************************************************************
210 * ICO_GetIconDirectory
212 * Reads .ico file and build phony ICONDIR struct
214 static BYTE * ICO_GetIconDirectory( LPBYTE peimage, LPicoICONDIR* lplpiID, ULONG *uSize )
216 CURSORICONFILEDIR *lpcid; /* icon resource in resource-dir format */
217 CURSORICONDIR * lpID; /* icon resource in resource format */
218 int i;
220 TRACE("%p %p\n", peimage, lplpiID);
222 lpcid = (CURSORICONFILEDIR*)peimage;
224 if( lpcid->idReserved || (lpcid->idType != 1) || (!lpcid->idCount) )
225 return 0;
227 /* allocate the phony ICONDIR structure */
228 *uSize = FIELD_OFFSET(CURSORICONDIR, idEntries[lpcid->idCount]);
229 if( (lpID = HeapAlloc(GetProcessHeap(),0, *uSize) ))
231 /* copy the header */
232 lpID->idReserved = lpcid->idReserved;
233 lpID->idType = lpcid->idType;
234 lpID->idCount = lpcid->idCount;
236 /* copy the entries */
237 for( i=0; i < lpcid->idCount; i++ )
239 memcpy(&lpID->idEntries[i], &lpcid->idEntries[i], sizeof(CURSORICONDIRENTRY) - 2);
240 lpID->idEntries[i].wResId = i;
243 *lplpiID = (LPicoICONDIR)peimage;
244 return (BYTE *)lpID;
246 return 0;
249 /*************************************************************************
250 * ICO_ExtractIconExW [internal]
252 * NOTES
253 * nIcons = 0: returns number of Icons in file
255 * returns
256 * invalid file: -1
257 * failure:0;
258 * success: number of icons in file (nIcons = 0) or nr of icons retrieved
260 static UINT ICO_ExtractIconExW(
261 LPCWSTR lpszExeFileName,
262 HICON * RetPtr,
263 INT nIconIndex,
264 UINT nIcons,
265 UINT cxDesired,
266 UINT cyDesired,
267 UINT *pIconId,
268 UINT flags)
270 UINT ret = 0;
271 UINT cx1, cx2, cy1, cy2;
272 LPBYTE pData;
273 DWORD sig;
274 HANDLE hFile;
275 UINT16 iconDirCount = 0,iconCount = 0;
276 LPBYTE peimage;
277 HANDLE fmapping;
278 DWORD fsizeh,fsizel;
279 WCHAR szExePath[MAX_PATH];
280 DWORD dwSearchReturn;
282 TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
284 dwSearchReturn = SearchPathW(NULL, lpszExeFileName, NULL, ARRAY_SIZE(szExePath), szExePath, NULL);
285 if ((dwSearchReturn == 0) || (dwSearchReturn > ARRAY_SIZE(szExePath)))
287 WARN("File %s not found or path too long\n", debugstr_w(lpszExeFileName));
288 return -1;
291 hFile = CreateFileW(szExePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
292 if (hFile == INVALID_HANDLE_VALUE) return ret;
293 fsizel = GetFileSize(hFile,&fsizeh);
295 /* Map the file */
296 fmapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
297 CloseHandle(hFile);
298 if (!fmapping)
300 WARN("CreateFileMapping error %d\n", GetLastError() );
301 return 0xFFFFFFFF;
304 if (!(peimage = MapViewOfFile(fmapping, FILE_MAP_READ, 0, 0, 0)))
306 WARN("MapViewOfFile error %d\n", GetLastError() );
307 CloseHandle(fmapping);
308 return 0xFFFFFFFF;
310 CloseHandle(fmapping);
312 cx1 = LOWORD(cxDesired);
313 cx2 = HIWORD(cxDesired);
314 cy1 = LOWORD(cyDesired);
315 cy2 = HIWORD(cyDesired);
317 if (pIconId) /* Invalidate first icon identifier */
318 *pIconId = 0xFFFFFFFF;
320 if (!pIconId) /* if no icon identifier array present use the icon handle array as intermediate storage */
321 pIconId = (UINT*)RetPtr;
323 sig = USER32_GetResourceTable(peimage, fsizel, &pData);
325 /* ico file or NE exe/dll*/
326 if (sig==IMAGE_OS2_SIGNATURE || sig==1) /* .ICO file */
328 BYTE *pCIDir = 0;
329 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
330 NE_NAMEINFO *pIconStorage = NULL;
331 NE_NAMEINFO *pIconDir = NULL;
332 LPicoICONDIR lpiID = NULL;
333 ULONG uSize = 0;
335 TRACE("-- OS2/icon Signature (0x%08x)\n", sig);
337 if (pData == (BYTE*)-1)
339 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
340 if (pCIDir)
342 iconDirCount = 1; iconCount = lpiID->idCount;
343 TRACE("-- icon found %p 0x%08x 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
346 else while (pTInfo->type_id && !(pIconStorage && pIconDir))
348 if (pTInfo->type_id == NE_RSCTYPE_GROUP_ICON) /* find icon directory and icon repository */
350 iconDirCount = pTInfo->count;
351 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
352 TRACE("\tfound directory - %i icon families\n", iconDirCount);
354 if (pTInfo->type_id == NE_RSCTYPE_ICON)
356 iconCount = pTInfo->count;
357 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
358 TRACE("\ttotal icons - %i\n", iconCount);
360 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
363 if ((pIconStorage && pIconDir) || lpiID) /* load resources and create icons */
365 if (nIcons == 0)
367 ret = iconDirCount;
368 if (lpiID) /* *.ico file, deallocate heap pointer*/
369 HeapFree(GetProcessHeap(), 0, pCIDir);
371 else if (nIconIndex < iconDirCount)
373 UINT16 i, icon;
374 if (nIcons > iconDirCount - nIconIndex)
375 nIcons = iconDirCount - nIconIndex;
377 for (i = 0; i < nIcons; i++)
379 /* .ICO files have only one icon directory */
380 if (lpiID == NULL) /* not *.ico */
381 pCIDir = USER32_LoadResource(peimage, pIconDir + i + nIconIndex, *(WORD*)pData, &uSize);
382 pIconId[i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx1, cy1, flags);
383 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(pCIDir, TRUE, cx2, cy2, flags);
385 if (lpiID) /* *.ico file, deallocate heap pointer*/
386 HeapFree(GetProcessHeap(), 0, pCIDir);
388 for (icon = 0; icon < nIcons; icon++)
390 pCIDir = NULL;
391 if (lpiID)
392 pCIDir = ICO_LoadIcon(peimage, lpiID->idEntries + (int)pIconId[icon], &uSize);
393 else
394 for (i = 0; i < iconCount; i++)
395 if (pIconStorage[i].id == ((int)pIconId[icon] | 0x8000) )
396 pCIDir = USER32_LoadResource(peimage, pIconStorage + i, *(WORD*)pData, &uSize);
398 if (pCIDir)
400 RetPtr[icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
401 cx1, cy1, flags);
402 if (cx2 && cy2)
403 RetPtr[++icon] = CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000,
404 cx2, cy2, flags);
406 else
407 RetPtr[icon] = 0;
409 ret = icon; /* return number of retrieved icons */
413 /* end ico file */
415 /* exe/dll */
416 else if( sig == IMAGE_NT_SIGNATURE )
418 BYTE *idata, *igdata;
419 const IMAGE_RESOURCE_DIRECTORY *rootresdir, *iconresdir, *icongroupresdir;
420 const IMAGE_RESOURCE_DATA_ENTRY *idataent, *igdataent;
421 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
422 ULONG size;
423 UINT i;
425 rootresdir = RtlImageDirectoryEntryToData((HMODULE)peimage, FALSE, IMAGE_DIRECTORY_ENTRY_RESOURCE, &size);
426 if (!rootresdir)
428 WARN("haven't found section for resource directory.\n");
429 goto end;
432 /* search for the group icon directory */
433 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICON), rootresdir)))
435 WARN("No Icongroupresourcedirectory!\n");
436 goto end; /* failure */
438 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
440 /* only number of icons requested */
441 if( !pIconId )
443 ret = iconDirCount;
444 goto end; /* success */
447 if( nIconIndex < 0 )
449 /* search resource id */
450 int n = 0;
451 int iId = abs(nIconIndex);
452 const IMAGE_RESOURCE_DIRECTORY_ENTRY* xprdeTmp = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1);
454 while(n<iconDirCount && xprdeTmp)
456 if(xprdeTmp->u.Id == iId)
458 nIconIndex = n;
459 break;
461 n++;
462 xprdeTmp++;
464 if (nIconIndex < 0)
466 WARN("resource id %d not found\n", iId);
467 goto end; /* failure */
470 else
472 /* check nIconIndex to be in range */
473 if (nIconIndex >= iconDirCount)
475 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
476 goto end; /* failure */
480 /* assure we don't get too much */
481 if( nIcons > iconDirCount - nIconIndex )
482 nIcons = iconDirCount - nIconIndex;
484 /* starting from specified index */
485 xresent = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(icongroupresdir+1) + nIconIndex;
487 for (i=0; i < nIcons; i++,xresent++)
489 const IMAGE_RESOURCE_DIRECTORY *resdir;
491 /* go down this resource entry, name */
492 resdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)rootresdir + xresent->u2.s2.OffsetToDirectory);
494 /* default language (0) */
495 resdir = find_entry_default(resdir,rootresdir);
496 igdataent = (const IMAGE_RESOURCE_DATA_ENTRY*)resdir;
498 /* lookup address in mapped image for virtual address */
499 igdata = RtlImageRvaToVa(RtlImageNtHeader((HMODULE)peimage), (HMODULE)peimage, igdataent->OffsetToData, NULL);
500 if (!igdata)
502 FIXME("no matching real address for icongroup!\n");
503 goto end; /* failure */
505 pIconId[i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx1, cy1, flags);
506 if (cx2 && cy2) pIconId[++i] = LookupIconIdFromDirectoryEx(igdata, TRUE, cx2, cy2, flags);
509 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICON),rootresdir)))
511 WARN("No Iconresourcedirectory!\n");
512 goto end; /* failure */
515 for (i=0; i<nIcons; i++)
517 const IMAGE_RESOURCE_DIRECTORY *xresdir;
518 xresdir = find_entry_by_id(iconresdir, LOWORD(pIconId[i]), rootresdir);
519 if( !xresdir )
521 WARN("icon entry %d not found\n", LOWORD(pIconId[i]));
522 RetPtr[i]=0;
523 continue;
525 xresdir = find_entry_default(xresdir, rootresdir);
526 idataent = (const IMAGE_RESOURCE_DATA_ENTRY*)xresdir;
528 idata = RtlImageRvaToVa(RtlImageNtHeader((HMODULE)peimage), (HMODULE)peimage, idataent->OffsetToData, NULL);
529 if (!idata)
531 WARN("no matching real address found for icondata!\n");
532 RetPtr[i]=0;
533 continue;
535 RetPtr[i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx1, cy1, flags);
536 if (cx2 && cy2)
537 RetPtr[++i] = CreateIconFromResourceEx(idata, idataent->Size, TRUE, 0x00030000, cx2, cy2, flags);
539 ret = i; /* return number of retrieved icons */
540 } /* if(sig == IMAGE_NT_SIGNATURE) */
542 end:
543 UnmapViewOfFile(peimage); /* success */
544 return ret;
547 /***********************************************************************
548 * PrivateExtractIconsW [USER32.@]
550 * NOTES
551 * If HIWORD(sizeX) && HIWORD(sizeY) 2 * ((nIcons + 1) MOD 2) icons are
552 * returned, with the LOWORD size icon first and the HIWORD size icon
553 * second.
554 * Also the Windows equivalent does extract icons in a strange way if
555 * nIndex is negative. Our implementation treats a negative nIndex as
556 * looking for that resource identifier for the first icon to retrieve.
558 * FIXME:
559 * should also support 16 bit EXE + DLLs, cursor and animated cursor as
560 * well as bitmap files.
563 UINT WINAPI PrivateExtractIconsW (
564 LPCWSTR lpwstrFile,
565 int nIndex,
566 int sizeX,
567 int sizeY,
568 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
569 UINT* pIconId, /* [out] pointer to array of nIcons icon identifiers or NULL */
570 UINT nIcons, /* [in] number of icons to retrieve */
571 UINT flags ) /* [in] LR_* flags used by LoadImage */
573 TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
574 debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
576 if ((nIcons & 1) && HIWORD(sizeX) && HIWORD(sizeY))
578 WARN("Uneven number %d of icons requested for small and large icons!\n", nIcons);
580 return ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX, sizeY, pIconId, flags);
583 /***********************************************************************
584 * PrivateExtractIconsA [USER32.@]
587 UINT WINAPI PrivateExtractIconsA (
588 LPCSTR lpstrFile,
589 int nIndex,
590 int sizeX,
591 int sizeY,
592 HICON * phicon, /* [out] pointer to array of nIcons HICON handles */
593 UINT* piconid, /* [out] pointer to array of nIcons icon identifiers or NULL */
594 UINT nIcons, /* [in] number of icons to retrieve */
595 UINT flags ) /* [in] LR_* flags used by LoadImage */
597 UINT ret;
598 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
599 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
601 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
602 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
604 HeapFree(GetProcessHeap(), 0, lpwstrFile);
605 return ret;
608 /***********************************************************************
609 * PrivateExtractIconExW [USER32.@]
610 * NOTES
611 * if nIndex == -1 it returns the number of icons in any case !!!
613 UINT WINAPI PrivateExtractIconExW (
614 LPCWSTR lpwstrFile,
615 int nIndex,
616 HICON * phIconLarge,
617 HICON * phIconSmall,
618 UINT nIcons )
620 DWORD cyicon, cysmicon, cxicon, cxsmicon;
621 UINT ret = 0;
623 TRACE("%s %d %p %p %d\n",
624 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
626 if (nIndex == -1)
627 /* get the number of icons */
628 return ICO_ExtractIconExW(lpwstrFile, NULL, 0, 0, 0, 0, NULL, LR_DEFAULTCOLOR);
630 if (nIcons == 1 && phIconSmall && phIconLarge)
632 HICON hIcon[2];
633 cxicon = GetSystemMetrics(SM_CXICON);
634 cyicon = GetSystemMetrics(SM_CYICON);
635 cxsmicon = GetSystemMetrics(SM_CXSMICON);
636 cysmicon = GetSystemMetrics(SM_CYSMICON);
638 ret = ICO_ExtractIconExW(lpwstrFile, hIcon, nIndex, 2, cxicon | (cxsmicon<<16),
639 cyicon | (cysmicon<<16), NULL, LR_DEFAULTCOLOR);
640 *phIconLarge = hIcon[0];
641 *phIconSmall = hIcon[1];
642 return ret;
645 if (phIconSmall)
647 /* extract n small icons */
648 cxsmicon = GetSystemMetrics(SM_CXSMICON);
649 cysmicon = GetSystemMetrics(SM_CYSMICON);
650 ret = ICO_ExtractIconExW(lpwstrFile, phIconSmall, nIndex, nIcons, cxsmicon,
651 cysmicon, NULL, LR_DEFAULTCOLOR);
653 if (phIconLarge)
655 /* extract n large icons */
656 cxicon = GetSystemMetrics(SM_CXICON);
657 cyicon = GetSystemMetrics(SM_CYICON);
658 ret = ICO_ExtractIconExW(lpwstrFile, phIconLarge, nIndex, nIcons, cxicon,
659 cyicon, NULL, LR_DEFAULTCOLOR);
661 return ret;
664 /***********************************************************************
665 * PrivateExtractIconExA [USER32.@]
667 UINT WINAPI PrivateExtractIconExA (
668 LPCSTR lpstrFile,
669 int nIndex,
670 HICON * phIconLarge,
671 HICON * phIconSmall,
672 UINT nIcons )
674 UINT ret;
675 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
676 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
678 TRACE("%s %d %p %p %d\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
680 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
681 ret = PrivateExtractIconExW(lpwstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
682 HeapFree(GetProcessHeap(), 0, lpwstrFile);
683 return ret;