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
24 #include "wine/port.h"
44 #include "wine/winbase16.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 extern HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
51 LPCSTR lpParameters,LPCSTR lpDirectory,
52 INT iShowCmd, void *callback);
54 #define HICON_16(h32) (LOWORD(h32))
55 #define HICON_32(h16) ((HICON)(ULONG_PTR)(h16))
56 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
57 #define HINSTANCE_16(h32) (LOWORD(h32))
59 typedef struct { /* structure for dropped files */
62 BOOL16 fInNonClientArea;
63 /* memory block with filenames follows */
64 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
66 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
67 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
68 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
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;
76 /***********************************************************************
77 * DllEntryPoint [SHELL.101]
79 * Initialization code for shell.dll. Automatically loads the
80 * 32-bit shell32.dll to allow thunking up to 32-bit code.
83 * Success: TRUE. Initialization completed successfully.
86 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
87 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
92 /*************************************************************************
93 * DragAcceptFiles [SHELL.9]
95 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
97 DragAcceptFiles(HWND_32(hWnd), b);
100 /*************************************************************************
101 * DragQueryFile [SHELL.11]
103 UINT16 WINAPI DragQueryFile16(
111 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
113 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
115 if(!lpDropFileStruct) goto end;
117 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
121 while (*lpDrop++); /* skip filename */
124 i = (wFile == 0xFFFF) ? i : 0;
130 if (!lpszFile ) goto end; /* needed buffer size */
131 lstrcpynA (lpszFile, lpDrop, wLength);
133 GlobalUnlock16(hDrop);
137 /*************************************************************************
138 * DragFinish [SHELL.12]
140 void WINAPI DragFinish16(HDROP16 h)
147 /*************************************************************************
148 * DragQueryPoint [SHELL.13]
150 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
152 LPDROPFILESTRUCT16 lpDropFileStruct;
155 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
157 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
158 bRet = lpDropFileStruct->fInNonClientArea;
160 GlobalUnlock16(hDrop);
164 /*************************************************************************
165 * FindExecutable (SHELL.21)
167 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
169 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
172 /*************************************************************************
173 * AboutDlgProc (SHELL.33)
175 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
183 /*************************************************************************
184 * ShellAbout (SHELL.22)
186 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
188 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
191 /*************************************************************************
192 * InternalExtractIcon [SHELL.39]
194 * This abortion is called directly by Progman
196 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
197 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
200 HICON16 *RetPtr = NULL;
202 TRACE("(%04x,file %s,start %d,extract %d\n",
203 hInstance, lpszExeFileName, nIconIndex, n);
208 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
209 RetPtr = GlobalLock16(hRet);
211 if (nIconIndex == (UINT16)-1) /* get number of icons */
213 RetPtr[0] = PrivateExtractIconsA(lpszExeFileName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
220 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
221 ret = PrivateExtractIconsA(lpszExeFileName, nIconIndex,
222 GetSystemMetrics(SM_CXICON),
223 GetSystemMetrics(SM_CYICON),
224 icons, NULL, n, LR_DEFAULTCOLOR);
225 if ((ret != 0xffffffff) && ret)
228 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
235 HeapFree(GetProcessHeap(), 0, icons);
240 /*************************************************************************
241 * ExtractIcon (SHELL.34)
243 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
246 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
249 /*************************************************************************
250 * ExtractIconEx (SHELL.40)
252 HICON16 WINAPI ExtractIconEx16(
253 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
254 HICON16 *phiconSmall, UINT16 nIcons
256 HICON *ilarge,*ismall;
261 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
265 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
268 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
270 for (i=0;i<nIcons;i++)
271 phiconLarge[i]=HICON_16(ilarge[i]);
272 HeapFree(GetProcessHeap(),0,ilarge);
275 for (i=0;i<nIcons;i++)
276 phiconSmall[i]=HICON_16(ismall[i]);
277 HeapFree(GetProcessHeap(),0,ismall);
282 /*************************************************************************
283 * ExtractAssociatedIcon [SHELL.36]
285 * Return icon for given file (either from file itself or from associated
286 * executable) and patch parameters if needed.
288 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
290 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
294 /*************************************************************************
295 * FindEnvironmentString [SHELL.38]
297 * Returns a pointer into the DOS environment... Ugh.
299 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
305 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
306 { if( strncasecmp(lpEnv, entry, l) )
309 return (lpEnv + l); /* empty entry */
310 else if ( *(lpEnv+l)== '=' )
311 return (lpEnv + l + 1);
316 /**********************************************************************/
318 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
320 LPSTR lpEnv,lpString;
323 spEnv = GetDOSEnvironment16();
325 lpEnv = MapSL(spEnv);
326 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
328 if( lpString ) /* offset should be small enough */
329 return spEnv + (lpString - lpEnv);
333 /*************************************************************************
334 * DoEnvironmentSubst [SHELL.37]
336 * Replace %KEYWORD% in the str with the value of variable KEYWORD
337 * from "DOS" environment. If it is not found the %KEYWORD% is left
338 * intact. If the buffer is too small, str is not modified.
341 * str [I] '\0' terminated string with %keyword%.
342 * [O] '\0' terminated string with %keyword% substituted.
343 * length [I] size of str.
346 * str length in the LOWORD and 1 in HIWORD if subst was successful.
348 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
350 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
353 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
358 WORD retLength = length;
362 TRACE("accept %s\n", str);
364 while( *lpstr && bufCnt <= length - 1 ) {
365 if ( *lpstr != '%' ) {
366 lpBuffer[bufCnt++] = *lpstr++;
370 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
372 envKeyLen = lpend - lpstr - 1;
373 if( *lpend != '%' || envKeyLen == 0)
374 goto err; /* "%\0" or "%%" found; back off and whine */
377 lpKey = SHELL_FindString(lpEnv, lpstr+1);
380 int l = strlen(lpKey);
382 if( bufCnt + l > length - 1 )
385 memcpy(lpBuffer + bufCnt, lpKey, l);
387 } else { /* Keyword not found; Leave the %KEYWORD% intact */
388 if( bufCnt + envKeyLen + 2 > length - 1 )
391 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
392 bufCnt += envKeyLen + 2;
398 if (!*lpstr && bufCnt <= length - 1) {
399 memcpy(str,lpBuffer, bufCnt);
401 retLength = bufCnt + 1;
407 WARN("-- Env subst aborted - string too short or invalid input\n");
408 TRACE("-- return %s\n", str);
411 HeapFree( GetProcessHeap(), 0, lpBuffer);
413 return (DWORD)MAKELONG(retLength, retStatus);
416 /*************************************************************************
419 * 32-bit version of the system-wide WH_SHELL hook.
421 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
423 TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
429 case HSHELL_WINDOWCREATED:
430 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
432 case HSHELL_WINDOWDESTROYED:
433 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
435 case HSHELL_ACTIVATESHELLWINDOW:
436 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
440 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
443 /*************************************************************************
444 * ShellHookProc [SHELL.103]
445 * System-wide WH_SHELL hook.
447 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
449 return SHELL_HookProc( code, wParam, lParam );
452 /*************************************************************************
453 * RegisterShellHook [SHELL.102]
455 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
457 TRACE("%04x [%u]\n", hWnd, uAction );
461 case 2: /* register hWnd as a shell window */
464 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
465 GetModuleHandleA("shell32.dll"), 0 );
468 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
469 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
470 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
473 WARN("-- unable to install ShellHookProc()!\n");
477 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
481 WARN("-- unknown code %i\n", uAction );
482 SHELL_hWnd = 0; /* just in case */
488 /***********************************************************************
489 * DriveType (SHELL.262)
491 UINT16 WINAPI DriveType16( UINT16 drive )
494 char path[] = "A:\\";
496 ret = GetDriveTypeA(path);
497 switch(ret) /* some values are not supported in Win16 */
502 case DRIVE_NO_ROOT_DIR:
510 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
511 * some programs. Do not remove those cases. -MM
513 static inline void fix_win16_hkey( HKEY *hkey )
515 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
518 /******************************************************************************
519 * RegOpenKey [SHELL.1]
521 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
523 fix_win16_hkey( &hkey );
524 return RegOpenKeyA( hkey, name, retkey );
527 /******************************************************************************
528 * RegCreateKey [SHELL.2]
530 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
532 fix_win16_hkey( &hkey );
533 return RegCreateKeyA( hkey, name, retkey );
536 /******************************************************************************
537 * RegCloseKey [SHELL.3]
539 DWORD WINAPI RegCloseKey16( HKEY hkey )
541 fix_win16_hkey( &hkey );
542 return RegCloseKey( hkey );
545 /******************************************************************************
546 * RegDeleteKey [SHELL.4]
548 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
550 fix_win16_hkey( &hkey );
551 return RegDeleteKeyA( hkey, name );
554 /******************************************************************************
555 * RegSetValue [SHELL.5]
557 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
559 fix_win16_hkey( &hkey );
560 return RegSetValueA( hkey, name, type, data, count );
563 /******************************************************************************
564 * RegQueryValue [SHELL.6]
567 * Is this HACK still applicable?
570 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
571 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
574 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
577 fix_win16_hkey( &hkey );
578 if (count) *count &= 0xffff;
579 return RegQueryValueA( hkey, name, data, (LONG*) count );
582 /******************************************************************************
583 * RegEnumKey [SHELL.7]
585 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
587 fix_win16_hkey( &hkey );
588 return RegEnumKeyA( hkey, index, name, name_len );
591 /*************************************************************************
592 * SHELL_Execute16 [Internal]
594 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
595 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
599 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
600 ret = WinExec16(sCmd, (UINT16)psei->nShow);
601 psei_out->hInstApp = HINSTANCE_32(ret);
605 /*************************************************************************
606 * ShellExecute [SHELL.20]
608 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
609 LPCSTR lpFile, LPCSTR lpParameters,
610 LPCSTR lpDirectory, INT16 iShowCmd )
612 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd), lpOperation, lpFile, lpParameters,
613 lpDirectory, iShowCmd, SHELL_Execute16 ));