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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
43 #include "wine/winbase16.h"
44 #include "wine/winuser16.h"
45 #include "shell32_main.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 WINE_DECLARE_DEBUG_CHANNEL(exec);
53 typedef struct { /* structure for dropped files */
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;
69 HINSTANCE16 SHELL_hInstance = 0;
70 HINSTANCE SHELL_hInstance32;
71 static int SHELL_Attach = 0;
73 /***********************************************************************
74 * DllEntryPoint [SHELL.101]
76 * Initialization code for shell.dll. Automatically loads the
77 * 32-bit shell32.dll to allow thunking up to 32-bit code.
81 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
82 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
84 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
85 Reason, hInst, ds, HeapSize, res1, res2);
89 case DLL_PROCESS_ATTACH:
90 if (SHELL_Attach++) break;
91 SHELL_hInstance = hInst;
92 if(!SHELL_hInstance32)
94 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
96 ERR("Could not load sibling shell32.dll\n");
102 case DLL_PROCESS_DETACH:
106 if(SHELL_hInstance32)
107 FreeLibrary(SHELL_hInstance32);
114 /*************************************************************************
115 * DragAcceptFiles [SHELL.9]
117 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
119 DragAcceptFiles(HWND_32(hWnd), b);
122 /*************************************************************************
123 * DragQueryFile [SHELL.11]
125 UINT16 WINAPI DragQueryFile16(
133 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
135 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
137 if(!lpDropFileStruct) goto end;
139 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
140 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
144 while (*lpDrop++); /* skip filename */
147 i = (wFile == 0xFFFFFFFF) ? i : 0;
154 if (!lpszFile ) goto end; /* needed buffer size */
155 i = (wLength > i) ? i : wLength;
156 lstrcpynA (lpszFile, lpDrop, i);
158 GlobalUnlock16(hDrop);
162 /*************************************************************************
163 * DragFinish [SHELL.12]
165 void WINAPI DragFinish16(HDROP16 h)
168 GlobalFree16((HGLOBAL16)h);
172 /*************************************************************************
173 * DragQueryPoint [SHELL.13]
175 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
177 LPDROPFILESTRUCT16 lpDropFileStruct;
180 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
182 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
183 bRet = lpDropFileStruct->fInNonClientArea;
185 GlobalUnlock16(hDrop);
189 /*************************************************************************
190 * FindExecutable (SHELL.21)
192 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
194 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
197 /*************************************************************************
198 * AboutDlgProc (SHELL.33)
200 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
202 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
206 /*************************************************************************
207 * ShellAbout (SHELL.22)
209 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
211 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
214 /*************************************************************************
215 * InternalExtractIcon [SHELL.39]
217 * This abortion is called directly by Progman
219 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
220 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
223 HICON16 *RetPtr = NULL;
226 TRACE("(%04x,file %s,start %d,extract %d\n",
227 hInstance, lpszExeFileName, nIconIndex, n);
232 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
233 RetPtr = (HICON16*)GlobalLock16(hRet);
235 if (nIconIndex == (UINT16)-1) /* get number of icons */
237 RetPtr[0] = PrivateExtractIconsA(ofs.szPathName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
244 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
245 ret = PrivateExtractIconsA(ofs.szPathName, nIconIndex,
246 GetSystemMetrics(SM_CXICON),
247 GetSystemMetrics(SM_CYICON),
248 icons, NULL, n, LR_DEFAULTCOLOR);
249 if ((ret != 0xffffffff) && ret)
252 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
259 HeapFree(GetProcessHeap(), 0, icons);
264 /*************************************************************************
265 * ExtractIcon (SHELL.34)
267 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
270 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
273 /*************************************************************************
274 * ExtractIconEx (SHELL.40)
276 HICON16 WINAPI ExtractIconEx16(
277 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
278 HICON16 *phiconSmall, UINT16 nIcons
280 HICON *ilarge,*ismall;
285 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
289 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
292 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
294 for (i=0;i<nIcons;i++)
295 phiconLarge[i]=HICON_16(ilarge[i]);
296 HeapFree(GetProcessHeap(),0,ilarge);
299 for (i=0;i<nIcons;i++)
300 phiconSmall[i]=HICON_16(ismall[i]);
301 HeapFree(GetProcessHeap(),0,ismall);
306 /*************************************************************************
307 * ExtractAssociatedIcon [SHELL.36]
309 * Return icon for given file (either from file itself or from associated
310 * executable) and patch parameters if needed.
312 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
314 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
318 /*************************************************************************
319 * FindEnvironmentString [SHELL.38]
321 * Returns a pointer into the DOS environment... Ugh.
323 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
329 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
330 { if( strncasecmp(lpEnv, entry, l) )
333 return (lpEnv + l); /* empty entry */
334 else if ( *(lpEnv+l)== '=' )
335 return (lpEnv + l + 1);
340 /**********************************************************************/
342 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
344 LPSTR lpEnv,lpString;
347 spEnv = GetDOSEnvironment16();
349 lpEnv = MapSL(spEnv);
350 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
352 if( lpString ) /* offset should be small enough */
353 return spEnv + (lpString - lpEnv);
357 /*************************************************************************
358 * DoEnvironmentSubst [SHELL.37]
360 * Replace %KEYWORD% in the str with the value of variable KEYWORD
361 * from "DOS" environment.
363 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
365 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
366 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
368 LPSTR lpbstr = lpBuffer;
372 TRACE("accept %s\n", str);
374 while( *lpstr && lpbstr - lpBuffer < length )
380 do { lpend++; } while( *lpend && *lpend != '%' );
381 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
385 lpKey = SHELL_FindString(lpEnv, lpstr+1);
386 if( lpKey ) /* found key value */
388 int l = strlen(lpKey);
390 if( l > length - (lpbstr - lpBuffer) - 1 )
392 WARN("-- Env subst aborted - string too short\n");
396 strcpy(lpbstr, lpKey);
403 else break; /* back off and whine */
408 *lpbstr++ = *lpstr++;
412 if( lpstr - str == strlen(str) )
414 strncpy(str, lpBuffer, length);
420 TRACE("-- return %s\n", str);
423 HeapFree( GetProcessHeap(), 0, lpBuffer);
425 /* Return str length in the LOWORD
426 * and 1 in HIWORD if subst was successful.
428 return (DWORD)MAKELONG(strlen(str), length);
431 /*************************************************************************
434 * 32-bit version of the system-wide WH_SHELL hook.
436 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
438 TRACE("%i, %x, %08lx\n", code, wParam, lParam );
444 case HSHELL_WINDOWCREATED:
445 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
447 case HSHELL_WINDOWDESTROYED:
448 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
450 case HSHELL_ACTIVATESHELLWINDOW:
451 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
455 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
458 /*************************************************************************
459 * ShellHookProc [SHELL.103]
460 * System-wide WH_SHELL hook.
462 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
464 return SHELL_HookProc( code, wParam, lParam );
467 /*************************************************************************
468 * RegisterShellHook [SHELL.102]
470 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
472 TRACE("%04x [%u]\n", hWnd, uAction );
476 case 2: /* register hWnd as a shell window */
479 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
480 GetModuleHandleA("shell32.dll"), 0 );
483 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
484 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
485 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
488 WARN("-- unable to install ShellHookProc()!\n");
492 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
496 WARN("-- unknown code %i\n", uAction );
497 SHELL_hWnd = 0; /* just in case */
503 /***********************************************************************
504 * DriveType (SHELL.262)
506 UINT16 WINAPI DriveType16( UINT16 drive )
509 char path[] = "A:\\";
511 ret = GetDriveTypeA(path);
512 switch(ret) /* some values are not supported in Win16 */
517 case DRIVE_NO_ROOT_DIR:
525 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
526 * some programs. Do not remove those cases. -MM
528 static inline void fix_win16_hkey( HKEY *hkey )
530 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
533 /******************************************************************************
534 * RegOpenKey [SHELL.1]
536 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
538 fix_win16_hkey( &hkey );
539 return RegOpenKeyA( hkey, name, retkey );
542 /******************************************************************************
543 * RegCreateKey [SHELL.2]
545 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
547 fix_win16_hkey( &hkey );
548 return RegCreateKeyA( hkey, name, retkey );
551 /******************************************************************************
552 * RegCloseKey [SHELL.3]
554 DWORD WINAPI RegCloseKey16( HKEY hkey )
556 fix_win16_hkey( &hkey );
557 return RegCloseKey( hkey );
560 /******************************************************************************
561 * RegDeleteKey [SHELL.4]
563 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
565 fix_win16_hkey( &hkey );
566 return RegDeleteKeyA( hkey, name );
569 /******************************************************************************
570 * RegSetValue [SHELL.5]
572 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
574 fix_win16_hkey( &hkey );
575 return RegSetValueA( hkey, name, type, data, count );
578 /******************************************************************************
579 * RegQueryValue [SHELL.6]
582 * Is this HACK still applicable?
585 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
586 * mask out the high 16 bit. This (not so much incidently) hopefully fixes
589 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
592 fix_win16_hkey( &hkey );
593 if (count) *count &= 0xffff;
594 return RegQueryValueA( hkey, name, data, count );
597 /******************************************************************************
598 * RegEnumKey [SHELL.7]
600 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
602 fix_win16_hkey( &hkey );
603 return RegEnumKeyA( hkey, index, name, name_len );
606 /*************************************************************************
607 * SHELL_Execute16 [Internal]
609 static UINT SHELL_Execute16(char *lpCmd, LPSHELLEXECUTEINFOA sei, BOOL shWait)
611 UINT ret = WinExec16(lpCmd, sei->nShow);
612 sei->hInstApp = HINSTANCE_32(ret);
616 /*************************************************************************
617 * ShellExecute [SHELL.20]
619 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
620 LPCSTR lpFile, LPCSTR lpParameters,
621 LPCSTR lpDirectory, INT16 iShowCmd )
623 SHELLEXECUTEINFOA sei;
626 sei.cbSize = sizeof(sei);
628 sei.hwnd = HWND_32(hWnd);
629 sei.lpVerb = lpOperation;
631 sei.lpParameters = lpParameters;
632 sei.lpDirectory = lpDirectory;
633 sei.nShow = iShowCmd;
638 sei.hProcess = hProcess;
640 ShellExecuteExA32 (&sei, SHELL_Execute16);
641 return HINSTANCE_16(sei.hInstApp);