ntdll: Document RtlSetThreadErrorMode and RtlGetThreadErrorMode.
[wine/multimedia.git] / dlls / shell.dll16 / shell.c
blobbe8ffeb817007be8911dd913f078c2edd5b66265
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 "config.h"
24 #include "wine/port.h"
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>
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"
44 #include "wine/winbase16.h"
45 #include "wine/winuser16.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
51 extern HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
52 LPCSTR lpParameters,LPCSTR lpDirectory,
53 INT iShowCmd, void *callback);
55 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
56 #define HINSTANCE_16(h32) (LOWORD(h32))
58 typedef struct { /* structure for dropped files */
59 WORD wSize;
60 POINT16 ptMousePos;
61 BOOL16 fInNonClientArea;
62 /* memory block with filenames follows */
63 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
65 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
66 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
67 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
69 static HWND SHELL_hWnd = 0;
70 static HHOOK SHELL_hHook = 0;
71 static UINT uMsgWndCreated = 0;
72 static UINT uMsgWndDestroyed = 0;
73 static UINT uMsgShellActivate = 0;
75 static HICON convert_icon_to_32( HICON16 icon16 )
77 CURSORICONINFO *info = GlobalLock16( icon16 );
78 void *and_bits = info + 1;
79 void *xor_bits = (BYTE *)and_bits + info->nHeight * 2 * ((info->nWidth + 15) / 16);
80 HICON ret = CreateIcon( 0, info->nWidth, info->nHeight, info->bPlanes, info->bBitsPerPixel,
81 and_bits, xor_bits );
82 GlobalUnlock16( icon16 );
83 return ret;
86 static HICON16 convert_icon_to_16( HINSTANCE16 inst, HICON icon )
88 static HICON16 (WINAPI *pCreateIcon16)(HINSTANCE16,INT16,INT16,BYTE,BYTE,LPCVOID,LPCVOID);
89 ICONINFO info;
90 BITMAP bm;
91 UINT and_size, xor_size;
92 void *xor_bits = NULL, *and_bits;
93 HICON16 handle = 0;
95 if (!pCreateIcon16 &&
96 !(pCreateIcon16 = (void *)GetProcAddress( GetModuleHandleA("user.exe16"), "CreateIcon16" )))
97 return 0;
99 if (!(GetIconInfo( icon, &info ))) return 0;
100 GetObjectW( info.hbmMask, sizeof(bm), &bm );
101 and_size = bm.bmHeight * bm.bmWidthBytes;
102 if (!(and_bits = HeapAlloc( GetProcessHeap(), 0, and_size ))) goto done;
103 GetBitmapBits( info.hbmMask, and_size, and_bits );
104 if (info.hbmColor)
106 GetObjectW( info.hbmColor, sizeof(bm), &bm );
107 xor_size = bm.bmHeight * bm.bmWidthBytes;
108 if (!(xor_bits = HeapAlloc( GetProcessHeap(), 0, xor_size ))) goto done;
109 GetBitmapBits( info.hbmColor, xor_size, xor_bits );
111 else
113 bm.bmHeight /= 2;
114 xor_bits = (char *)and_bits + and_size / 2;
116 handle = pCreateIcon16( inst, bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel,
117 and_bits, xor_bits );
118 done:
119 HeapFree( GetProcessHeap(), 0, and_bits );
120 if (info.hbmColor)
122 HeapFree( GetProcessHeap(), 0, xor_bits );
123 DeleteObject( info.hbmColor );
125 DeleteObject( info.hbmMask );
126 DestroyIcon( icon );
127 return handle;
130 /***********************************************************************
131 * DllEntryPoint [SHELL.101]
133 * Initialization code for shell.dll. Automatically loads the
134 * 32-bit shell32.dll to allow thunking up to 32-bit code.
136 * RETURNS
137 * Success: TRUE. Initialization completed successfully.
138 * Failure: FALSE.
140 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
141 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
143 return TRUE;
146 /*************************************************************************
147 * DragAcceptFiles [SHELL.9]
149 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
151 DragAcceptFiles(HWND_32(hWnd), b);
154 /*************************************************************************
155 * DragQueryFile [SHELL.11]
157 UINT16 WINAPI DragQueryFile16(
158 HDROP16 hDrop,
159 WORD wFile,
160 LPSTR lpszFile,
161 WORD wLength)
163 LPSTR lpDrop;
164 UINT i = 0;
165 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
167 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
169 if(!lpDropFileStruct) goto end;
171 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
173 while (i++ < wFile)
175 while (*lpDrop++); /* skip filename */
176 if (!*lpDrop)
178 i = (wFile == 0xFFFF) ? i : 0;
179 goto end;
183 i = strlen(lpDrop);
184 if (!lpszFile ) goto end; /* needed buffer size */
185 lstrcpynA (lpszFile, lpDrop, wLength);
186 end:
187 GlobalUnlock16(hDrop);
188 return i;
191 /*************************************************************************
192 * DragFinish [SHELL.12]
194 void WINAPI DragFinish16(HDROP16 h)
196 TRACE("\n");
197 GlobalFree16(h);
201 /*************************************************************************
202 * DragQueryPoint [SHELL.13]
204 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
206 LPDROPFILESTRUCT16 lpDropFileStruct;
207 BOOL16 bRet;
208 TRACE("\n");
209 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
211 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
212 bRet = lpDropFileStruct->fInNonClientArea;
214 GlobalUnlock16(hDrop);
215 return bRet;
218 /*************************************************************************
219 * FindExecutable (SHELL.21)
221 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
222 LPSTR lpResult )
223 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
226 /*************************************************************************
227 * AboutDlgProc (SHELL.33)
229 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
230 LPARAM lParam )
232 FIXME( "stub\n" );
233 return FALSE;
237 /*************************************************************************
238 * ShellAbout (SHELL.22)
240 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON16 icon16 )
242 HICON icon = convert_icon_to_32( icon16 );
243 BOOL ret = ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, icon );
244 DestroyIcon( icon );
245 return ret;
248 /*************************************************************************
249 * InternalExtractIcon [SHELL.39]
251 * This abortion is called directly by Progman
253 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
254 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
256 HGLOBAL16 hRet = 0;
257 HICON16 *RetPtr = NULL;
259 TRACE("(%04x,file %s,start %d,extract %d\n",
260 hInstance, lpszExeFileName, nIconIndex, n);
262 if (!n)
263 return 0;
265 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
266 RetPtr = GlobalLock16(hRet);
268 if (nIconIndex == (UINT16)-1) /* get number of icons */
270 RetPtr[0] = PrivateExtractIconsA(lpszExeFileName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
272 else
274 UINT ret;
275 HICON *icons;
277 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
278 ret = PrivateExtractIconsA(lpszExeFileName, nIconIndex,
279 GetSystemMetrics(SM_CXICON),
280 GetSystemMetrics(SM_CYICON),
281 icons, NULL, n, LR_DEFAULTCOLOR);
282 if ((ret != 0xffffffff) && ret)
284 int i;
285 for (i = 0; i < n; i++) RetPtr[i] = convert_icon_to_16(hInstance, icons[i]);
287 else
289 GlobalFree16(hRet);
290 hRet = 0;
292 HeapFree(GetProcessHeap(), 0, icons);
294 return hRet;
297 /*************************************************************************
298 * ExtractIcon (SHELL.34)
300 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
301 UINT16 nIconIndex )
302 { TRACE("\n");
303 return convert_icon_to_16( hInstance, ExtractIconA(NULL, lpszExeFileName, nIconIndex) );
306 /*************************************************************************
307 * ExtractIconEx (SHELL.40)
309 UINT16 WINAPI ExtractIconEx16(
310 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
311 HICON16 *phiconSmall, UINT16 nIcons
313 HICON *ilarge,*ismall;
314 int i, ret;
316 if (phiconLarge)
317 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
318 else
319 ilarge = NULL;
320 if (phiconSmall)
321 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
322 else
323 ismall = NULL;
324 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
325 if (ilarge) {
326 for (i=0;i<ret;i++)
327 phiconLarge[i] = convert_icon_to_16(0, ilarge[i]);
328 HeapFree(GetProcessHeap(),0,ilarge);
330 if (ismall) {
331 for (i=0;i<ret;i++)
332 phiconSmall[i] = convert_icon_to_16(0, ismall[i]);
333 HeapFree(GetProcessHeap(),0,ismall);
335 return ret;
338 /*************************************************************************
339 * ExtractAssociatedIcon [SHELL.36]
341 * Return icon for given file (either from file itself or from associated
342 * executable) and patch parameters if needed.
344 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
346 return convert_icon_to_16( hInst, ExtractAssociatedIconA(NULL, lpIconPath, lpiIcon) );
349 /*************************************************************************
350 * FindEnvironmentString [SHELL.38]
352 * Returns a pointer into the DOS environment... Ugh.
354 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
355 { UINT16 l;
357 TRACE("\n");
359 l = strlen(entry);
360 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
361 { if( strncasecmp(lpEnv, entry, l) )
362 continue;
363 if( !*(lpEnv+l) )
364 return (lpEnv + l); /* empty entry */
365 else if ( *(lpEnv+l)== '=' )
366 return (lpEnv + l + 1);
368 return NULL;
371 /**********************************************************************/
373 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
374 { SEGPTR spEnv;
375 LPSTR lpEnv,lpString;
376 TRACE("\n");
378 spEnv = GetDOSEnvironment16();
380 lpEnv = MapSL(spEnv);
381 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
383 if( lpString ) /* offset should be small enough */
384 return spEnv + (lpString - lpEnv);
385 return 0;
388 /*************************************************************************
389 * DoEnvironmentSubst [SHELL.37]
391 * Replace %KEYWORD% in the str with the value of variable KEYWORD
392 * from "DOS" environment. If it is not found the %KEYWORD% is left
393 * intact. If the buffer is too small, str is not modified.
395 * PARAMS
396 * str [I] '\0' terminated string with %keyword%.
397 * [O] '\0' terminated string with %keyword% substituted.
398 * length [I] size of str.
400 * RETURNS
401 * str length in the LOWORD and 1 in HIWORD if subst was successful.
403 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
405 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
406 LPSTR lpstr = str;
407 LPSTR lpend;
408 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
409 WORD bufCnt = 0;
410 WORD envKeyLen;
411 LPSTR lpKey;
412 WORD retStatus = 0;
413 WORD retLength = length;
415 CharToOemA(str,str);
417 TRACE("accept %s\n", str);
419 while( *lpstr && bufCnt <= length - 1 ) {
420 if ( *lpstr != '%' ) {
421 lpBuffer[bufCnt++] = *lpstr++;
422 continue;
425 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
427 envKeyLen = lpend - lpstr - 1;
428 if( *lpend != '%' || envKeyLen == 0)
429 goto err; /* "%\0" or "%%" found; back off and whine */
431 *lpend = '\0';
432 lpKey = SHELL_FindString(lpEnv, lpstr+1);
433 *lpend = '%';
434 if( lpKey ) {
435 int l = strlen(lpKey);
437 if( bufCnt + l > length - 1 )
438 goto err;
440 memcpy(lpBuffer + bufCnt, lpKey, l);
441 bufCnt += l;
442 } else { /* Keyword not found; Leave the %KEYWORD% intact */
443 if( bufCnt + envKeyLen + 2 > length - 1 )
444 goto err;
446 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
447 bufCnt += envKeyLen + 2;
450 lpstr = lpend + 1;
453 if (!*lpstr && bufCnt <= length - 1) {
454 memcpy(str,lpBuffer, bufCnt);
455 str[bufCnt] = '\0';
456 retLength = bufCnt + 1;
457 retStatus = 1;
460 err:
461 if (!retStatus)
462 WARN("-- Env subst aborted - string too short or invalid input\n");
463 TRACE("-- return %s\n", str);
465 OemToCharA(str,str);
466 HeapFree( GetProcessHeap(), 0, lpBuffer);
468 return (DWORD)MAKELONG(retLength, retStatus);
471 /*************************************************************************
472 * SHELL_HookProc
474 * 32-bit version of the system-wide WH_SHELL hook.
476 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
478 TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
480 if (SHELL_hWnd)
482 switch( code )
484 case HSHELL_WINDOWCREATED:
485 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
486 break;
487 case HSHELL_WINDOWDESTROYED:
488 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
489 break;
490 case HSHELL_ACTIVATESHELLWINDOW:
491 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
492 break;
495 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
498 /*************************************************************************
499 * ShellHookProc [SHELL.103]
500 * System-wide WH_SHELL hook.
502 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
504 return SHELL_HookProc( code, wParam, lParam );
507 /*************************************************************************
508 * RegisterShellHook [SHELL.102]
510 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
512 TRACE("%04x [%u]\n", hWnd, uAction );
514 switch( uAction )
516 case 2: /* register hWnd as a shell window */
517 if( !SHELL_hHook )
519 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
520 GetModuleHandleA("shell32.dll"), 0 );
521 if ( SHELL_hHook )
523 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
524 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
525 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
527 else
528 WARN("-- unable to install ShellHookProc()!\n");
531 if ( SHELL_hHook )
532 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
533 break;
535 default:
536 WARN("-- unknown code %i\n", uAction );
537 SHELL_hWnd = 0; /* just in case */
539 return FALSE;
543 /***********************************************************************
544 * DriveType (SHELL.262)
546 UINT16 WINAPI DriveType16( UINT16 drive )
548 UINT ret;
549 char path[] = "A:\\";
550 path[0] += drive;
551 ret = GetDriveTypeA(path);
552 switch(ret) /* some values are not supported in Win16 */
554 case DRIVE_CDROM:
555 ret = DRIVE_REMOTE;
556 break;
557 case DRIVE_NO_ROOT_DIR:
558 ret = DRIVE_UNKNOWN;
559 break;
561 return ret;
565 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
566 * some programs. Do not remove those cases. -MM
568 static inline void fix_win16_hkey( HKEY *hkey )
570 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
573 /******************************************************************************
574 * RegOpenKey [SHELL.1]
576 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
578 fix_win16_hkey( &hkey );
579 return RegOpenKeyA( hkey, name, retkey );
582 /******************************************************************************
583 * RegCreateKey [SHELL.2]
585 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
587 fix_win16_hkey( &hkey );
588 return RegCreateKeyA( hkey, name, retkey );
591 /******************************************************************************
592 * RegCloseKey [SHELL.3]
594 DWORD WINAPI RegCloseKey16( HKEY hkey )
596 fix_win16_hkey( &hkey );
597 return RegCloseKey( hkey );
600 /******************************************************************************
601 * RegDeleteKey [SHELL.4]
603 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
605 fix_win16_hkey( &hkey );
606 return RegDeleteKeyA( hkey, name );
609 /******************************************************************************
610 * RegSetValue [SHELL.5]
612 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
614 fix_win16_hkey( &hkey );
615 return RegSetValueA( hkey, name, type, data, count );
618 /******************************************************************************
619 * RegQueryValue [SHELL.6]
621 * NOTES
622 * Is this HACK still applicable?
624 * HACK
625 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
626 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
627 * Aldus FH4)
629 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
632 fix_win16_hkey( &hkey );
633 if (count) *count &= 0xffff;
634 return RegQueryValueA( hkey, name, data, (LONG*) count );
637 /******************************************************************************
638 * RegEnumKey [SHELL.7]
640 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
642 fix_win16_hkey( &hkey );
643 return RegEnumKeyA( hkey, index, name, name_len );
646 /*************************************************************************
647 * SHELL_Execute16 [Internal]
649 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
650 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
652 UINT ret;
653 char sCmd[MAX_PATH];
654 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
655 ret = WinExec16(sCmd, (UINT16)psei->nShow);
656 psei_out->hInstApp = HINSTANCE_32(ret);
657 return ret;
660 /*************************************************************************
661 * ShellExecute [SHELL.20]
663 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
664 LPCSTR lpFile, LPCSTR lpParameters,
665 LPCSTR lpDirectory, INT16 iShowCmd )
667 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd), lpOperation, lpFile, lpParameters,
668 lpDirectory, iShowCmd, SHELL_Execute16 ));
672 /*************************************************************************
673 * RunDLL_CallEntry16
675 * Only exported from shell32 on Windows, probably imported
676 * from shell through the 16/32 thunks.
678 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst, LPCSTR cmdline, INT cmdshow )
680 WORD args[5];
681 SEGPTR cmdline_seg;
683 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
684 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
686 cmdline_seg = MapLS( cmdline );
687 args[4] = HWND_16(hwnd);
688 args[3] = MapHModuleLS(inst);
689 args[2] = SELECTOROF(cmdline_seg);
690 args[1] = OFFSETOF(cmdline_seg);
691 args[0] = cmdshow;
692 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
693 UnMapLS( cmdline_seg );