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