Modify PrivateExtractIcons{AW} to match better current MDSN
[wine/wine64.git] / dlls / user / exticon.c
blob256cfe23489f0ed605fc075422d97c8ed1b48098
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
27 #include <string.h>
28 #include <stdlib.h> /* abs() */
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "winbase.h"
35 #include "windef.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "wine/winbase16.h"
40 #include "cursoricon.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(icon);
45 #include "pshpack1.h"
47 typedef struct
49 BYTE bWidth; /* Width, in pixels, of the image */
50 BYTE bHeight; /* Height, in pixels, of the image */
51 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
52 BYTE bReserved; /* Reserved ( must be 0) */
53 WORD wPlanes; /* Color Planes */
54 WORD wBitCount; /* Bits per pixel */
55 DWORD dwBytesInRes; /* How many bytes in this resource? */
56 DWORD dwImageOffset; /* Where in the file is this image? */
57 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
59 typedef struct
61 WORD idReserved; /* Reserved (must be 0) */
62 WORD idType; /* Resource Type (RES_ICON or RES_CURSOR) */
63 WORD idCount; /* How many images */
64 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
65 } icoICONDIR, *LPicoICONDIR;
67 #include "poppack.h"
69 #if 0
70 static void dumpIcoDirEnty ( LPicoICONDIRENTRY entry )
72 TRACE("width = 0x%08x height = 0x%08x\n", entry->bWidth, entry->bHeight);
73 TRACE("colors = 0x%08x planes = 0x%08x\n", entry->bColorCount, entry->wPlanes);
74 TRACE("bitcount = 0x%08x bytesinres = 0x%08lx offset = 0x%08lx\n",
75 entry->wBitCount, entry->dwBytesInRes, entry->dwImageOffset);
77 static void dumpIcoDir ( LPicoICONDIR entry )
79 TRACE("type = 0x%08x count = 0x%08x\n", entry->idType, entry->idCount);
81 #endif
83 /**********************************************************************
84 * find_entry_by_id
86 * Find an entry by id in a resource directory
87 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
89 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
90 WORD id, const void *root )
92 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
93 int min, max, pos;
95 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
96 min = dir->NumberOfNamedEntries;
97 max = min + dir->NumberOfIdEntries - 1;
98 while (min <= max)
100 pos = (min + max) / 2;
101 if (entry[pos].u1.s2.Id == id)
102 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
103 if (entry[pos].u1.s2.Id > id) max = pos - 1;
104 else min = pos + 1;
106 return NULL;
109 /**********************************************************************
110 * find_entry_default
112 * Find a default entry in a resource directory
113 * Copied from loader/pe_resource.c (FIXME: should use exported resource functions)
115 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
116 const void *root )
118 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
119 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
120 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
123 /*************************************************************************
124 * USER32_GetResourceTable
126 static DWORD USER32_GetResourceTable(LPBYTE peimage,DWORD pesize,LPBYTE *retptr)
128 IMAGE_DOS_HEADER * mz_header;
130 TRACE("%p %p\n", peimage, retptr);
132 *retptr = NULL;
134 mz_header = (IMAGE_DOS_HEADER*) peimage;
136 if (mz_header->e_magic != IMAGE_DOS_SIGNATURE)
138 if (mz_header->e_cblp == 1) /* .ICO file ? */
140 *retptr = (LPBYTE)-1; /* ICONHEADER.idType, must be 1 */
141 return 1;
143 else
144 return 0; /* failed */
146 if (mz_header->e_lfanew >= pesize) {
147 return 0; /* failed, happens with PKZIP DOS Exes for instance. */
149 if (*((DWORD*)(peimage + mz_header->e_lfanew)) == IMAGE_NT_SIGNATURE )
150 return IMAGE_NT_SIGNATURE;
152 if (*((WORD*)(peimage + mz_header->e_lfanew)) == IMAGE_OS2_SIGNATURE )
154 IMAGE_OS2_HEADER * ne_header;
156 ne_header = (IMAGE_OS2_HEADER*)(peimage + mz_header->e_lfanew);
158 if (ne_header->ne_magic != IMAGE_OS2_SIGNATURE)
159 return 0;
161 if( (ne_header->ne_restab - ne_header->ne_rsrctab) <= sizeof(NE_TYPEINFO) )
162 *retptr = (LPBYTE)-1;
163 else
164 *retptr = peimage + mz_header->e_lfanew + ne_header->ne_rsrctab;
166 return IMAGE_OS2_SIGNATURE;
168 return 0; /* failed */
170 /*************************************************************************
171 * USER32_LoadResource
173 static BYTE * USER32_LoadResource( LPBYTE peimage, NE_NAMEINFO* pNInfo, WORD sizeShift, ULONG *uSize)
175 TRACE("%p %p 0x%08x\n", peimage, pNInfo, sizeShift);
177 *uSize = (DWORD)pNInfo->length << sizeShift;
178 return peimage + ((DWORD)pNInfo->offset << sizeShift);
181 /*************************************************************************
182 * ICO_LoadIcon
184 static BYTE * ICO_LoadIcon( LPBYTE peimage, LPicoICONDIRENTRY lpiIDE, ULONG *uSize)
186 TRACE("%p %p\n", peimage, lpiIDE);
188 *uSize = lpiIDE->dwBytesInRes;
189 return peimage + lpiIDE->dwImageOffset;
192 /*************************************************************************
193 * ICO_GetIconDirectory
195 * Reads .ico file and build phony ICONDIR struct
196 * see http://www.microsoft.com/win32dev/ui/icons.htm
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 = (CURSORICONDIR*)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((void*)&(lpID->idEntries[i]),(void*)&(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 * failure:0; success: number of icons in file (nIcons = 0) or nr of icons retrieved
245 static UINT ICO_ExtractIconExW(
246 LPCWSTR lpszExeFileName,
247 HICON * RetPtr,
248 INT nIconIndex,
249 UINT nIcons,
250 UINT cxDesired,
251 UINT cyDesired,
252 UINT *pIconId,
253 UINT flags)
255 UINT ret = 0;
256 LPBYTE pData;
257 DWORD sig;
258 HANDLE hFile;
259 UINT16 iconDirCount = 0,iconCount = 0;
260 LPBYTE peimage;
261 HANDLE fmapping;
262 ULONG uSize;
263 DWORD fsizeh,fsizel;
265 TRACE("%s, %d, %d %p 0x%08x\n", debugstr_w(lpszExeFileName), nIconIndex, nIcons, pIconId, flags);
267 hFile = CreateFileW( lpszExeFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
268 if (hFile == INVALID_HANDLE_VALUE) return ret;
269 fsizel = GetFileSize(hFile,&fsizeh);
271 /* Map the file */
272 fmapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL );
273 CloseHandle( hFile );
274 if (!fmapping)
276 WARN("CreateFileMapping error %ld\n", GetLastError() );
277 return ret;
280 if ( !(peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0)))
282 WARN("MapViewOfFile error %ld\n", GetLastError() );
283 CloseHandle( fmapping );
284 return ret;
286 CloseHandle( fmapping );
288 sig = USER32_GetResourceTable(peimage,fsizel,&pData);
290 /* ico file */
291 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
293 BYTE *pCIDir = 0;
294 NE_TYPEINFO *pTInfo = (NE_TYPEINFO*)(pData + 2);
295 NE_NAMEINFO *pIconStorage = NULL;
296 NE_NAMEINFO *pIconDir = NULL;
297 LPicoICONDIR lpiID = NULL;
299 TRACE("-- OS2/icon Signature (0x%08lx)\n", sig);
301 if( pData == (BYTE*)-1 )
303 /* FIXME: pCIDir is allocated on the heap - memory leak */
304 pCIDir = ICO_GetIconDirectory(peimage, &lpiID, &uSize); /* check for .ICO file */
305 if( pCIDir )
307 iconDirCount = 1; iconCount = lpiID->idCount;
308 TRACE("-- icon found %p 0x%08lx 0x%08x 0x%08x\n", pCIDir, uSize, iconDirCount, iconCount);
311 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
313 if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
315 iconDirCount = pTInfo->count;
316 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
317 TRACE("\tfound directory - %i icon families\n", iconDirCount);
319 if( pTInfo->type_id == NE_RSCTYPE_ICON )
321 iconCount = pTInfo->count;
322 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
323 TRACE("\ttotal icons - %i\n", iconCount);
325 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
328 if( (pIconStorage && pIconDir) || lpiID ) /* load resources and create icons */
330 if( nIcons == 0 )
332 ret = iconDirCount;
334 else if( nIconIndex < iconDirCount )
336 UINT16 i, icon;
337 if( nIcons > iconDirCount - nIconIndex )
338 nIcons = iconDirCount - nIconIndex;
340 for( i = nIconIndex; i < nIconIndex + nIcons; i++ )
342 /* .ICO files have only one icon directory */
343 if( lpiID == NULL ) /* *.ico */
344 pCIDir = USER32_LoadResource( peimage, pIconDir + i, *(WORD*)pData, &uSize );
345 RetPtr[i-nIconIndex] = (HICON)LookupIconIdFromDirectoryEx( pCIDir, TRUE, cxDesired, cyDesired, flags );
348 for( icon = nIconIndex; icon < nIconIndex + nIcons; icon++ )
350 pCIDir = NULL;
351 if( lpiID )
352 pCIDir = ICO_LoadIcon( peimage, lpiID->idEntries + (int)RetPtr[icon-nIconIndex], &uSize);
353 else
354 for ( i = 0; i < iconCount; i++ )
355 if ( pIconStorage[i].id == ((int)RetPtr[icon-nIconIndex] | 0x8000) )
356 pCIDir = USER32_LoadResource( peimage, pIconStorage + i,*(WORD*)pData, &uSize );
358 if( pCIDir )
359 RetPtr[icon - nIconIndex] = (HICON)CreateIconFromResourceEx(pCIDir, uSize, TRUE, 0x00030000, cxDesired, cyDesired, flags);
360 else
361 RetPtr[icon - nIconIndex] = 0;
363 ret = icon - nIconIndex; /* return number of retrieved icons */
367 /* end ico file */
369 /* exe/dll */
370 else if( sig == IMAGE_NT_SIGNATURE )
372 LPBYTE idata,igdata;
373 PIMAGE_DOS_HEADER dheader;
374 PIMAGE_NT_HEADERS pe_header;
375 PIMAGE_SECTION_HEADER pe_sections;
376 const IMAGE_RESOURCE_DIRECTORY *rootresdir,*iconresdir,*icongroupresdir;
377 const IMAGE_RESOURCE_DATA_ENTRY *idataent,*igdataent;
378 const IMAGE_RESOURCE_DIRECTORY_ENTRY *xresent;
379 int i,j;
381 dheader = (PIMAGE_DOS_HEADER)peimage;
382 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew); /* it is a pe header, USER32_GetResourceTable checked that */
383 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header)); /* probably makes problems with short PE headers...*/
384 rootresdir = NULL;
386 /* search for the root resource directory */
387 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
389 if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
390 continue;
391 if (fsizel < pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData) {
392 FIXME("File %s too short (section is at %ld bytes, real size is %ld)\n",
393 debugstr_w(lpszExeFileName),
394 pe_sections[i].PointerToRawData+pe_sections[i].SizeOfRawData,
395 fsizel
397 goto end;
399 /* FIXME: doesn't work when the resources are not in a separate section */
400 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
402 rootresdir = (PIMAGE_RESOURCE_DIRECTORY)(peimage+pe_sections[i].PointerToRawData);
403 break;
407 if (!rootresdir)
409 WARN("haven't found section for resource directory.\n");
410 goto end; /* failure */
413 /* search for the group icon directory */
414 if (!(icongroupresdir = find_entry_by_id(rootresdir, LOWORD(RT_GROUP_ICONW), rootresdir)))
416 WARN("No Icongroupresourcedirectory!\n");
417 goto end; /* failure */
419 iconDirCount = icongroupresdir->NumberOfNamedEntries + icongroupresdir->NumberOfIdEntries;
421 /* only number of icons requested */
422 if( nIcons == 0 )
424 ret = iconDirCount;
425 goto end; /* success */
428 if( nIconIndex < 0 )
430 /* search resource id */
431 int n = 0;
432 int iId = abs(nIconIndex);
433 PIMAGE_RESOURCE_DIRECTORY_ENTRY xprdeTmp = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
435 while(n<iconDirCount && xprdeTmp)
437 if(xprdeTmp->u1.s2.Id == iId)
439 nIconIndex = n;
440 break;
442 n++;
443 xprdeTmp++;
445 if (nIconIndex < 0)
447 WARN("resource id %d not found\n", iId);
448 goto end; /* failure */
451 else
453 /* check nIconIndex to be in range */
454 if (nIconIndex >= iconDirCount)
456 WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
457 goto end; /* failure */
461 /* assure we don't get too much */
462 if( nIcons > iconDirCount - nIconIndex )
463 nIcons = iconDirCount - nIconIndex;
465 /* starting from specified index */
466 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1) + nIconIndex;
468 for (i=0; i < nIcons; i++,xresent++)
470 const IMAGE_RESOURCE_DIRECTORY *resdir;
472 /* go down this resource entry, name */
473 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s3.OffsetToDirectory));
475 /* default language (0) */
476 resdir = find_entry_default(resdir,rootresdir);
477 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
479 /* lookup address in mapped image for virtual address */
480 igdata = NULL;
482 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
484 if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
485 continue;
486 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
487 continue;
489 if (igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData+igdataent->Size > fsizel) {
490 FIXME("overflow in PE lookup (%s has len %ld, have offset %ld), short file?\n", debugstr_w(lpszExeFileName), fsizel,
491 igdataent->OffsetToData - pe_sections[j].VirtualAddress + pe_sections[j].PointerToRawData + igdataent->Size);
492 goto end; /* failure */
494 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
497 if (!igdata)
499 FIXME("no matching real address for icongroup!\n");
500 goto end; /* failure */
502 RetPtr[i] = (HICON)LookupIconIdFromDirectoryEx(igdata, TRUE, cxDesired, cyDesired, flags);
505 if (pIconId)
506 *pIconId = LOWORD(*RetPtr);
508 if (!(iconresdir=find_entry_by_id(rootresdir,LOWORD(RT_ICONW),rootresdir)))
510 WARN("No Iconresourcedirectory!\n");
511 goto end; /* failure */
514 for (i=0; i<nIcons; i++)
516 const IMAGE_RESOURCE_DIRECTORY *xresdir;
517 xresdir = find_entry_by_id(iconresdir,LOWORD(RetPtr[i]),rootresdir);
518 xresdir = find_entry_default(xresdir,rootresdir);
519 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
520 idata = NULL;
522 /* map virtual to address in image */
523 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
525 if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
526 continue;
527 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
528 continue;
529 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
531 if (!idata)
533 WARN("no matching real address found for icondata!\n");
534 RetPtr[i]=0;
535 continue;
537 RetPtr[i] = (HICON) CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000, cxDesired, cyDesired, 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 * nIndex = 1: a small and a large icon are extracted.
552 * the higher word of sizeXY contains the size of the small icon, the lower
553 * word the size of the big icon. phicon points to HICON[2].
555 * FIXME:
556 * 1) should also support 16 bit EXE + DLLs, cursor and animated cursor as well
557 * as bitmap files.
558 * 2) should return according to MSDN
559 * phicons == NULL : the number of icons in file or 0 on error
560 * phicons != NULL : the number of icons extracted or 0xFFFFFFFF on error
561 * does return in Win2000
562 * when file valid the number of icons or 0 on any error
563 * when file invalid and phicon == NULL returns 0
564 * when file invalid and phicon != NULL returns 0xFFFFFFFF
566 * *pIconID is always set to 0 when file invalid
567 * *pIconID is always set to 0xFFFFFFFF for valid icon and cursor files
568 * *pIconID is set to actual identifier for valid dll/exes or -1 on error
571 UINT WINAPI PrivateExtractIconsW (
572 LPCWSTR lpwstrFile,
573 int nIndex,
574 int sizeX,
575 int sizeY,
576 HICON * phicon, /* [out] pointer to array of HICON handles */
577 UINT* pIconId, /* [out] pointer to returned icon identifier which fits best */
578 UINT nIcons, /* [in] number of icons to retrieve */
579 UINT flags ) /* [in] LR_* flags used by LoadImage */
581 UINT ret;
582 TRACE("%s %d %dx%d %p %p %d 0x%08x\n",
583 debugstr_w(lpwstrFile), nIndex, sizeX, sizeY, phicon, pIconId, nIcons, flags);
585 if (pIconId) /* Invalidate icon identifier on entry */
586 *pIconId = 0xFFFFFFFF;
588 if (!phicon)
589 return ICO_ExtractIconExW(lpwstrFile, NULL, nIndex, 0, sizeX & 0xffff, sizeY & 0xffff, pIconId, flags);
591 if ((nIcons == 2) && HIWORD(sizeX) && HIWORD(sizeY))
593 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, 1, sizeX & 0xffff, sizeY & 0xffff, pIconId, flags);
594 if (!SUCCEEDED(ret)) return ret;
595 ret = ICO_ExtractIconExW(lpwstrFile, phicon+1, nIndex, 1, (sizeX>>16) & 0xffff, (sizeY>>16) & 0xffff, pIconId, flags);
596 } else
597 ret = ICO_ExtractIconExW(lpwstrFile, phicon, nIndex, nIcons, sizeX & 0xffff, sizeY & 0xffff, pIconId, flags);
598 return ret;
601 /***********************************************************************
602 * PrivateExtractIconsA [USER32.@]
605 UINT WINAPI PrivateExtractIconsA (
606 LPCSTR lpstrFile,
607 int nIndex,
608 int sizeX,
609 int sizeY,
610 HICON * phicon,
611 UINT* piconid, /* [out] pointer to returned icon identifier which fits best */
612 UINT nIcons, /* [in] number of icons to retrieve */
613 UINT flags ) /* [in] LR_* flags used by LoadImage */
615 UINT ret;
616 INT len = MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, NULL, 0);
617 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
619 MultiByteToWideChar(CP_ACP, 0, lpstrFile, -1, lpwstrFile, len);
620 ret = PrivateExtractIconsW(lpwstrFile, nIndex, sizeX, sizeY, phicon, piconid, nIcons, flags);
622 HeapFree(GetProcessHeap(), 0, lpwstrFile);
623 return ret;
626 /***********************************************************************
627 * PrivateExtractIconExW [USER32.@]
628 * NOTES
629 * if nIcons = -1 it returns the number of icons in any case !!!
631 HRESULT WINAPI PrivateExtractIconExW (
632 LPCWSTR lpwstrFile,
633 DWORD nIndex,
634 HICON * phIconLarge,
635 HICON * phIconSmall,
636 UINT nIcons )
638 DWORD cyicon, cysmicon, cxicon, cxsmicon;
639 HRESULT ret = 0;
641 TRACE("%s 0x%08lx %p %p 0x%08x\n",
642 debugstr_w(lpwstrFile),nIndex,phIconLarge, phIconSmall, nIcons);
644 if (nIndex == 1 && phIconSmall && phIconLarge)
646 HICON hIcon[2];
647 cxicon = GetSystemMetrics(SM_CXICON);
648 cyicon = GetSystemMetrics(SM_CYICON);
649 cxsmicon = GetSystemMetrics(SM_CXSMICON);
650 cysmicon = GetSystemMetrics(SM_CYSMICON);
652 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon | (cxsmicon<<16), cyicon | (cysmicon<<16),
653 (HICON*) &hIcon, 0, 2, 0 );
654 *phIconLarge = hIcon[0];
655 *phIconSmall = hIcon[1];
656 return ret;
659 if (nIndex != -1)
661 if (phIconSmall)
663 /* extract n small icons */
664 cxsmicon = GetSystemMetrics(SM_CXSMICON);
665 cysmicon = GetSystemMetrics(SM_CYSMICON);
666 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxsmicon, cysmicon, phIconSmall, 0, nIcons, 0 );
668 if (phIconLarge )
670 /* extract n large icons */
671 cxicon = GetSystemMetrics(SM_CXICON);
672 cyicon = GetSystemMetrics(SM_CYICON);
673 ret = PrivateExtractIconsW ( lpwstrFile, nIndex, cxicon, cyicon, phIconLarge, 0, nIcons, 0 );
675 return ret;
678 /* get the number of icons */
679 return PrivateExtractIconsW ( lpwstrFile, 0, 0, 0, 0, 0, 0, 0 );
682 /***********************************************************************
683 * PrivateExtractIconExA [USER32.@]
685 HRESULT WINAPI PrivateExtractIconExA (
686 LPCSTR lpstrFile,
687 DWORD nIndex,
688 HICON * phIconLarge,
689 HICON * phIconSmall,
690 UINT nIcons )
692 DWORD ret;
693 INT len = MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, NULL, 0 );
694 LPWSTR lpwstrFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
696 TRACE("%s 0x%08lx %p %p 0x%08x\n", lpstrFile, nIndex, phIconLarge, phIconSmall, nIcons);
698 MultiByteToWideChar( CP_ACP, 0, lpstrFile, -1, lpwstrFile, len );
699 ret = PrivateExtractIconExW(lpwstrFile,nIndex,phIconLarge, phIconSmall, nIcons);
700 HeapFree(GetProcessHeap(), 0, lpwstrFile);
701 return ret;