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