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