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