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