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