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