Authors: Michael Lin <mlin@corvu.com.au>, Michael Jung <mjung@iss.tu-darmstadt.de>
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 writeable 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 #define SHGFI_KNOWN_FLAGS \
301     (SHGFI_SMALLICON | SHGFI_OPENICON | SHGFI_SHELLICONSIZE | SHGFI_PIDL | \
302      SHGFI_USEFILEATTRIBUTES | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX | \
303      SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ATTRIBUTES | \
304      SHGFI_ICONLOCATION | SHGFI_EXETYPE | SHGFI_SYSICONINDEX | \
305      SHGFI_LINKOVERLAY | SHGFI_SELECTED | SHGFI_ATTR_SPECIFIED)
306
307 /*************************************************************************
308  * SHGetFileInfoW            [SHELL32.@]
309  *
310  */
311 DWORD WINAPI SHGetFileInfoW(LPCWSTR path,DWORD dwFileAttributes,
312                             SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags )
313 {
314     WCHAR szLocation[MAX_PATH], szFullPath[MAX_PATH];
315     int iIndex;
316     DWORD ret = TRUE, dwAttributes = 0;
317     IShellFolder * psfParent = NULL;
318     IExtractIconW * pei = NULL;
319     LPITEMIDLIST    pidlLast = NULL, pidl = NULL;
320     HRESULT hr = S_OK;
321     BOOL IconNotYetLoaded=TRUE;
322
323     TRACE("%s fattr=0x%lx sfi=%p(attr=0x%08lx) size=0x%x flags=0x%x\n",
324           (flags & SHGFI_PIDL)? "pidl" : debugstr_w(path), dwFileAttributes,
325           psfi, psfi->dwAttributes, sizeofpsfi, flags);
326
327     if ( (flags & SHGFI_USEFILEATTRIBUTES) && 
328          (flags & (SHGFI_ATTRIBUTES|SHGFI_EXETYPE|SHGFI_PIDL)))
329         return FALSE;
330
331     /* windows initializes this values regardless of the flags */
332     if (psfi != NULL)
333     {
334         psfi->szDisplayName[0] = '\0';
335         psfi->szTypeName[0] = '\0';
336         psfi->iIcon = 0;
337     }
338
339     if (!(flags & SHGFI_PIDL))
340     {
341         /* SHGetFileInfo should work with absolute and relative paths */
342         if (PathIsRelativeW(path))
343         {
344             GetCurrentDirectoryW(MAX_PATH, szLocation);
345             PathCombineW(szFullPath, szLocation, path);
346         }
347         else
348         {
349             lstrcpynW(szFullPath, path, MAX_PATH);
350         }
351     }
352
353     if (flags & SHGFI_EXETYPE)
354     {
355         if (flags != SHGFI_EXETYPE)
356             return 0;
357         return shgfi_get_exe_type(szFullPath);
358     }
359
360     /*
361      * psfi is NULL normally to query EXE type. If it is NULL, none of the
362      * below makes sense anyway. Windows allows this and just returns FALSE
363      */
364     if (psfi == NULL)
365         return FALSE;
366
367     /*
368      * translate the path into a pidl only when SHGFI_USEFILEATTRIBUTES
369      * is not specified.
370      * The pidl functions fail on not existing file names
371      */
372
373     if (flags & SHGFI_PIDL)
374     {
375         pidl = ILClone((LPCITEMIDLIST)path);
376     }
377     else if (!(flags & SHGFI_USEFILEATTRIBUTES))
378     {
379         hr = SHILCreateFromPathW(szFullPath, &pidl, &dwAttributes);
380     }
381
382     if ((flags & SHGFI_PIDL) || !(flags & SHGFI_USEFILEATTRIBUTES))
383     {
384         /* get the parent shellfolder */
385         if (pidl)
386         {
387             hr = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&psfParent,
388                                 (LPCITEMIDLIST*)&pidlLast );
389             ILFree(pidl);
390         }
391         else
392         {
393             ERR("pidl is null!\n");
394             return FALSE;
395         }
396     }
397
398     /* get the attributes of the child */
399     if (SUCCEEDED(hr) && (flags & SHGFI_ATTRIBUTES))
400     {
401         if (!(flags & SHGFI_ATTR_SPECIFIED))
402         {
403             psfi->dwAttributes = 0xffffffff;
404         }
405         IShellFolder_GetAttributesOf( psfParent, 1, (LPCITEMIDLIST*)&pidlLast,
406                                       &(psfi->dwAttributes) );
407     }
408
409     /* get the displayname */
410     if (SUCCEEDED(hr) && (flags & SHGFI_DISPLAYNAME))
411     {
412         if (flags & SHGFI_USEFILEATTRIBUTES)
413         {
414             lstrcpyW (psfi->szDisplayName, PathFindFileNameW(szFullPath));
415         }
416         else
417         {
418             STRRET str;
419             hr = IShellFolder_GetDisplayNameOf( psfParent, pidlLast,
420                                                 SHGDN_INFOLDER, &str);
421             StrRetToStrNW (psfi->szDisplayName, MAX_PATH, &str, pidlLast);
422         }
423     }
424
425     /* get the type name */
426     if (SUCCEEDED(hr) && (flags & SHGFI_TYPENAME))
427     {
428         static const WCHAR szFile[] = { 'F','i','l','e',0 };
429         static const WCHAR szDashFile[] = { '-','f','i','l','e',0 };
430
431         if (!(flags & SHGFI_USEFILEATTRIBUTES))
432         {
433             char ftype[80];
434
435             _ILGetFileType(pidlLast, ftype, 80);
436             MultiByteToWideChar(CP_ACP, 0, ftype, -1, psfi->szTypeName, 80 );
437         }
438         else
439         {
440             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
441                 strcatW (psfi->szTypeName, szFile);
442             else 
443             {
444                 WCHAR sTemp[64];
445
446                 lstrcpyW(sTemp,PathFindExtensionW(szFullPath));
447                 if (!( HCR_MapTypeToValueW(sTemp, sTemp, 64, TRUE) &&
448                     HCR_MapTypeToValueW(sTemp, psfi->szTypeName, 80, FALSE )))
449                 {
450                     lstrcpynW (psfi->szTypeName, sTemp, 64);
451                     strcatW (psfi->szTypeName, szDashFile);
452                 }
453             }
454         }
455     }
456
457     /* ### icons ###*/
458     if (flags & SHGFI_ADDOVERLAYS)
459         FIXME("SHGFI_ADDOVERLAYS unhandled\n");
460
461     if (flags & SHGFI_OVERLAYINDEX)
462         FIXME("SHGFI_OVERLAYINDEX unhandled\n");
463
464     if (flags & SHGFI_LINKOVERLAY)
465         FIXME("set icon to link, stub\n");
466
467     if (flags & SHGFI_SELECTED)
468         FIXME("set icon to selected, stub\n");
469
470     if (flags & SHGFI_SHELLICONSIZE)
471         FIXME("set icon to shell size, stub\n");
472
473     /* get the iconlocation */
474     if (SUCCEEDED(hr) && (flags & SHGFI_ICONLOCATION ))
475     {
476         UINT uDummy,uFlags;
477
478         hr = IShellFolder_GetUIObjectOf(psfParent, 0, 1,
479                (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA,
480                &uDummy, (LPVOID*)&pei);
481         if (SUCCEEDED(hr))
482         {
483             hr = IExtractIconW_GetIconLocation(pei, 
484                     (flags & SHGFI_OPENICON)? GIL_OPENICON : 0,
485                     szLocation, MAX_PATH, &iIndex, &uFlags);
486             psfi->iIcon = iIndex;
487
488             if (uFlags != GIL_NOTFILENAME)
489                 lstrcpyW (psfi->szDisplayName, szLocation);
490             else
491                 ret = FALSE;
492
493             IExtractIconA_Release(pei);
494         }
495     }
496
497     /* get icon index (or load icon)*/
498     if (SUCCEEDED(hr) && (flags & (SHGFI_ICON | SHGFI_SYSICONINDEX)))
499     {
500         if (flags & SHGFI_USEFILEATTRIBUTES)
501         {
502             WCHAR sTemp [MAX_PATH];
503             WCHAR * szExt;
504             DWORD dwNr=0;
505
506             lstrcpynW(sTemp, szFullPath, MAX_PATH);
507
508             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
509                 psfi->iIcon = SIC_GetIconIndex(swShell32Name, -IDI_SHELL_FOLDER, 0);
510             else
511             {
512                 static const WCHAR p1W[] = {'%','1',0};
513
514                 psfi->iIcon = 0;
515                 szExt = (LPWSTR) PathFindExtensionW(sTemp);
516                 if ( szExt &&
517                      HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE) &&
518                      HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &dwNr))
519                 {
520                     if (!lstrcmpW(p1W,sTemp))            /* icon is in the file */
521                         strcpyW(sTemp, szFullPath);
522
523                     if (flags & SHGFI_SYSICONINDEX) 
524                     {
525                         psfi->iIcon = SIC_GetIconIndex(sTemp,dwNr,0);
526                         if (psfi->iIcon == -1)
527                             psfi->iIcon = 0;
528                     }
529                     else 
530                     {
531                         IconNotYetLoaded=FALSE;
532                         if (flags & SHGFI_SMALLICON)
533                             PrivateExtractIconsW( sTemp,dwNr,
534                                 GetSystemMetrics( SM_CXSMICON ),
535                                 GetSystemMetrics( SM_CYSMICON ),
536                                 &psfi->hIcon, 0, 1, 0);
537                         else
538                             PrivateExtractIconsW( sTemp, dwNr,
539                                 GetSystemMetrics( SM_CXICON),
540                                 GetSystemMetrics( SM_CYICON),
541                                 &psfi->hIcon, 0, 1, 0);
542                         psfi->iIcon = dwNr;
543                     }
544                 }
545             }
546         }
547         else
548         {
549             if (!(PidlToSicIndex(psfParent, pidlLast, !(flags & SHGFI_SMALLICON),
550                 (flags & SHGFI_OPENICON)? GIL_OPENICON : 0, &(psfi->iIcon))))
551             {
552                 ret = FALSE;
553             }
554         }
555         if (ret)
556         {
557             if (flags & SHGFI_SMALLICON)
558                 ret = (DWORD) ShellSmallIconList;
559             else
560                 ret = (DWORD) ShellBigIconList;
561         }
562     }
563
564     /* icon handle */
565     if (SUCCEEDED(hr) && (flags & SHGFI_ICON) && IconNotYetLoaded)
566     {
567         if (flags & SHGFI_SMALLICON)
568             psfi->hIcon = ImageList_GetIcon( ShellSmallIconList, psfi->iIcon, ILD_NORMAL);
569         else
570             psfi->hIcon = ImageList_GetIcon( ShellBigIconList, psfi->iIcon, ILD_NORMAL);
571     }
572
573     if (flags & ~SHGFI_KNOWN_FLAGS)
574         FIXME("unknown flags %08x\n", flags & ~SHGFI_KNOWN_FLAGS);
575
576     if (psfParent)
577         IShellFolder_Release(psfParent);
578
579     if (hr != S_OK)
580         ret = FALSE;
581
582     if (pidlLast)
583         SHFree(pidlLast);
584
585 #ifdef MORE_DEBUG
586     TRACE ("icon=%p index=0x%08x attr=0x%08lx name=%s type=%s ret=0x%08lx\n",
587            psfi->hIcon, psfi->iIcon, psfi->dwAttributes,
588            debugstr_w(psfi->szDisplayName), debugstr_w(psfi->szTypeName), ret);
589 #endif
590
591     return ret;
592 }
593
594 /*************************************************************************
595  * SHGetFileInfoA            [SHELL32.@]
596  */
597 DWORD WINAPI SHGetFileInfoA(LPCSTR path,DWORD dwFileAttributes,
598                               SHFILEINFOA *psfi, UINT sizeofpsfi,
599                               UINT flags )
600 {
601     INT len;
602     LPWSTR temppath;
603     DWORD ret;
604     SHFILEINFOW temppsfi;
605
606     if (flags & SHGFI_PIDL)
607     {
608         /* path contains a pidl */
609         temppath = (LPWSTR) path;
610     }
611     else
612     {
613         len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
614         temppath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
615         MultiByteToWideChar(CP_ACP, 0, path, -1, temppath, len);
616     }
617
618     if (psfi && (flags & SHGFI_ATTR_SPECIFIED))
619         temppsfi.dwAttributes=psfi->dwAttributes;
620
621     if (psfi == NULL)
622         ret = SHGetFileInfoW(temppath, dwFileAttributes, NULL, sizeof(temppsfi), flags);
623     else
624         ret = SHGetFileInfoW(temppath, dwFileAttributes, &temppsfi, sizeof(temppsfi), flags);
625
626     if (psfi)
627     {
628         if(flags & SHGFI_ICON)
629             psfi->hIcon=temppsfi.hIcon;
630         if(flags & (SHGFI_SYSICONINDEX|SHGFI_ICON|SHGFI_ICONLOCATION))
631             psfi->iIcon=temppsfi.iIcon;
632         if(flags & SHGFI_ATTRIBUTES)
633             psfi->dwAttributes=temppsfi.dwAttributes;
634         if(flags & (SHGFI_DISPLAYNAME|SHGFI_ICONLOCATION))
635         {
636             WideCharToMultiByte(CP_ACP, 0, temppsfi.szDisplayName, -1,
637                   psfi->szDisplayName, sizeof(psfi->szDisplayName), NULL, NULL);
638         }
639         if(flags & SHGFI_TYPENAME)
640         {
641             WideCharToMultiByte(CP_ACP, 0, temppsfi.szTypeName, -1,
642                   psfi->szTypeName, sizeof(psfi->szTypeName), NULL, NULL);
643         }
644     }
645
646     if (!(flags & SHGFI_PIDL))
647         HeapFree(GetProcessHeap(), 0, temppath);
648
649     return ret;
650 }
651
652 /*************************************************************************
653  * DuplicateIcon            [SHELL32.@]
654  */
655 HICON WINAPI DuplicateIcon( HINSTANCE hInstance, HICON hIcon)
656 {
657     ICONINFO IconInfo;
658     HICON hDupIcon = 0;
659
660     TRACE("%p %p\n", hInstance, hIcon);
661
662     if (GetIconInfo(hIcon, &IconInfo))
663     {
664         hDupIcon = CreateIconIndirect(&IconInfo);
665
666         /* clean up hbmMask and hbmColor */
667         DeleteObject(IconInfo.hbmMask);
668         DeleteObject(IconInfo.hbmColor);
669     }
670
671     return hDupIcon;
672 }
673
674 /*************************************************************************
675  * ExtractIconA                [SHELL32.@]
676  */
677 HICON WINAPI ExtractIconA(HINSTANCE hInstance, LPCSTR lpszFile, UINT nIconIndex)
678 {   
679     HICON ret;
680     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
681     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
682
683     TRACE("%p %s %d\n", hInstance, lpszFile, nIconIndex);
684
685     MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
686     ret = ExtractIconW(hInstance, lpwstrFile, nIconIndex);
687     HeapFree(GetProcessHeap(), 0, lpwstrFile);
688
689     return ret;
690 }
691
692 /*************************************************************************
693  * ExtractIconW                [SHELL32.@]
694  */
695 HICON WINAPI ExtractIconW(HINSTANCE hInstance, LPCWSTR lpszFile, UINT nIconIndex)
696 {
697     HICON  hIcon = NULL;
698     UINT ret;
699     UINT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
700
701     TRACE("%p %s %d\n", hInstance, debugstr_w(lpszFile), nIconIndex);
702
703     if (nIconIndex == 0xFFFFFFFF)
704     {
705         ret = PrivateExtractIconsW(lpszFile, 0, cx, cy, NULL, NULL, 0, LR_DEFAULTCOLOR);
706         if (ret != 0xFFFFFFFF && ret)
707             return (HICON)ret;
708         return NULL;
709     }
710     else
711         ret = PrivateExtractIconsW(lpszFile, nIconIndex, cx, cy, &hIcon, NULL, 1, LR_DEFAULTCOLOR);
712
713     if (ret == 0xFFFFFFFF)
714         return (HICON)1;
715     else if (ret > 0 && hIcon)
716         return hIcon;
717
718     return NULL;
719 }
720
721 /*************************************************************************
722  * Printer_LoadIconsW        [SHELL32.205]
723  */
724 VOID WINAPI Printer_LoadIconsW(LPCWSTR wsPrinterName, HICON * pLargeIcon, HICON * pSmallIcon)
725 {
726     INT iconindex=IDI_SHELL_PRINTER;
727
728     TRACE("(%s, %p, %p)\n", debugstr_w(wsPrinterName), pLargeIcon, pSmallIcon);
729
730     /* We should check if wsPrinterName is
731        1. the Default Printer or not
732        2. connected or not
733        3. a Local Printer or a Network-Printer
734        and use different Icons
735     */
736
737     FIXME("(select Icon by PrinterName %s not implemented)\n", debugstr_w(wsPrinterName));
738
739     if(pLargeIcon != NULL)
740         *pLargeIcon = LoadImageW(shell32_hInstance,
741                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
742                                  0, 0, LR_DEFAULTCOLOR|LR_DEFAULTSIZE);
743
744     if(pSmallIcon != NULL)
745         *pSmallIcon = LoadImageW(shell32_hInstance,
746                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
747                                  16, 16, LR_DEFAULTCOLOR);
748 }
749
750 /*************************************************************************/
751
752 typedef struct
753 {
754     LPCWSTR  szApp;
755     LPCWSTR  szOtherStuff;
756     HICON hIcon;
757     HFONT hFont;
758 } ABOUT_INFO;
759
760 #define IDC_STATIC_TEXT1   100
761 #define IDC_STATIC_TEXT2   101
762 #define IDC_LISTBOX        99
763 #define IDC_WINE_TEXT      98
764
765 #define DROP_FIELD_TOP    (-15)
766 #define DROP_FIELD_HEIGHT  15
767
768 static BOOL __get_dropline( HWND hWnd, LPRECT lprect )
769 {
770     HWND hWndCtl = GetDlgItem(hWnd, IDC_WINE_TEXT);
771
772     if( hWndCtl )
773     {
774         GetWindowRect( hWndCtl, lprect );
775         MapWindowPoints( 0, hWnd, (LPPOINT)lprect, 2 );
776         lprect->bottom = (lprect->top += DROP_FIELD_TOP);
777         return TRUE;
778     }
779     return FALSE;
780 }
781
782 /*************************************************************************
783  * SHAppBarMessage            [SHELL32.@]
784  */
785 UINT WINAPI SHAppBarMessage(DWORD msg, PAPPBARDATA data)
786 {
787     int width=data->rc.right - data->rc.left;
788     int height=data->rc.bottom - data->rc.top;
789     RECT rec=data->rc;
790
791     switch (msg)
792     {
793     case ABM_GETSTATE:
794         return ABS_ALWAYSONTOP | ABS_AUTOHIDE;
795     case ABM_GETTASKBARPOS:
796         GetWindowRect(data->hWnd, &rec);
797         data->rc=rec;
798         return TRUE;
799     case ABM_ACTIVATE:
800         SetActiveWindow(data->hWnd);
801         return TRUE;
802     case ABM_GETAUTOHIDEBAR:
803         data->hWnd=GetActiveWindow();
804         return TRUE;
805     case ABM_NEW:
806         SetWindowPos(data->hWnd,HWND_TOP,rec.left,rec.top,
807                           width,height,SWP_SHOWWINDOW);
808         return TRUE;
809     case ABM_QUERYPOS:
810         GetWindowRect(data->hWnd, &(data->rc));
811         return TRUE;
812     case ABM_REMOVE:
813         FIXME("ABM_REMOVE broken\n");
814         /* FIXME: this is wrong; should it be DestroyWindow instead? */
815         /*CloseHandle(data->hWnd);*/
816         return TRUE;
817     case ABM_SETAUTOHIDEBAR:
818         SetWindowPos(data->hWnd,HWND_TOP,rec.left+1000,rec.top,
819                          width,height,SWP_SHOWWINDOW);
820         return TRUE;
821     case ABM_SETPOS:
822         data->uEdge=(ABE_RIGHT | ABE_LEFT);
823         SetWindowPos(data->hWnd,HWND_TOP,data->rc.left,data->rc.top,
824                      width,height,SWP_SHOWWINDOW);
825         return TRUE;
826     case ABM_WINDOWPOSCHANGED:
827         return TRUE;
828     }
829     return FALSE;
830 }
831
832 /*************************************************************************
833  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
834  *
835  */
836 DWORD WINAPI SHHelpShortcuts_RunDLLA(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
837 {
838     FIXME("(%lx, %lx, %lx, %lx) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
839     return 0;
840 }
841
842 /*************************************************************************
843  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
844  *
845  */
846 DWORD WINAPI SHHelpShortcuts_RunDLLW(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
847 {
848     FIXME("(%lx, %lx, %lx, %lx) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
849     return 0;
850 }
851
852 /*************************************************************************
853  * SHLoadInProc                [SHELL32.@]
854  * Create an instance of specified object class from within
855  * the shell process and release it immediately
856  */
857 HRESULT WINAPI SHLoadInProc (REFCLSID rclsid)
858 {
859     void *ptr = NULL;
860
861     TRACE("%s\n", debugstr_guid(rclsid));
862
863     CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown,&ptr);
864     if(ptr)
865     {
866         IUnknown * pUnk = ptr;
867         IUnknown_Release(pUnk);
868         return NOERROR;
869     }
870     return DISP_E_MEMBERNOTFOUND;
871 }
872
873 /*************************************************************************
874  * AboutDlgProc            (internal)
875  */
876 INT_PTR CALLBACK AboutDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
877                               LPARAM lParam )
878 {
879     HWND hWndCtl;
880
881     TRACE("\n");
882
883     switch(msg)
884     {
885     case WM_INITDIALOG:
886         {
887             ABOUT_INFO *info = (ABOUT_INFO *)lParam;
888             WCHAR Template[512], AppTitle[512];
889
890             if (info)
891             {
892                 const char* const *pstr = SHELL_Authors;
893                 SendDlgItemMessageW(hWnd, stc1, STM_SETICON,(WPARAM)info->hIcon, 0);
894                 GetWindowTextW( hWnd, Template, sizeof(Template)/sizeof(WCHAR) );
895                 sprintfW( AppTitle, Template, info->szApp );
896                 SetWindowTextW( hWnd, AppTitle );
897                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT1), info->szApp );
898                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT2), info->szOtherStuff );
899                 hWndCtl = GetDlgItem(hWnd, IDC_LISTBOX);
900                 SendMessageW( hWndCtl, WM_SETREDRAW, 0, 0 );
901                 SendMessageW( hWndCtl, WM_SETFONT, (WPARAM)info->hFont, 0 );
902                 while (*pstr)
903                 {
904                     WCHAR name[64];
905                     /* authors list is in iso-8859-1 format */
906                     MultiByteToWideChar( 28591, 0, *pstr, -1, name, sizeof(name)/sizeof(WCHAR) );
907                     SendMessageW( hWndCtl, LB_ADDSTRING, (WPARAM)-1, (LPARAM)name );
908                     pstr++;
909                 }
910                 SendMessageW( hWndCtl, WM_SETREDRAW, 1, 0 );
911             }
912         }
913         return 1;
914
915     case WM_PAINT:
916         {
917             RECT rect;
918             PAINTSTRUCT ps;
919             HDC hDC = BeginPaint( hWnd, &ps );
920
921             if (__get_dropline( hWnd, &rect ))
922             {
923                 SelectObject( hDC, GetStockObject( BLACK_PEN ) );
924                 MoveToEx( hDC, rect.left, rect.top, NULL );
925                 LineTo( hDC, rect.right, rect.bottom );
926             }
927             EndPaint( hWnd, &ps );
928         }
929     break;
930
931     case WM_COMMAND:
932         if (wParam == IDOK || wParam == IDCANCEL)
933         {
934             EndDialog(hWnd, TRUE);
935             return TRUE;
936         }
937         break;
938     case WM_CLOSE:
939       EndDialog(hWnd, TRUE);
940       break;
941     }
942
943     return 0;
944 }
945
946
947 /*************************************************************************
948  * ShellAboutA                [SHELL32.288]
949  */
950 BOOL WINAPI ShellAboutA( HWND hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON hIcon )
951 {
952     BOOL ret;
953     LPWSTR appW = NULL, otherW = NULL;
954     int len;
955
956     if (szApp)
957     {
958         len = MultiByteToWideChar(CP_ACP, 0, szApp, -1, NULL, 0);
959         appW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
960         MultiByteToWideChar(CP_ACP, 0, szApp, -1, appW, len);
961     }
962     if (szOtherStuff)
963     {
964         len = MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, NULL, 0);
965         otherW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
966         MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, otherW, len);
967     }
968
969     ret = ShellAboutW(hWnd, appW, otherW, hIcon);
970
971     HeapFree(GetProcessHeap(), 0, otherW);
972     HeapFree(GetProcessHeap(), 0, appW);
973     return ret;
974 }
975
976
977 /*************************************************************************
978  * ShellAboutW                [SHELL32.289]
979  */
980 BOOL WINAPI ShellAboutW( HWND hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
981                              HICON hIcon )
982 {
983     ABOUT_INFO info;
984     LOGFONTW logFont;
985     HRSRC hRes;
986     LPVOID template;
987     BOOL bRet;
988     static const WCHAR wszSHELL_ABOUT_MSGBOX[] =
989         {'S','H','E','L','L','_','A','B','O','U','T','_','M','S','G','B','O','X',0};
990
991     TRACE("\n");
992
993     if(!(hRes = FindResourceW(shell32_hInstance, wszSHELL_ABOUT_MSGBOX, (LPWSTR)RT_DIALOG)))
994         return FALSE;
995     if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
996         return FALSE;
997     info.szApp        = szApp;
998     info.szOtherStuff = szOtherStuff;
999     info.hIcon        = hIcon ? hIcon : LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
1000
1001     SystemParametersInfoW( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
1002     info.hFont = CreateFontIndirectW( &logFont );
1003
1004     bRet = DialogBoxIndirectParamW((HINSTANCE)GetWindowLongPtrW( hWnd, GWLP_HINSTANCE ),
1005                                    template, hWnd, AboutDlgProc, (LPARAM)&info );
1006     DeleteObject(info.hFont);
1007     return bRet;
1008 }
1009
1010 /*************************************************************************
1011  * FreeIconList (SHELL32.@)
1012  */
1013 void WINAPI FreeIconList( DWORD dw )
1014 {
1015     FIXME("%lx: stub\n",dw);
1016 }
1017
1018
1019 /***********************************************************************
1020  * DllGetVersion [SHELL32.@]
1021  *
1022  * Retrieves version information of the 'SHELL32.DLL'
1023  *
1024  * PARAMS
1025  *     pdvi [O] pointer to version information structure.
1026  *
1027  * RETURNS
1028  *     Success: S_OK
1029  *     Failure: E_INVALIDARG
1030  *
1031  * NOTES
1032  *     Returns version of a shell32.dll from IE4.01 SP1.
1033  */
1034
1035 HRESULT WINAPI SHELL32_DllGetVersion (DLLVERSIONINFO *pdvi)
1036 {
1037     /* FIXME: shouldn't these values come from the version resource? */
1038     if (pdvi->cbSize == sizeof(DLLVERSIONINFO) ||
1039         pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1040     {
1041         pdvi->dwMajorVersion = WINE_FILEVERSION_MAJOR;
1042         pdvi->dwMinorVersion = WINE_FILEVERSION_MINOR;
1043         pdvi->dwBuildNumber = WINE_FILEVERSION_BUILD;
1044         pdvi->dwPlatformID = WINE_FILEVERSION_PLATFORMID;
1045         if (pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1046         {
1047             DLLVERSIONINFO2 *pdvi2 = (DLLVERSIONINFO2 *)pdvi;
1048
1049             pdvi2->dwFlags = 0;
1050             pdvi2->ullVersion = MAKEDLLVERULL(WINE_FILEVERSION_MAJOR,
1051                                               WINE_FILEVERSION_MINOR,
1052                                               WINE_FILEVERSION_BUILD,
1053                                               WINE_FILEVERSION_PLATFORMID);
1054         }
1055         TRACE("%lu.%lu.%lu.%lu\n",
1056               pdvi->dwMajorVersion, pdvi->dwMinorVersion,
1057               pdvi->dwBuildNumber, pdvi->dwPlatformID);
1058         return S_OK;
1059     }
1060     else
1061     {
1062         WARN("wrong DLLVERSIONINFO size from app\n");
1063         return E_INVALIDARG;
1064     }
1065 }
1066
1067 /*************************************************************************
1068  * global variables of the shell32.dll
1069  * all are once per process
1070  *
1071  */
1072 HINSTANCE    shell32_hInstance = 0;
1073 HIMAGELIST   ShellSmallIconList = 0;
1074 HIMAGELIST   ShellBigIconList = 0;
1075
1076
1077 /*************************************************************************
1078  * SHELL32 DllMain
1079  *
1080  * NOTES
1081  *  calling oleinitialize here breaks sone apps.
1082  */
1083 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
1084 {
1085     TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
1086
1087     switch (fdwReason)
1088     {
1089     case DLL_PROCESS_ATTACH:
1090         shell32_hInstance = hinstDLL;
1091         DisableThreadLibraryCalls(shell32_hInstance);
1092
1093         /* get full path to this DLL for IExtractIconW_fnGetIconLocation() */
1094         GetModuleFileNameW(hinstDLL, swShell32Name, MAX_PATH);
1095         swShell32Name[MAX_PATH - 1] = '\0';
1096
1097         InitCommonControlsEx(NULL);
1098
1099         SIC_Initialize();
1100         SYSTRAY_Init();
1101         InitChangeNotifications();
1102         break;
1103
1104     case DLL_PROCESS_DETACH:
1105         shell32_hInstance = 0;
1106         SIC_Destroy();
1107         FreeChangeNotifications();
1108         break;
1109     }
1110     return TRUE;
1111 }
1112
1113 /*************************************************************************
1114  * DllInstall         [SHELL32.@]
1115  *
1116  * PARAMETERS
1117  *
1118  *    BOOL bInstall - TRUE for install, FALSE for uninstall
1119  *    LPCWSTR pszCmdLine - command line (unused by shell32?)
1120  */
1121
1122 HRESULT WINAPI SHELL32_DllInstall(BOOL bInstall, LPCWSTR cmdline)
1123 {
1124     FIXME("%s %s: stub\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
1125     return S_OK;        /* indicate success */
1126 }
1127
1128 /***********************************************************************
1129  *              DllCanUnloadNow (SHELL32.@)
1130  */
1131 HRESULT WINAPI SHELL32_DllCanUnloadNow(void)
1132 {
1133     FIXME("stub\n");
1134     return S_FALSE;
1135 }