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