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