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