Stub implementation for MsiGetFileHashA/W.
[wine] / dlls / shell32 / shell.c
1 /*
2  *                              Shell Library Functions
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 2000 Juergen Schmied
6  * Copyright 2002 Eric Pouech
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winreg.h"
38 #include "wownt32.h"
39 #include "dlgs.h"
40 #include "shellapi.h"
41 #include "winuser.h"
42 #include "wingdi.h"
43 #include "shlobj.h"
44 #include "shlwapi.h"
45 #include "ddeml.h"
46
47 #include "wine/winbase16.h"
48 #include "wine/winuser16.h"
49 #include "shell32_main.h"
50
51 #include "wine/debug.h"
52
53 WINE_DEFAULT_DEBUG_CHANNEL(shell);
54
55
56 typedef struct {     /* structure for dropped files */
57  WORD     wSize;
58  POINT16  ptMousePos;
59  BOOL16   fInNonClientArea;
60  /* memory block with filenames follows */
61 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
62
63 static const char*      lpstrMsgWndCreated = "OTHERWINDOWCREATED";
64 static const char*      lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
65 static const char*      lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
66
67 static HWND     SHELL_hWnd = 0;
68 static HHOOK    SHELL_hHook = 0;
69 static UINT     uMsgWndCreated = 0;
70 static UINT     uMsgWndDestroyed = 0;
71 static UINT     uMsgShellActivate = 0;
72
73 /***********************************************************************
74  * DllEntryPoint [SHELL.101]
75  *
76  * Initialization code for shell.dll. Automatically loads the
77  * 32-bit shell32.dll to allow thunking up to 32-bit code.
78  *
79  * RETURNS
80  *  Success: TRUE. Initialization completed successfully.
81  *  Failure: FALSE.
82  */
83 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
84                                 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
85 {
86     return TRUE;
87 }
88
89 /*************************************************************************
90  *                              DragAcceptFiles         [SHELL.9]
91  */
92 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
93 {
94   DragAcceptFiles(HWND_32(hWnd), b);
95 }
96
97 /*************************************************************************
98  *                              DragQueryFile           [SHELL.11]
99  */
100 UINT16 WINAPI DragQueryFile16(
101         HDROP16 hDrop,
102         WORD wFile,
103         LPSTR lpszFile,
104         WORD wLength)
105 {
106         LPSTR lpDrop;
107         UINT i = 0;
108         LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
109
110         TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
111
112         if(!lpDropFileStruct) goto end;
113
114         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
115
116         while (i++ < wFile)
117         {
118           while (*lpDrop++); /* skip filename */
119           if (!*lpDrop)
120           {
121             i = (wFile == 0xFFFF) ? i : 0;
122             goto end;
123           }
124         }
125
126         i = strlen(lpDrop);
127         i++;
128         if (!lpszFile ) goto end;   /* needed buffer size */
129         i = (wLength > i) ? i : wLength;
130         lstrcpynA (lpszFile,  lpDrop,  i);
131 end:
132         GlobalUnlock16(hDrop);
133         return i;
134 }
135
136 /*************************************************************************
137  *                              DragFinish              [SHELL.12]
138  */
139 void WINAPI DragFinish16(HDROP16 h)
140 {
141     TRACE("\n");
142     GlobalFree16((HGLOBAL16)h);
143 }
144
145
146 /*************************************************************************
147  *                              DragQueryPoint          [SHELL.13]
148  */
149 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
150 {
151   LPDROPFILESTRUCT16 lpDropFileStruct;
152   BOOL16           bRet;
153   TRACE("\n");
154   lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
155
156   memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
157   bRet = lpDropFileStruct->fInNonClientArea;
158
159   GlobalUnlock16(hDrop);
160   return bRet;
161 }
162
163 /*************************************************************************
164  *             FindExecutable   (SHELL.21)
165  */
166 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
167                                      LPSTR lpResult )
168 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
169 }
170
171 /*************************************************************************
172  *             AboutDlgProc   (SHELL.33)
173  */
174 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
175                                LPARAM lParam )
176 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
177 }
178
179
180 /*************************************************************************
181  *             ShellAbout   (SHELL.22)
182  */
183 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
184                             HICON16 hIcon )
185 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
186 }
187
188 /*************************************************************************
189  *                      InternalExtractIcon             [SHELL.39]
190  *
191  * This abortion is called directly by Progman
192  */
193 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
194                                      LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
195 {
196     HGLOBAL16 hRet = 0;
197     HICON16 *RetPtr = NULL;
198     OFSTRUCT ofs;
199
200         TRACE("(%04x,file %s,start %d,extract %d\n",
201                        hInstance, lpszExeFileName, nIconIndex, n);
202
203         if (!n)
204           return 0;
205
206         hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
207         RetPtr = (HICON16*)GlobalLock16(hRet);
208
209         if (nIconIndex == (UINT16)-1)  /* get number of icons */
210         {
211           RetPtr[0] = PrivateExtractIconsA(ofs.szPathName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
212         }
213         else
214         {
215           UINT ret;
216           HICON *icons;
217
218           icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
219           ret = PrivateExtractIconsA(ofs.szPathName, nIconIndex,
220                                      GetSystemMetrics(SM_CXICON),
221                                      GetSystemMetrics(SM_CYICON),
222                                      icons, NULL, n, LR_DEFAULTCOLOR);
223           if ((ret != 0xffffffff) && ret)
224           {
225             int i;
226             for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
227           }
228           else
229           {
230             GlobalFree16(hRet);
231             hRet = 0;
232           }
233           HeapFree(GetProcessHeap(), 0, icons);
234         }
235         return hRet;
236 }
237
238 /*************************************************************************
239  *             ExtractIcon   (SHELL.34)
240  */
241 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
242         UINT16 nIconIndex )
243 {   TRACE("\n");
244     return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
245 }
246
247 /*************************************************************************
248  *             ExtractIconEx   (SHELL.40)
249  */
250 HICON16 WINAPI ExtractIconEx16(
251         LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
252         HICON16 *phiconSmall, UINT16 nIcons
253 ) {
254     HICON       *ilarge,*ismall;
255     UINT16      ret;
256     int         i;
257
258     if (phiconLarge)
259         ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
260     else
261         ilarge = NULL;
262     if (phiconSmall)
263         ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
264     else
265         ismall = NULL;
266     ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
267     if (ilarge) {
268         for (i=0;i<nIcons;i++)
269             phiconLarge[i]=HICON_16(ilarge[i]);
270         HeapFree(GetProcessHeap(),0,ilarge);
271     }
272     if (ismall) {
273         for (i=0;i<nIcons;i++)
274             phiconSmall[i]=HICON_16(ismall[i]);
275         HeapFree(GetProcessHeap(),0,ismall);
276     }
277     return ret;
278 }
279
280 /*************************************************************************
281  *                              ExtractAssociatedIcon   [SHELL.36]
282  *
283  * Return icon for given file (either from file itself or from associated
284  * executable) and patch parameters if needed.
285  */
286 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
287 {
288     return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
289                     lpiIcon));
290 }
291
292 /*************************************************************************
293  *                              FindEnvironmentString   [SHELL.38]
294  *
295  * Returns a pointer into the DOS environment... Ugh.
296  */
297 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
298 { UINT16 l;
299
300   TRACE("\n");
301
302   l = strlen(entry);
303   for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
304   { if( strncasecmp(lpEnv, entry, l) )
305       continue;
306         if( !*(lpEnv+l) )
307             return (lpEnv + l);                 /* empty entry */
308         else if ( *(lpEnv+l)== '=' )
309             return (lpEnv + l + 1);
310     }
311     return NULL;
312 }
313
314 /**********************************************************************/
315
316 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
317 { SEGPTR  spEnv;
318   LPSTR lpEnv,lpString;
319   TRACE("\n");
320
321   spEnv = GetDOSEnvironment16();
322
323   lpEnv = MapSL(spEnv);
324   lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
325
326     if( lpString )              /*  offset should be small enough */
327         return spEnv + (lpString - lpEnv);
328     return (SEGPTR)NULL;
329 }
330
331 /*************************************************************************
332  *                              DoEnvironmentSubst      [SHELL.37]
333  *
334  * Replace %KEYWORD% in the str with the value of variable KEYWORD
335  * from "DOS" environment. If it is not found the %KEYWORD% is left
336  * intact. If the buffer is too small, str is not modified.
337  *
338  * str         [I] '\0' terminated string with %keyword%.
339  *             [O] '\0' terminated string with %keyword% substituted.
340  * length      [I] size of str.
341  *
342  * Return
343  *     str length in the LOWORD and 1 in HIWORD if subst was successful.
344  */
345 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
346 {
347   LPSTR   lpEnv = MapSL(GetDOSEnvironment16());
348   LPSTR   lpstr = str;
349   LPSTR   lpend;
350   LPSTR   lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
351   WORD    bufCnt = 0;
352   WORD    envKeyLen;
353   LPSTR   lpKey;
354   WORD    retStatus = 0;
355   WORD    retLength = length;
356
357   CharToOemA(str,str);
358
359   TRACE("accept %s\n", str);
360
361   while( *lpstr && bufCnt <= length - 1 ) {
362      if ( *lpstr != '%' ) {
363         lpBuffer[bufCnt++] = *lpstr++;
364         continue;
365      }
366
367      for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
368
369      envKeyLen = lpend - lpstr - 1;
370      if( *lpend != '%' || envKeyLen == 0)
371         goto err; /* "%\0" or "%%" found; back off and whine */
372
373      *lpend = '\0';
374      lpKey = SHELL_FindString(lpEnv, lpstr+1);
375      *lpend = '%';
376      if( lpKey ) {
377          int l = strlen(lpKey);
378
379          if( bufCnt + l > length - 1 )
380                 goto err;
381
382         memcpy(lpBuffer + bufCnt, lpKey, l);
383         bufCnt += l;
384      } else { /* Keyword not found; Leave the %KEYWORD% intact */
385         if( bufCnt + envKeyLen + 2 > length - 1 )
386             goto err;
387
388          memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
389         bufCnt += envKeyLen + 2;
390      }
391
392      lpstr = lpend + 1;
393   }
394
395   if (!*lpstr && bufCnt <= length - 1) {
396       memcpy(str,lpBuffer, bufCnt);
397       str[bufCnt] = '\0';
398       retLength = bufCnt + 1;
399       retStatus = 1;
400   }
401
402   err:
403   if (!retStatus)
404       WARN("-- Env subst aborted - string too short or invalid input\n");
405   TRACE("-- return %s\n", str);
406
407   OemToCharA(str,str);
408   HeapFree( GetProcessHeap(), 0, lpBuffer);
409
410   return (DWORD)MAKELONG(retLength, retStatus);
411 }
412
413 /*************************************************************************
414  *                              SHELL_HookProc
415  *
416  * 32-bit version of the system-wide WH_SHELL hook.
417  */
418 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
419 {
420     TRACE("%i, %x, %08lx\n", code, wParam, lParam );
421
422     if (SHELL_hWnd)
423     {
424         switch( code )
425         {
426         case HSHELL_WINDOWCREATED:
427             PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
428             break;
429         case HSHELL_WINDOWDESTROYED:
430             PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
431             break;
432         case HSHELL_ACTIVATESHELLWINDOW:
433             PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
434             break;
435         }
436     }
437     return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
438 }
439
440 /*************************************************************************
441  *                              ShellHookProc           [SHELL.103]
442  * System-wide WH_SHELL hook.
443  */
444 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
445 {
446     return SHELL_HookProc( code, wParam, lParam );
447 }
448
449 /*************************************************************************
450  *                              RegisterShellHook       [SHELL.102]
451  */
452 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
453 {
454     TRACE("%04x [%u]\n", hWnd, uAction );
455
456     switch( uAction )
457     {
458     case 2:  /* register hWnd as a shell window */
459         if( !SHELL_hHook )
460         {
461             SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
462                                              GetModuleHandleA("shell32.dll"), 0 );
463             if ( SHELL_hHook )
464             {
465                 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
466                 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
467                 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
468             }
469             else
470                 WARN("-- unable to install ShellHookProc()!\n");
471         }
472
473         if ( SHELL_hHook )
474             return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
475         break;
476
477     default:
478         WARN("-- unknown code %i\n", uAction );
479         SHELL_hWnd = 0; /* just in case */
480     }
481     return FALSE;
482 }
483
484
485 /***********************************************************************
486  *           DriveType   (SHELL.262)
487  */
488 UINT16 WINAPI DriveType16( UINT16 drive )
489 {
490     UINT ret;
491     char path[] = "A:\\";
492     path[0] += drive;
493     ret = GetDriveTypeA(path);
494     switch(ret)  /* some values are not supported in Win16 */
495     {
496     case DRIVE_CDROM:
497         ret = DRIVE_REMOTE;
498         break;
499     case DRIVE_NO_ROOT_DIR:
500         ret = DRIVE_UNKNOWN;
501         break;
502     }
503     return ret;
504 }
505
506
507 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
508  * some programs. Do not remove those cases. -MM
509  */
510 static inline void fix_win16_hkey( HKEY *hkey )
511 {
512     if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
513 }
514
515 /******************************************************************************
516  *           RegOpenKey   [SHELL.1]
517  */
518 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
519 {
520     fix_win16_hkey( &hkey );
521     return RegOpenKeyA( hkey, name, retkey );
522 }
523
524 /******************************************************************************
525  *           RegCreateKey   [SHELL.2]
526  */
527 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
528 {
529     fix_win16_hkey( &hkey );
530     return RegCreateKeyA( hkey, name, retkey );
531 }
532
533 /******************************************************************************
534  *           RegCloseKey   [SHELL.3]
535  */
536 DWORD WINAPI RegCloseKey16( HKEY hkey )
537 {
538     fix_win16_hkey( &hkey );
539     return RegCloseKey( hkey );
540 }
541
542 /******************************************************************************
543  *           RegDeleteKey   [SHELL.4]
544  */
545 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
546 {
547     fix_win16_hkey( &hkey );
548     return RegDeleteKeyA( hkey, name );
549 }
550
551 /******************************************************************************
552  *           RegSetValue   [SHELL.5]
553  */
554 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
555 {
556     fix_win16_hkey( &hkey );
557     return RegSetValueA( hkey, name, type, data, count );
558 }
559
560 /******************************************************************************
561  *           RegQueryValue   [SHELL.6]
562  *
563  * NOTES
564  *    Is this HACK still applicable?
565  *
566  * HACK
567  *    The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
568  *    mask out the high 16 bit.  This (not so much incidently) hopefully fixes
569  *    Aldus FH4)
570  */
571 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
572 )
573 {
574     fix_win16_hkey( &hkey );
575     if (count) *count &= 0xffff;
576     return RegQueryValueA( hkey, name, data, (LONG*) count );
577 }
578
579 /******************************************************************************
580  *           RegEnumKey   [SHELL.7]
581  */
582 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
583 {
584     fix_win16_hkey( &hkey );
585     return RegEnumKeyA( hkey, index, name, name_len );
586 }
587
588 /*************************************************************************
589  *           SHELL_Execute16 [Internal]
590  */
591 static UINT SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
592                             LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
593 {
594     UINT ret;
595     char sCmd[MAX_PATH];
596     WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
597     ret = WinExec16(sCmd, (UINT16)psei->nShow);
598     psei_out->hInstApp = HINSTANCE_32(ret);
599     return ret;
600 }
601
602 /*************************************************************************
603  *                              ShellExecute            [SHELL.20]
604  */
605 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
606                                    LPCSTR lpFile, LPCSTR lpParameters,
607                                    LPCSTR lpDirectory, INT16 iShowCmd )
608 {
609     SHELLEXECUTEINFOW seiW;
610     WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
611     HANDLE hProcess = 0;
612
613     seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
614     seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
615     seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
616     seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
617
618     seiW.cbSize = sizeof(seiW);
619     seiW.fMask = 0;
620     seiW.hwnd = HWND_32(hWnd);
621     seiW.nShow = iShowCmd;
622     seiW.lpIDList = 0;
623     seiW.lpClass = 0;
624     seiW.hkeyClass = 0;
625     seiW.dwHotKey = 0;
626     seiW.hProcess = hProcess;
627
628     SHELL_execute( &seiW, SHELL_Execute16, FALSE );
629
630     if (wVerb) SHFree(wVerb);
631     if (wFile) SHFree(wFile);
632     if (wParameters) SHFree(wParameters);
633     if (wDirectory) SHFree(wDirectory);
634
635     return HINSTANCE_16(seiW.hInstApp);
636 }