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