EnumThemeColors() and EnumThemeSizes() actually do not return a single
[wine] / dlls / shell32 / shell32_main.c
1 /*
2  *                 Shell basics
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Juergen Schmied (jsch)  *  <juergen.schmied@metronet.de>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "dlgs.h"
36 #include "shellapi.h"
37 #include "winuser.h"
38 #include "wingdi.h"
39 #include "shlobj.h"
40 #include "shlguid.h"
41 #include "shlwapi.h"
42
43 #include "undocshell.h"
44 #include "pidl.h"
45 #include "shell32_main.h"
46 #include "version.h"
47 #include "shresdef.h"
48
49 #include "wine/debug.h"
50 #include "wine/unicode.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(shell);
53
54 extern const char * const SHELL_Authors[];
55
56 #define MORE_DEBUG 1
57 /*************************************************************************
58  * CommandLineToArgvW            [SHELL32.@]
59  *
60  * We must interpret the quotes in the command line to rebuild the argv
61  * array correctly:
62  * - arguments are separated by spaces or tabs
63  * - quotes serve as optional argument delimiters
64  *   '"a b"'   -> 'a b'
65  * - escaped quotes must be converted back to '"'
66  *   '\"'      -> '"'
67  * - an odd number of '\'s followed by '"' correspond to half that number
68  *   of '\' followed by a '"' (extension of the above)
69  *   '\\\"'    -> '\"'
70  *   '\\\\\"'  -> '\\"'
71  * - an even number of '\'s followed by a '"' correspond to half that number
72  *   of '\', plus a regular quote serving as an argument delimiter (which
73  *   means it does not appear in the result)
74  *   'a\\"b c"'   -> 'a\b c'
75  *   'a\\\\"b c"' -> 'a\\b c'
76  * - '\' that are not followed by a '"' are copied literally
77  *   'a\b'     -> 'a\b'
78  *   'a\\b'    -> 'a\\b'
79  *
80  * Note:
81  * '\t' == 0x0009
82  * ' '  == 0x0020
83  * '"'  == 0x0022
84  * '\\' == 0x005c
85  */
86 LPWSTR* WINAPI CommandLineToArgvW(LPCWSTR lpCmdline, int* numargs)
87 {
88     DWORD argc;
89     HGLOBAL hargv;
90     LPWSTR  *argv;
91     LPCWSTR cs;
92     LPWSTR arg,s,d;
93     LPWSTR cmdline;
94     int in_quotes,bcount;
95
96     if (*lpCmdline==0)
97     {
98         /* Return the path to the executable */
99         DWORD len, size=16;
100
101         hargv=GlobalAlloc(size, 0);
102         argv=GlobalLock(hargv);
103         for (;;)
104         {
105             len = GetModuleFileNameW(0, (LPWSTR)(argv+1), size-sizeof(LPWSTR));
106             if (!len)
107             {
108                 GlobalFree(hargv);
109                 return NULL;
110             }
111             if (len < size) break;
112             size*=2;
113             hargv=GlobalReAlloc(hargv, size, 0);
114             argv=GlobalLock(hargv);
115         }
116         argv[0]=(LPWSTR)(argv+1);
117         if (numargs)
118             *numargs=2;
119
120         return argv;
121     }
122
123     /* to get a writeable copy */
124     argc=0;
125     bcount=0;
126     in_quotes=0;
127     cs=lpCmdline;
128     while (1)
129     {
130         if (*cs==0 || ((*cs==0x0009 || *cs==0x0020) && !in_quotes))
131         {
132             /* space */
133             argc++;
134             /* skip the remaining spaces */
135             while (*cs==0x0009 || *cs==0x0020) {
136                 cs++;
137             }
138             if (*cs==0)
139                 break;
140             bcount=0;
141             continue;
142         }
143         else if (*cs==0x005c)
144         {
145             /* '\', count them */
146             bcount++;
147         }
148         else if ((*cs==0x0022) && ((bcount & 1)==0))
149         {
150             /* unescaped '"' */
151             in_quotes=!in_quotes;
152             bcount=0;
153         }
154         else
155         {
156             /* a regular character */
157             bcount=0;
158         }
159         cs++;
160     }
161     /* Allocate in a single lump, the string array, and the strings that go with it.
162      * This way the caller can make a single GlobalFree call to free both, as per MSDN.
163      */
164     hargv=GlobalAlloc(0, argc*sizeof(LPWSTR)+(strlenW(lpCmdline)+1)*sizeof(WCHAR));
165     argv=GlobalLock(hargv);
166     if (!argv)
167         return NULL;
168     cmdline=(LPWSTR)(argv+argc);
169     strcpyW(cmdline, lpCmdline);
170
171     argc=0;
172     bcount=0;
173     in_quotes=0;
174     arg=d=s=cmdline;
175     while (*s)
176     {
177         if ((*s==0x0009 || *s==0x0020) && !in_quotes)
178         {
179             /* Close the argument and copy it */
180             *d=0;
181             argv[argc++]=arg;
182
183             /* skip the remaining spaces */
184             do {
185                 s++;
186             } while (*s==0x0009 || *s==0x0020);
187
188             /* Start with a new argument */
189             arg=d=s;
190             bcount=0;
191         }
192         else if (*s==0x005c)
193         {
194             /* '\\' */
195             *d++=*s++;
196             bcount++;
197         }
198         else if (*s==0x0022)
199         {
200             /* '"' */
201             if ((bcount & 1)==0)
202             {
203                 /* Preceded by an even number of '\', this is half that
204                  * number of '\', plus a quote which we erase.
205                  */
206                 d-=bcount/2;
207                 in_quotes=!in_quotes;
208                 s++;
209             }
210             else
211             {
212                 /* Preceded by an odd number of '\', this is half that
213                  * number of '\' followed by a '"'
214                  */
215                 d=d-bcount/2-1;
216                 *d++='"';
217                 s++;
218             }
219             bcount=0;
220         }
221         else
222         {
223             /* a regular character */
224             *d++=*s++;
225             bcount=0;
226         }
227     }
228     if (*arg)
229     {
230         *d='\0';
231         argv[argc++]=arg;
232     }
233     if (numargs)
234         *numargs=argc;
235
236     return argv;
237 }
238
239 static DWORD shgfi_get_exe_type(LPCWSTR szFullPath)
240 {
241     BOOL status = FALSE;
242     HANDLE hfile;
243     DWORD BinaryType;
244     IMAGE_DOS_HEADER mz_header;
245     IMAGE_NT_HEADERS nt;
246     DWORD len;
247     char magic[4];
248
249     status = GetBinaryTypeW (szFullPath, &BinaryType);
250     if (!status)
251         return 0;
252     if (BinaryType == SCS_DOS_BINARY || BinaryType == SCS_PIF_BINARY)
253         return 0x4d5a;
254
255     hfile = CreateFileW( szFullPath, GENERIC_READ, FILE_SHARE_READ,
256                          NULL, OPEN_EXISTING, 0, 0 );
257     if ( hfile == INVALID_HANDLE_VALUE )
258         return 0;
259
260     /*
261      * The next section is adapted from MODULE_GetBinaryType, as we need
262      * to examine the image header to get OS and version information. We
263      * know from calling GetBinaryTypeA that the image is valid and either
264      * an NE or PE, so much error handling can be omitted.
265      * Seek to the start of the file and read the header information.
266      */
267
268     SetFilePointer( hfile, 0, NULL, SEEK_SET );
269     ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL );
270
271     SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
272     ReadFile( hfile, magic, sizeof(magic), &len, NULL );
273     if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
274     {
275         SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
276         ReadFile( hfile, &nt, sizeof(nt), &len, NULL );
277         CloseHandle( hfile );
278         if (nt.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
279         {
280              return IMAGE_NT_SIGNATURE | 
281                    (nt.OptionalHeader.MajorSubsystemVersion << 24) |
282                    (nt.OptionalHeader.MinorSubsystemVersion << 16);
283         }
284         return IMAGE_NT_SIGNATURE;
285     }
286     else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
287     {
288         IMAGE_OS2_HEADER ne;
289         SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
290         ReadFile( hfile, &ne, sizeof(ne), &len, NULL );
291         CloseHandle( hfile );
292         if (ne.ne_exetyp == 2)
293             return IMAGE_OS2_SIGNATURE | (ne.ne_expver << 16);
294         return 0;
295     }
296     CloseHandle( hfile );
297     return 0;
298 }
299
300 #define SHGFI_KNOWN_FLAGS \
301     (SHGFI_SMALLICON | SHGFI_OPENICON | SHGFI_SHELLICONSIZE | SHGFI_PIDL | \
302      SHGFI_USEFILEATTRIBUTES | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX | \
303      SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ATTRIBUTES | \
304      SHGFI_ICONLOCATION | SHGFI_EXETYPE | SHGFI_SYSICONINDEX | \
305      SHGFI_LINKOVERLAY | SHGFI_SELECTED | SHGFI_ATTR_SPECIFIED)
306
307 /*************************************************************************
308  * SHGetFileInfoW            [SHELL32.@]
309  *
310  */
311 DWORD WINAPI SHGetFileInfoW(LPCWSTR path,DWORD dwFileAttributes,
312                             SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags )
313 {
314     WCHAR szLocation[MAX_PATH], szFullPath[MAX_PATH];
315     int iIndex;
316     DWORD ret = TRUE, dwAttributes = 0;
317     IShellFolder * psfParent = NULL;
318     IExtractIconW * pei = NULL;
319     LPITEMIDLIST    pidlLast = NULL, pidl = NULL;
320     HRESULT hr = S_OK;
321     BOOL IconNotYetLoaded=TRUE;
322
323     TRACE("%s fattr=0x%lx sfi=%p(attr=0x%08lx) size=0x%x flags=0x%x\n",
324           (flags & SHGFI_PIDL)? "pidl" : debugstr_w(path), dwFileAttributes,
325           psfi, psfi->dwAttributes, sizeofpsfi, flags);
326
327     if ( (flags & SHGFI_USEFILEATTRIBUTES) && 
328          (flags & (SHGFI_ATTRIBUTES|SHGFI_EXETYPE|SHGFI_PIDL)))
329         return FALSE;
330
331     /* windows initializes this values regardless of the flags */
332     if (psfi != NULL)
333     {
334         psfi->szDisplayName[0] = '\0';
335         psfi->szTypeName[0] = '\0';
336         psfi->iIcon = 0;
337     }
338
339     if (!(flags & SHGFI_PIDL))
340     {
341         /* SHGetFileInfo should work with absolute and relative paths */
342         if (PathIsRelativeW(path))
343         {
344             GetCurrentDirectoryW(MAX_PATH, szLocation);
345             PathCombineW(szFullPath, szLocation, path);
346         }
347         else
348         {
349             lstrcpynW(szFullPath, path, MAX_PATH);
350         }
351     }
352
353     if (flags & SHGFI_EXETYPE)
354     {
355         if (flags != SHGFI_EXETYPE)
356             return 0;
357         return shgfi_get_exe_type(szFullPath);
358     }
359
360     /*
361      * psfi is NULL normally to query EXE type. If it is NULL, none of the
362      * below makes sense anyway. Windows allows this and just returns FALSE
363      */
364     if (psfi == NULL)
365         return FALSE;
366
367     /*
368      * translate the path into a pidl only when SHGFI_USEFILEATTRIBUTES
369      * is not specified.
370      * The pidl functions fail on not existing file names
371      */
372
373     if (flags & SHGFI_PIDL)
374     {
375         pidl = ILClone((LPCITEMIDLIST)path);
376     }
377     else if (!(flags & SHGFI_USEFILEATTRIBUTES))
378     {
379         hr = SHILCreateFromPathW(szFullPath, &pidl, &dwAttributes);
380     }
381
382     if ((flags & SHGFI_PIDL) || !(flags & SHGFI_USEFILEATTRIBUTES))
383     {
384         /* get the parent shellfolder */
385         if (pidl)
386         {
387             hr = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&psfParent,
388                                 (LPCITEMIDLIST*)&pidlLast );
389             if (SUCCEEDED(hr))
390                 pidlLast = ILClone(pidlLast);
391             ILFree(pidl);
392         }
393         else
394         {
395             ERR("pidl is null!\n");
396             return FALSE;
397         }
398     }
399
400     /* get the attributes of the child */
401     if (SUCCEEDED(hr) && (flags & SHGFI_ATTRIBUTES))
402     {
403         if (!(flags & SHGFI_ATTR_SPECIFIED))
404         {
405             psfi->dwAttributes = 0xffffffff;
406         }
407         IShellFolder_GetAttributesOf( psfParent, 1, (LPCITEMIDLIST*)&pidlLast,
408                                       &(psfi->dwAttributes) );
409     }
410
411     /* get the displayname */
412     if (SUCCEEDED(hr) && (flags & SHGFI_DISPLAYNAME))
413     {
414         if (flags & SHGFI_USEFILEATTRIBUTES)
415         {
416             lstrcpyW (psfi->szDisplayName, PathFindFileNameW(szFullPath));
417         }
418         else
419         {
420             STRRET str;
421             hr = IShellFolder_GetDisplayNameOf( psfParent, pidlLast,
422                                                 SHGDN_INFOLDER, &str);
423             StrRetToStrNW (psfi->szDisplayName, MAX_PATH, &str, pidlLast);
424         }
425     }
426
427     /* get the type name */
428     if (SUCCEEDED(hr) && (flags & SHGFI_TYPENAME))
429     {
430         static const WCHAR szFile[] = { 'F','i','l','e',0 };
431         static const WCHAR szDashFile[] = { '-','f','i','l','e',0 };
432
433         if (!(flags & SHGFI_USEFILEATTRIBUTES))
434         {
435             char ftype[80];
436
437             _ILGetFileType(pidlLast, ftype, 80);
438             MultiByteToWideChar(CP_ACP, 0, ftype, -1, psfi->szTypeName, 80 );
439         }
440         else
441         {
442             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
443                 strcatW (psfi->szTypeName, szFile);
444             else 
445             {
446                 WCHAR sTemp[64];
447
448                 lstrcpyW(sTemp,PathFindExtensionW(szFullPath));
449                 if (!( HCR_MapTypeToValueW(sTemp, sTemp, 64, TRUE) &&
450                     HCR_MapTypeToValueW(sTemp, psfi->szTypeName, 80, FALSE )))
451                 {
452                     lstrcpynW (psfi->szTypeName, sTemp, 64);
453                     strcatW (psfi->szTypeName, szDashFile);
454                 }
455             }
456         }
457     }
458
459     /* ### icons ###*/
460     if (flags & SHGFI_ADDOVERLAYS)
461         FIXME("SHGFI_ADDOVERLAYS unhandled\n");
462
463     if (flags & SHGFI_OVERLAYINDEX)
464         FIXME("SHGFI_OVERLAYINDEX unhandled\n");
465
466     if (flags & SHGFI_LINKOVERLAY)
467         FIXME("set icon to link, stub\n");
468
469     if (flags & SHGFI_SELECTED)
470         FIXME("set icon to selected, stub\n");
471
472     if (flags & SHGFI_SHELLICONSIZE)
473         FIXME("set icon to shell size, stub\n");
474
475     /* get the iconlocation */
476     if (SUCCEEDED(hr) && (flags & SHGFI_ICONLOCATION ))
477     {
478         UINT uDummy,uFlags;
479
480         hr = IShellFolder_GetUIObjectOf(psfParent, 0, 1,
481                (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA,
482                &uDummy, (LPVOID*)&pei);
483         if (SUCCEEDED(hr))
484         {
485             hr = IExtractIconW_GetIconLocation(pei, 
486                     (flags & SHGFI_OPENICON)? GIL_OPENICON : 0,
487                     szLocation, MAX_PATH, &iIndex, &uFlags);
488             psfi->iIcon = iIndex;
489
490             if (uFlags != GIL_NOTFILENAME)
491                 lstrcpyW (psfi->szDisplayName, szLocation);
492             else
493                 ret = FALSE;
494
495             IExtractIconA_Release(pei);
496         }
497     }
498
499     /* get icon index (or load icon)*/
500     if (SUCCEEDED(hr) && (flags & (SHGFI_ICON | SHGFI_SYSICONINDEX)))
501     {
502         if (flags & SHGFI_USEFILEATTRIBUTES)
503         {
504             WCHAR sTemp [MAX_PATH];
505             WCHAR * szExt;
506             DWORD dwNr=0;
507
508             lstrcpynW(sTemp, szFullPath, MAX_PATH);
509
510             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
511                 psfi->iIcon = SIC_GetIconIndex(swShell32Name, -IDI_SHELL_FOLDER, 0);
512             else
513             {
514                 static const WCHAR p1W[] = {'%','1',0};
515
516                 psfi->iIcon = 0;
517                 szExt = (LPWSTR) PathFindExtensionW(sTemp);
518                 if ( szExt &&
519                      HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE) &&
520                      HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &dwNr))
521                 {
522                     if (!lstrcmpW(p1W,sTemp))            /* icon is in the file */
523                         strcpyW(sTemp, szFullPath);
524
525                     if (flags & SHGFI_SYSICONINDEX) 
526                     {
527                         psfi->iIcon = SIC_GetIconIndex(sTemp,dwNr,0);
528                         if (psfi->iIcon == -1)
529                             psfi->iIcon = 0;
530                     }
531                     else 
532                     {
533                         IconNotYetLoaded=FALSE;
534                         if (flags & SHGFI_SMALLICON)
535                             PrivateExtractIconsW( sTemp,dwNr,
536                                 GetSystemMetrics( SM_CXSMICON ),
537                                 GetSystemMetrics( SM_CYSMICON ),
538                                 &psfi->hIcon, 0, 1, 0);
539                         else
540                             PrivateExtractIconsW( sTemp, dwNr,
541                                 GetSystemMetrics( SM_CXICON),
542                                 GetSystemMetrics( SM_CYICON),
543                                 &psfi->hIcon, 0, 1, 0);
544                         psfi->iIcon = dwNr;
545                     }
546                 }
547             }
548         }
549         else
550         {
551             if (!(PidlToSicIndex(psfParent, pidlLast, !(flags & SHGFI_SMALLICON),
552                 (flags & SHGFI_OPENICON)? GIL_OPENICON : 0, &(psfi->iIcon))))
553             {
554                 ret = FALSE;
555             }
556         }
557         if (ret)
558         {
559             if (flags & SHGFI_SMALLICON)
560                 ret = (DWORD) ShellSmallIconList;
561             else
562                 ret = (DWORD) ShellBigIconList;
563         }
564     }
565
566     /* icon handle */
567     if (SUCCEEDED(hr) && (flags & SHGFI_ICON) && IconNotYetLoaded)
568     {
569         if (flags & SHGFI_SMALLICON)
570             psfi->hIcon = ImageList_GetIcon( ShellSmallIconList, psfi->iIcon, ILD_NORMAL);
571         else
572             psfi->hIcon = ImageList_GetIcon( ShellBigIconList, psfi->iIcon, ILD_NORMAL);
573     }
574
575     if (flags & ~SHGFI_KNOWN_FLAGS)
576         FIXME("unknown flags %08x\n", flags & ~SHGFI_KNOWN_FLAGS);
577
578     if (psfParent)
579         IShellFolder_Release(psfParent);
580
581     if (hr != S_OK)
582         ret = FALSE;
583
584     if (pidlLast)
585         SHFree(pidlLast);
586
587 #ifdef MORE_DEBUG
588     TRACE ("icon=%p index=0x%08x attr=0x%08lx name=%s type=%s ret=0x%08lx\n",
589            psfi->hIcon, psfi->iIcon, psfi->dwAttributes,
590            debugstr_w(psfi->szDisplayName), debugstr_w(psfi->szTypeName), ret);
591 #endif
592
593     return ret;
594 }
595
596 /*************************************************************************
597  * SHGetFileInfoA            [SHELL32.@]
598  */
599 DWORD WINAPI SHGetFileInfoA(LPCSTR path,DWORD dwFileAttributes,
600                               SHFILEINFOA *psfi, UINT sizeofpsfi,
601                               UINT flags )
602 {
603     INT len;
604     LPWSTR temppath;
605     DWORD ret;
606     SHFILEINFOW temppsfi;
607
608     if (flags & SHGFI_PIDL)
609     {
610         /* path contains a pidl */
611         temppath = (LPWSTR) path;
612     }
613     else
614     {
615         len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
616         temppath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
617         MultiByteToWideChar(CP_ACP, 0, path, -1, temppath, len);
618     }
619
620     if (psfi && (flags & SHGFI_ATTR_SPECIFIED))
621         temppsfi.dwAttributes=psfi->dwAttributes;
622
623     if (psfi == NULL)
624         ret = SHGetFileInfoW(temppath, dwFileAttributes, NULL, sizeof(temppsfi), flags);
625     else
626         ret = SHGetFileInfoW(temppath, dwFileAttributes, &temppsfi, sizeof(temppsfi), flags);
627
628     if (psfi)
629     {
630         if(flags & SHGFI_ICON)
631             psfi->hIcon=temppsfi.hIcon;
632         if(flags & (SHGFI_SYSICONINDEX|SHGFI_ICON|SHGFI_ICONLOCATION))
633             psfi->iIcon=temppsfi.iIcon;
634         if(flags & SHGFI_ATTRIBUTES)
635             psfi->dwAttributes=temppsfi.dwAttributes;
636         if(flags & (SHGFI_DISPLAYNAME|SHGFI_ICONLOCATION))
637         {
638             WideCharToMultiByte(CP_ACP, 0, temppsfi.szDisplayName, -1,
639                   psfi->szDisplayName, sizeof(psfi->szDisplayName), NULL, NULL);
640         }
641         if(flags & SHGFI_TYPENAME)
642         {
643             WideCharToMultiByte(CP_ACP, 0, temppsfi.szTypeName, -1,
644                   psfi->szTypeName, sizeof(psfi->szTypeName), NULL, NULL);
645         }
646     }
647
648     if (!(flags & SHGFI_PIDL))
649         HeapFree(GetProcessHeap(), 0, temppath);
650
651     return ret;
652 }
653
654 /*************************************************************************
655  * DuplicateIcon            [SHELL32.@]
656  */
657 HICON WINAPI DuplicateIcon( HINSTANCE hInstance, HICON hIcon)
658 {
659     ICONINFO IconInfo;
660     HICON hDupIcon = 0;
661
662     TRACE("%p %p\n", hInstance, hIcon);
663
664     if (GetIconInfo(hIcon, &IconInfo))
665     {
666         hDupIcon = CreateIconIndirect(&IconInfo);
667
668         /* clean up hbmMask and hbmColor */
669         DeleteObject(IconInfo.hbmMask);
670         DeleteObject(IconInfo.hbmColor);
671     }
672
673     return hDupIcon;
674 }
675
676 /*************************************************************************
677  * ExtractIconA                [SHELL32.@]
678  */
679 HICON WINAPI ExtractIconA(HINSTANCE hInstance, LPCSTR lpszFile, UINT nIconIndex)
680 {   
681     HICON ret;
682     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
683     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
684
685     TRACE("%p %s %d\n", hInstance, lpszFile, nIconIndex);
686
687     MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
688     ret = ExtractIconW(hInstance, lpwstrFile, nIconIndex);
689     HeapFree(GetProcessHeap(), 0, lpwstrFile);
690
691     return ret;
692 }
693
694 /*************************************************************************
695  * ExtractIconW                [SHELL32.@]
696  */
697 HICON WINAPI ExtractIconW(HINSTANCE hInstance, LPCWSTR lpszFile, UINT nIconIndex)
698 {
699     HICON  hIcon = NULL;
700     UINT ret;
701     UINT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
702
703     TRACE("%p %s %d\n", hInstance, debugstr_w(lpszFile), nIconIndex);
704
705     if (nIconIndex == 0xFFFFFFFF)
706     {
707         ret = PrivateExtractIconsW(lpszFile, 0, cx, cy, NULL, NULL, 0, LR_DEFAULTCOLOR);
708         if (ret != 0xFFFFFFFF && ret)
709             return (HICON)ret;
710         return NULL;
711     }
712     else
713         ret = PrivateExtractIconsW(lpszFile, nIconIndex, cx, cy, &hIcon, NULL, 1, LR_DEFAULTCOLOR);
714
715     if (ret == 0xFFFFFFFF)
716         return (HICON)1;
717     else if (ret > 0 && hIcon)
718         return hIcon;
719
720     return NULL;
721 }
722
723 /*************************************************************************
724  * Printer_LoadIconsW        [SHELL32.205]
725  */
726 VOID WINAPI Printer_LoadIconsW(LPCWSTR wsPrinterName, HICON * pLargeIcon, HICON * pSmallIcon)
727 {
728     INT iconindex=IDI_SHELL_PRINTER;
729
730     TRACE("(%s, %p, %p)\n", debugstr_w(wsPrinterName), pLargeIcon, pSmallIcon);
731
732     /* We should check if wsPrinterName is
733        1. the Default Printer or not
734        2. connected or not
735        3. a Local Printer or a Network-Printer
736        and use different Icons
737     */
738     if((wsPrinterName != NULL) && (wsPrinterName[0] != 0))
739     {
740         FIXME("(select Icon by PrinterName %s not implemented)\n", debugstr_w(wsPrinterName));
741     }
742
743     if(pLargeIcon != NULL)
744         *pLargeIcon = LoadImageW(shell32_hInstance,
745                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
746                                  0, 0, LR_DEFAULTCOLOR|LR_DEFAULTSIZE);
747
748     if(pSmallIcon != NULL)
749         *pSmallIcon = LoadImageW(shell32_hInstance,
750                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
751                                  16, 16, LR_DEFAULTCOLOR);
752 }
753
754 /*************************************************************************
755  * Printers_RegisterWindowW        [SHELL32.213]
756  * used by "printui.dll":
757  * find the Window of the given Type for the specific Printer and 
758  * return the already existent hwnd or open a new window
759  */
760 BOOL WINAPI Printers_RegisterWindowW(LPCWSTR wsPrinter, DWORD dwType,
761             HANDLE * phClassPidl, HWND * phwnd)
762 {
763     FIXME("(%s, %lx, %p (%p), %p (%p)) stub!\n", debugstr_w(wsPrinter), dwType,
764                 phClassPidl, (phClassPidl != NULL) ? *(phClassPidl) : NULL,
765                 phwnd, (phwnd != NULL) ? *(phwnd) : NULL);
766
767     return FALSE;
768
769
770 /*************************************************************************
771  * Printers_UnregisterWindow      [SHELL32.214]
772  */
773 VOID WINAPI Printers_UnregisterWindow(HANDLE hClassPidl, HWND hwnd)
774 {
775     FIXME("(%p, %p) stub!\n", hClassPidl, hwnd);
776
777
778 /*************************************************************************/
779
780 typedef struct
781 {
782     LPCWSTR  szApp;
783     LPCWSTR  szOtherStuff;
784     HICON hIcon;
785     HFONT hFont;
786 } ABOUT_INFO;
787
788 #define IDC_STATIC_TEXT1   100
789 #define IDC_STATIC_TEXT2   101
790 #define IDC_LISTBOX        99
791 #define IDC_WINE_TEXT      98
792
793 #define DROP_FIELD_TOP    (-15)
794 #define DROP_FIELD_HEIGHT  15
795
796 static BOOL __get_dropline( HWND hWnd, LPRECT lprect )
797 {
798     HWND hWndCtl = GetDlgItem(hWnd, IDC_WINE_TEXT);
799
800     if( hWndCtl )
801     {
802         GetWindowRect( hWndCtl, lprect );
803         MapWindowPoints( 0, hWnd, (LPPOINT)lprect, 2 );
804         lprect->bottom = (lprect->top += DROP_FIELD_TOP);
805         return TRUE;
806     }
807     return FALSE;
808 }
809
810 /*************************************************************************
811  * SHAppBarMessage            [SHELL32.@]
812  */
813 UINT WINAPI SHAppBarMessage(DWORD msg, PAPPBARDATA data)
814 {
815     int width=data->rc.right - data->rc.left;
816     int height=data->rc.bottom - data->rc.top;
817     RECT rec=data->rc;
818
819     switch (msg)
820     {
821     case ABM_GETSTATE:
822         return ABS_ALWAYSONTOP | ABS_AUTOHIDE;
823     case ABM_GETTASKBARPOS:
824         GetWindowRect(data->hWnd, &rec);
825         data->rc=rec;
826         return TRUE;
827     case ABM_ACTIVATE:
828         SetActiveWindow(data->hWnd);
829         return TRUE;
830     case ABM_GETAUTOHIDEBAR:
831         data->hWnd=GetActiveWindow();
832         return TRUE;
833     case ABM_NEW:
834         SetWindowPos(data->hWnd,HWND_TOP,rec.left,rec.top,
835                           width,height,SWP_SHOWWINDOW);
836         return TRUE;
837     case ABM_QUERYPOS:
838         GetWindowRect(data->hWnd, &(data->rc));
839         return TRUE;
840     case ABM_REMOVE:
841         FIXME("ABM_REMOVE broken\n");
842         /* FIXME: this is wrong; should it be DestroyWindow instead? */
843         /*CloseHandle(data->hWnd);*/
844         return TRUE;
845     case ABM_SETAUTOHIDEBAR:
846         SetWindowPos(data->hWnd,HWND_TOP,rec.left+1000,rec.top,
847                          width,height,SWP_SHOWWINDOW);
848         return TRUE;
849     case ABM_SETPOS:
850         data->uEdge=(ABE_RIGHT | ABE_LEFT);
851         SetWindowPos(data->hWnd,HWND_TOP,data->rc.left,data->rc.top,
852                      width,height,SWP_SHOWWINDOW);
853         return TRUE;
854     case ABM_WINDOWPOSCHANGED:
855         return TRUE;
856     }
857     return FALSE;
858 }
859
860 /*************************************************************************
861  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
862  *
863  */
864 DWORD WINAPI SHHelpShortcuts_RunDLLA(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
865 {
866     FIXME("(%lx, %lx, %lx, %lx) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
867     return 0;
868 }
869
870 /*************************************************************************
871  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
872  *
873  */
874 DWORD WINAPI SHHelpShortcuts_RunDLLW(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
875 {
876     FIXME("(%lx, %lx, %lx, %lx) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
877     return 0;
878 }
879
880 /*************************************************************************
881  * SHLoadInProc                [SHELL32.@]
882  * Create an instance of specified object class from within
883  * the shell process and release it immediately
884  */
885 HRESULT WINAPI SHLoadInProc (REFCLSID rclsid)
886 {
887     void *ptr = NULL;
888
889     TRACE("%s\n", debugstr_guid(rclsid));
890
891     CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown,&ptr);
892     if(ptr)
893     {
894         IUnknown * pUnk = ptr;
895         IUnknown_Release(pUnk);
896         return NOERROR;
897     }
898     return DISP_E_MEMBERNOTFOUND;
899 }
900
901 /*************************************************************************
902  * AboutDlgProc            (internal)
903  */
904 INT_PTR CALLBACK AboutDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
905                               LPARAM lParam )
906 {
907     HWND hWndCtl;
908
909     TRACE("\n");
910
911     switch(msg)
912     {
913     case WM_INITDIALOG:
914         {
915             ABOUT_INFO *info = (ABOUT_INFO *)lParam;
916             WCHAR Template[512], AppTitle[512];
917
918             if (info)
919             {
920                 const char* const *pstr = SHELL_Authors;
921                 SendDlgItemMessageW(hWnd, stc1, STM_SETICON,(WPARAM)info->hIcon, 0);
922                 GetWindowTextW( hWnd, Template, sizeof(Template)/sizeof(WCHAR) );
923                 sprintfW( AppTitle, Template, info->szApp );
924                 SetWindowTextW( hWnd, AppTitle );
925                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT1), info->szApp );
926                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT2), info->szOtherStuff );
927                 hWndCtl = GetDlgItem(hWnd, IDC_LISTBOX);
928                 SendMessageW( hWndCtl, WM_SETREDRAW, 0, 0 );
929                 SendMessageW( hWndCtl, WM_SETFONT, (WPARAM)info->hFont, 0 );
930                 while (*pstr)
931                 {
932                     WCHAR name[64];
933                     /* authors list is in iso-8859-1 format */
934                     MultiByteToWideChar( 28591, 0, *pstr, -1, name, sizeof(name)/sizeof(WCHAR) );
935                     SendMessageW( hWndCtl, LB_ADDSTRING, (WPARAM)-1, (LPARAM)name );
936                     pstr++;
937                 }
938                 SendMessageW( hWndCtl, WM_SETREDRAW, 1, 0 );
939             }
940         }
941         return 1;
942
943     case WM_PAINT:
944         {
945             RECT rect;
946             PAINTSTRUCT ps;
947             HDC hDC = BeginPaint( hWnd, &ps );
948
949             if (__get_dropline( hWnd, &rect ))
950             {
951                 SelectObject( hDC, GetStockObject( BLACK_PEN ) );
952                 MoveToEx( hDC, rect.left, rect.top, NULL );
953                 LineTo( hDC, rect.right, rect.bottom );
954             }
955             EndPaint( hWnd, &ps );
956         }
957     break;
958
959     case WM_COMMAND:
960         if (wParam == IDOK || wParam == IDCANCEL)
961         {
962             EndDialog(hWnd, TRUE);
963             return TRUE;
964         }
965         break;
966     case WM_CLOSE:
967       EndDialog(hWnd, TRUE);
968       break;
969     }
970
971     return 0;
972 }
973
974
975 /*************************************************************************
976  * ShellAboutA                [SHELL32.288]
977  */
978 BOOL WINAPI ShellAboutA( HWND hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON hIcon )
979 {
980     BOOL ret;
981     LPWSTR appW = NULL, otherW = NULL;
982     int len;
983
984     if (szApp)
985     {
986         len = MultiByteToWideChar(CP_ACP, 0, szApp, -1, NULL, 0);
987         appW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
988         MultiByteToWideChar(CP_ACP, 0, szApp, -1, appW, len);
989     }
990     if (szOtherStuff)
991     {
992         len = MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, NULL, 0);
993         otherW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
994         MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, otherW, len);
995     }
996
997     ret = ShellAboutW(hWnd, appW, otherW, hIcon);
998
999     HeapFree(GetProcessHeap(), 0, otherW);
1000     HeapFree(GetProcessHeap(), 0, appW);
1001     return ret;
1002 }
1003
1004
1005 /*************************************************************************
1006  * ShellAboutW                [SHELL32.289]
1007  */
1008 BOOL WINAPI ShellAboutW( HWND hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
1009                              HICON hIcon )
1010 {
1011     ABOUT_INFO info;
1012     LOGFONTW logFont;
1013     HRSRC hRes;
1014     LPVOID template;
1015     BOOL bRet;
1016     static const WCHAR wszSHELL_ABOUT_MSGBOX[] =
1017         {'S','H','E','L','L','_','A','B','O','U','T','_','M','S','G','B','O','X',0};
1018
1019     TRACE("\n");
1020
1021     if(!(hRes = FindResourceW(shell32_hInstance, wszSHELL_ABOUT_MSGBOX, (LPWSTR)RT_DIALOG)))
1022         return FALSE;
1023     if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
1024         return FALSE;
1025     info.szApp        = szApp;
1026     info.szOtherStuff = szOtherStuff;
1027     info.hIcon        = hIcon ? hIcon : LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
1028
1029     SystemParametersInfoW( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
1030     info.hFont = CreateFontIndirectW( &logFont );
1031
1032     bRet = DialogBoxIndirectParamW((HINSTANCE)GetWindowLongPtrW( hWnd, GWLP_HINSTANCE ),
1033                                    template, hWnd, AboutDlgProc, (LPARAM)&info );
1034     DeleteObject(info.hFont);
1035     return bRet;
1036 }
1037
1038 /*************************************************************************
1039  * FreeIconList (SHELL32.@)
1040  */
1041 void WINAPI FreeIconList( DWORD dw )
1042 {
1043     FIXME("%lx: stub\n",dw);
1044 }
1045
1046
1047 /***********************************************************************
1048  * DllGetVersion [SHELL32.@]
1049  *
1050  * Retrieves version information of the 'SHELL32.DLL'
1051  *
1052  * PARAMS
1053  *     pdvi [O] pointer to version information structure.
1054  *
1055  * RETURNS
1056  *     Success: S_OK
1057  *     Failure: E_INVALIDARG
1058  *
1059  * NOTES
1060  *     Returns version of a shell32.dll from IE4.01 SP1.
1061  */
1062
1063 HRESULT WINAPI DllGetVersion (DLLVERSIONINFO *pdvi)
1064 {
1065     /* FIXME: shouldn't these values come from the version resource? */
1066     if (pdvi->cbSize == sizeof(DLLVERSIONINFO) ||
1067         pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1068     {
1069         pdvi->dwMajorVersion = WINE_FILEVERSION_MAJOR;
1070         pdvi->dwMinorVersion = WINE_FILEVERSION_MINOR;
1071         pdvi->dwBuildNumber = WINE_FILEVERSION_BUILD;
1072         pdvi->dwPlatformID = WINE_FILEVERSION_PLATFORMID;
1073         if (pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1074         {
1075             DLLVERSIONINFO2 *pdvi2 = (DLLVERSIONINFO2 *)pdvi;
1076
1077             pdvi2->dwFlags = 0;
1078             pdvi2->ullVersion = MAKEDLLVERULL(WINE_FILEVERSION_MAJOR,
1079                                               WINE_FILEVERSION_MINOR,
1080                                               WINE_FILEVERSION_BUILD,
1081                                               WINE_FILEVERSION_PLATFORMID);
1082         }
1083         TRACE("%lu.%lu.%lu.%lu\n",
1084               pdvi->dwMajorVersion, pdvi->dwMinorVersion,
1085               pdvi->dwBuildNumber, pdvi->dwPlatformID);
1086         return S_OK;
1087     }
1088     else
1089     {
1090         WARN("wrong DLLVERSIONINFO size from app\n");
1091         return E_INVALIDARG;
1092     }
1093 }
1094
1095 /*************************************************************************
1096  * global variables of the shell32.dll
1097  * all are once per process
1098  *
1099  */
1100 HINSTANCE    shell32_hInstance = 0;
1101 HIMAGELIST   ShellSmallIconList = 0;
1102 HIMAGELIST   ShellBigIconList = 0;
1103
1104
1105 /*************************************************************************
1106  * SHELL32 DllMain
1107  *
1108  * NOTES
1109  *  calling oleinitialize here breaks sone apps.
1110  */
1111 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
1112 {
1113     TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
1114
1115     switch (fdwReason)
1116     {
1117     case DLL_PROCESS_ATTACH:
1118         shell32_hInstance = hinstDLL;
1119         DisableThreadLibraryCalls(shell32_hInstance);
1120
1121         /* get full path to this DLL for IExtractIconW_fnGetIconLocation() */
1122         GetModuleFileNameW(hinstDLL, swShell32Name, MAX_PATH);
1123         swShell32Name[MAX_PATH - 1] = '\0';
1124
1125         InitCommonControlsEx(NULL);
1126
1127         SIC_Initialize();
1128         SYSTRAY_Init();
1129         InitChangeNotifications();
1130         break;
1131
1132     case DLL_PROCESS_DETACH:
1133         shell32_hInstance = 0;
1134         SIC_Destroy();
1135         FreeChangeNotifications();
1136         break;
1137     }
1138     return TRUE;
1139 }
1140
1141 /*************************************************************************
1142  * DllInstall         [SHELL32.@]
1143  *
1144  * PARAMETERS
1145  *
1146  *    BOOL bInstall - TRUE for install, FALSE for uninstall
1147  *    LPCWSTR pszCmdLine - command line (unused by shell32?)
1148  */
1149
1150 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
1151 {
1152     FIXME("%s %s: stub\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
1153     return S_OK;        /* indicate success */
1154 }
1155
1156 /***********************************************************************
1157  *              DllCanUnloadNow (SHELL32.@)
1158  */
1159 HRESULT WINAPI DllCanUnloadNow(void)
1160 {
1161     FIXME("stub\n");
1162     return S_FALSE;
1163 }