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