vbscript: Fix memory leak in owned safearray iterator.
[wine.git] / dlls / shell.dll16 / shell.c
blob342adeb9a4a6ec692ac95901f0a9255ef100f863
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2000 Juergen Schmied
6 * Copyright 2002 Eric Pouech
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.
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.
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
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "wownt32.h"
32 #include "shellapi.h"
33 #include "winuser.h"
34 #include "wingdi.h"
35 #include "shlobj.h"
36 #include "shlwapi.h"
37 #include "winternl.h"
39 #include "wine/winbase16.h"
40 #include "wine/winuser16.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46 extern HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
47 LPCSTR lpParameters,LPCSTR lpDirectory,
48 INT iShowCmd, void *callback);
50 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
51 #define HINSTANCE_16(h32) (LOWORD(h32))
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;
60 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
61 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
62 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
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;
70 static HICON convert_icon_to_32( HICON16 icon16 )
72 CURSORICONINFO *info = GlobalLock16( icon16 );
73 void *and_bits = info + 1;
74 void *xor_bits = (BYTE *)and_bits + info->nHeight * 2 * ((info->nWidth + 15) / 16);
75 HICON ret = CreateIcon( 0, info->nWidth, info->nHeight, info->bPlanes, info->bBitsPerPixel,
76 and_bits, xor_bits );
77 GlobalUnlock16( icon16 );
78 return ret;
81 static HICON16 convert_icon_to_16( HINSTANCE16 inst, HICON icon )
83 static HICON16 (WINAPI *pCreateIcon16)(HINSTANCE16,INT16,INT16,BYTE,BYTE,LPCVOID,LPCVOID);
84 ICONINFO info;
85 BITMAP bm;
86 UINT and_size, xor_size;
87 void *xor_bits = NULL, *and_bits;
88 HICON16 handle = 0;
90 if (!pCreateIcon16 &&
91 !(pCreateIcon16 = (void *)GetProcAddress( GetModuleHandleA("user.exe16"), "CreateIcon16" )))
92 return 0;
94 if (!(GetIconInfo( icon, &info ))) return 0;
95 GetObjectW( info.hbmMask, sizeof(bm), &bm );
96 and_size = bm.bmHeight * bm.bmWidthBytes;
97 if (!(and_bits = HeapAlloc( GetProcessHeap(), 0, and_size ))) goto done;
98 GetBitmapBits( info.hbmMask, and_size, and_bits );
99 if (info.hbmColor)
101 GetObjectW( info.hbmColor, sizeof(bm), &bm );
102 xor_size = bm.bmHeight * bm.bmWidthBytes;
103 if (!(xor_bits = HeapAlloc( GetProcessHeap(), 0, xor_size ))) goto done;
104 GetBitmapBits( info.hbmColor, xor_size, xor_bits );
106 else
108 bm.bmHeight /= 2;
109 xor_bits = (char *)and_bits + and_size / 2;
111 handle = pCreateIcon16( inst, bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel,
112 and_bits, xor_bits );
113 done:
114 HeapFree( GetProcessHeap(), 0, and_bits );
115 if (info.hbmColor)
117 HeapFree( GetProcessHeap(), 0, xor_bits );
118 DeleteObject( info.hbmColor );
120 DeleteObject( info.hbmMask );
121 DestroyIcon( icon );
122 return handle;
125 /***********************************************************************
126 * DllEntryPoint [SHELL.101]
128 * Initialization code for shell.dll. Automatically loads the
129 * 32-bit shell32.dll to allow thunking up to 32-bit code.
131 * RETURNS
132 * Success: TRUE. Initialization completed successfully.
133 * Failure: FALSE.
135 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
136 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
138 return TRUE;
141 /*************************************************************************
142 * DragAcceptFiles [SHELL.9]
144 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
146 DragAcceptFiles(HWND_32(hWnd), b);
149 /*************************************************************************
150 * DragQueryFile [SHELL.11]
152 UINT16 WINAPI DragQueryFile16(
153 HDROP16 hDrop,
154 WORD wFile,
155 LPSTR lpszFile,
156 WORD wLength)
158 LPSTR lpDrop;
159 UINT i = 0;
160 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
162 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
164 if(!lpDropFileStruct) goto end;
166 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
168 while (i++ < wFile)
170 while (*lpDrop++); /* skip filename */
171 if (!*lpDrop)
173 i = (wFile == 0xFFFF) ? i : 0;
174 goto end;
178 i = strlen(lpDrop);
179 if (!lpszFile ) goto end; /* needed buffer size */
180 lstrcpynA (lpszFile, lpDrop, wLength);
181 end:
182 GlobalUnlock16(hDrop);
183 return i;
186 /*************************************************************************
187 * DragFinish [SHELL.12]
189 void WINAPI DragFinish16(HDROP16 h)
191 TRACE("\n");
192 GlobalFree16(h);
196 /*************************************************************************
197 * DragQueryPoint [SHELL.13]
199 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
201 LPDROPFILESTRUCT16 lpDropFileStruct;
202 BOOL16 bRet;
203 TRACE("\n");
204 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
206 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
207 bRet = lpDropFileStruct->fInNonClientArea;
209 GlobalUnlock16(hDrop);
210 return bRet;
213 /*************************************************************************
214 * FindExecutable (SHELL.21)
216 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
217 LPSTR lpResult )
218 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
221 /*************************************************************************
222 * AboutDlgProc (SHELL.33)
224 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
225 LPARAM lParam )
227 FIXME( "stub\n" );
228 return FALSE;
232 /*************************************************************************
233 * ShellAbout (SHELL.22)
235 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON16 icon16 )
237 HICON icon = convert_icon_to_32( icon16 );
238 BOOL ret = ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, icon );
239 DestroyIcon( icon );
240 return ret;
243 /*************************************************************************
244 * InternalExtractIcon [SHELL.39]
246 * This abortion is called directly by Progman
248 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
249 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
251 HGLOBAL16 hRet = 0;
252 HICON16 *RetPtr = NULL;
254 TRACE("(%04x,file %s,start %d,extract %d\n",
255 hInstance, lpszExeFileName, nIconIndex, n);
257 if (!n)
258 return 0;
260 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
261 RetPtr = GlobalLock16(hRet);
263 if (nIconIndex == (UINT16)-1) /* get number of icons */
265 RetPtr[0] = PrivateExtractIconsA(lpszExeFileName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
267 else
269 UINT ret;
270 HICON *icons;
272 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
273 ret = PrivateExtractIconsA(lpszExeFileName, nIconIndex,
274 GetSystemMetrics(SM_CXICON),
275 GetSystemMetrics(SM_CYICON),
276 icons, NULL, n, LR_DEFAULTCOLOR);
277 if ((ret != 0xffffffff) && ret)
279 int i;
280 for (i = 0; i < n; i++) RetPtr[i] = convert_icon_to_16(hInstance, icons[i]);
282 else
284 GlobalFree16(hRet);
285 hRet = 0;
287 HeapFree(GetProcessHeap(), 0, icons);
289 return hRet;
292 /*************************************************************************
293 * ExtractIcon (SHELL.34)
295 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
296 UINT16 nIconIndex )
297 { TRACE("\n");
298 return convert_icon_to_16( hInstance, ExtractIconA(NULL, lpszExeFileName, nIconIndex) );
301 /*************************************************************************
302 * ExtractIconEx (SHELL.40)
304 UINT16 WINAPI ExtractIconEx16(
305 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
306 HICON16 *phiconSmall, UINT16 nIcons
308 HICON *ilarge,*ismall;
309 int i, ret;
311 if (phiconLarge)
312 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
313 else
314 ilarge = NULL;
315 if (phiconSmall)
316 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
317 else
318 ismall = NULL;
319 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
320 if (ilarge) {
321 for (i=0;i<ret;i++)
322 phiconLarge[i] = convert_icon_to_16(0, ilarge[i]);
323 HeapFree(GetProcessHeap(),0,ilarge);
325 if (ismall) {
326 for (i=0;i<ret;i++)
327 phiconSmall[i] = convert_icon_to_16(0, ismall[i]);
328 HeapFree(GetProcessHeap(),0,ismall);
330 return ret;
333 /*************************************************************************
334 * ExtractAssociatedIcon [SHELL.36]
336 * Return icon for given file (either from file itself or from associated
337 * executable) and patch parameters if needed.
339 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
341 return convert_icon_to_16( hInst, ExtractAssociatedIconA(NULL, lpIconPath, lpiIcon) );
344 /*************************************************************************
345 * FindEnvironmentString [SHELL.38]
347 * Returns a pointer into the DOS environment... Ugh.
349 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
350 { UINT16 l;
352 TRACE("\n");
354 l = strlen(entry);
355 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
356 { if( _strnicmp(lpEnv, entry, l) )
357 continue;
358 if( !*(lpEnv+l) )
359 return (lpEnv + l); /* empty entry */
360 else if ( *(lpEnv+l)== '=' )
361 return (lpEnv + l + 1);
363 return NULL;
366 /**********************************************************************/
368 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
369 { SEGPTR spEnv;
370 LPSTR lpEnv,lpString;
371 TRACE("\n");
373 spEnv = GetDOSEnvironment16();
375 lpEnv = MapSL(spEnv);
376 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
378 if( lpString ) /* offset should be small enough */
379 return spEnv + (lpString - lpEnv);
380 return 0;
383 /*************************************************************************
384 * DoEnvironmentSubst [SHELL.37]
386 * Replace %KEYWORD% in the str with the value of variable KEYWORD
387 * from "DOS" environment. If it is not found the %KEYWORD% is left
388 * intact. If the buffer is too small, str is not modified.
390 * PARAMS
391 * str [I] '\0' terminated string with %keyword%.
392 * [O] '\0' terminated string with %keyword% substituted.
393 * length [I] size of str.
395 * RETURNS
396 * str length in the LOWORD and 1 in HIWORD if subst was successful.
398 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
400 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
401 LPSTR lpstr = str;
402 LPSTR lpend;
403 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
404 WORD bufCnt = 0;
405 WORD envKeyLen;
406 LPSTR lpKey;
407 WORD retStatus = 0;
408 WORD retLength = length;
410 CharToOemA(str,str);
412 TRACE("accept %s\n", str);
414 while( *lpstr && bufCnt <= length - 1 ) {
415 if ( *lpstr != '%' ) {
416 lpBuffer[bufCnt++] = *lpstr++;
417 continue;
420 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
422 envKeyLen = lpend - lpstr - 1;
423 if( *lpend != '%' || envKeyLen == 0)
424 goto err; /* "%\0" or "%%" found; back off and whine */
426 *lpend = '\0';
427 lpKey = SHELL_FindString(lpEnv, lpstr+1);
428 *lpend = '%';
429 if( lpKey ) {
430 int l = strlen(lpKey);
432 if( bufCnt + l > length - 1 )
433 goto err;
435 memcpy(lpBuffer + bufCnt, lpKey, l);
436 bufCnt += l;
437 } else { /* Keyword not found; Leave the %KEYWORD% intact */
438 if( bufCnt + envKeyLen + 2 > length - 1 )
439 goto err;
441 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
442 bufCnt += envKeyLen + 2;
445 lpstr = lpend + 1;
448 if (!*lpstr && bufCnt <= length - 1) {
449 memcpy(str,lpBuffer, bufCnt);
450 str[bufCnt] = '\0';
451 retLength = bufCnt + 1;
452 retStatus = 1;
455 err:
456 if (!retStatus)
457 WARN("-- Env subst aborted - string too short or invalid input\n");
458 TRACE("-- return %s\n", str);
460 OemToCharA(str,str);
461 HeapFree( GetProcessHeap(), 0, lpBuffer);
463 return (DWORD)MAKELONG(retLength, retStatus);
466 /*************************************************************************
467 * SHELL_HookProc
469 * 32-bit version of the system-wide WH_SHELL hook.
471 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
473 TRACE("%i, %x, %08Ix\n", code, wParam, lParam );
475 if (SHELL_hWnd)
477 switch( code )
479 case HSHELL_WINDOWCREATED:
480 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
481 break;
482 case HSHELL_WINDOWDESTROYED:
483 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
484 break;
485 case HSHELL_ACTIVATESHELLWINDOW:
486 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
487 break;
490 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
493 /*************************************************************************
494 * ShellHookProc [SHELL.103]
495 * System-wide WH_SHELL hook.
497 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
499 return SHELL_HookProc( code, wParam, lParam );
502 /*************************************************************************
503 * RegisterShellHook [SHELL.102]
505 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
507 TRACE("%04x [%u]\n", hWnd, uAction );
509 switch( uAction )
511 case 2: /* register hWnd as a shell window */
512 if( !SHELL_hHook )
514 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
515 GetModuleHandleA("shell32.dll"), 0 );
516 if ( SHELL_hHook )
518 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
519 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
520 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
522 else
523 WARN("-- unable to install ShellHookProc()!\n");
526 if ( SHELL_hHook )
527 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
528 break;
530 default:
531 WARN("-- unknown code %i\n", uAction );
532 SHELL_hWnd = 0; /* just in case */
534 return FALSE;
538 /***********************************************************************
539 * DriveType (SHELL.262)
541 UINT16 WINAPI DriveType16( UINT16 drive )
543 UINT ret;
544 char path[] = "A:\\";
545 path[0] += drive;
546 ret = GetDriveTypeA(path);
547 switch(ret) /* some values are not supported in Win16 */
549 case DRIVE_CDROM:
550 ret = DRIVE_REMOTE;
551 break;
552 case DRIVE_NO_ROOT_DIR:
553 ret = DRIVE_UNKNOWN;
554 break;
556 return ret;
560 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
561 * some programs. Do not remove those cases. -MM
563 static inline void fix_win16_hkey( HKEY *hkey )
565 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
568 /******************************************************************************
569 * RegOpenKey [SHELL.1]
571 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
573 fix_win16_hkey( &hkey );
574 return RegOpenKeyA( hkey, name, retkey );
577 /******************************************************************************
578 * RegCreateKey [SHELL.2]
580 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
582 fix_win16_hkey( &hkey );
583 return RegCreateKeyA( hkey, name, retkey );
586 /******************************************************************************
587 * RegCloseKey [SHELL.3]
589 DWORD WINAPI RegCloseKey16( HKEY hkey )
591 fix_win16_hkey( &hkey );
592 return RegCloseKey( hkey );
595 /******************************************************************************
596 * RegDeleteKey [SHELL.4]
598 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
600 fix_win16_hkey( &hkey );
601 return RegDeleteKeyA( hkey, name );
604 /******************************************************************************
605 * RegSetValue [SHELL.5]
607 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
609 fix_win16_hkey( &hkey );
610 return RegSetValueA( hkey, name, type, data, count );
613 /******************************************************************************
614 * RegQueryValue [SHELL.6]
616 * NOTES
617 * Is this HACK still applicable?
619 * HACK
620 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
621 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
622 * Aldus FH4)
624 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
627 fix_win16_hkey( &hkey );
628 if (count) *count &= 0xffff;
629 return RegQueryValueA( hkey, name, data, (LONG*) count );
632 /******************************************************************************
633 * RegEnumKey [SHELL.7]
635 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
637 fix_win16_hkey( &hkey );
638 return RegEnumKeyA( hkey, index, name, name_len );
641 /*************************************************************************
642 * SHELL_Execute16 [Internal]
644 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
645 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
647 UINT ret;
648 char sCmd[MAX_PATH];
649 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
650 ret = WinExec16(sCmd, (UINT16)psei->nShow);
651 psei_out->hInstApp = HINSTANCE_32(ret);
652 return ret;
655 /*************************************************************************
656 * ShellExecute [SHELL.20]
658 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
659 LPCSTR lpFile, LPCSTR lpParameters,
660 LPCSTR lpDirectory, INT16 iShowCmd )
662 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd), lpOperation, lpFile, lpParameters,
663 lpDirectory, iShowCmd, SHELL_Execute16 ));
667 /*************************************************************************
668 * RunDLL_CallEntry16
670 * Only exported from shell32 on Windows, probably imported
671 * from shell through the 16/32 thunks.
673 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst, LPSTR cmdline, INT cmdshow )
675 WORD args[5];
676 SEGPTR cmdline_seg;
678 TRACE( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
679 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
681 cmdline_seg = MapLS( cmdline );
682 args[4] = HWND_16(hwnd);
683 args[3] = MapHModuleLS(inst);
684 args[2] = SELECTOROF(cmdline_seg);
685 args[1] = OFFSETOF(cmdline_seg);
686 args[0] = cmdshow;
687 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
688 UnMapLS( cmdline_seg );