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