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