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