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