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