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"
45 #include "shell32_main.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52 typedef struct { /* structure for dropped files */
55 BOOL16 fInNonClientArea;
56 /* memory block with filenames follows */
57 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
59 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
60 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
61 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
63 static HWND SHELL_hWnd = 0;
64 static HHOOK SHELL_hHook = 0;
65 static UINT uMsgWndCreated = 0;
66 static UINT uMsgWndDestroyed = 0;
67 static UINT uMsgShellActivate = 0;
69 /***********************************************************************
70 * DllEntryPoint [SHELL.101]
72 * Initialization code for shell.dll. Automatically loads the
73 * 32-bit shell32.dll to allow thunking up to 32-bit code.
76 * Success: TRUE. Initialization completed successfully.
79 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
80 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
85 /*************************************************************************
86 * DragAcceptFiles [SHELL.9]
88 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
90 DragAcceptFiles(HWND_32(hWnd), b);
93 /*************************************************************************
94 * DragQueryFile [SHELL.11]
96 UINT16 WINAPI DragQueryFile16(
104 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
106 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
108 if(!lpDropFileStruct) goto end;
110 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
114 while (*lpDrop++); /* skip filename */
117 i = (wFile == 0xFFFF) ? i : 0;
123 if (!lpszFile ) goto end; /* needed buffer size */
124 lstrcpynA (lpszFile, lpDrop, wLength);
126 GlobalUnlock16(hDrop);
130 /*************************************************************************
131 * DragFinish [SHELL.12]
133 void WINAPI DragFinish16(HDROP16 h)
140 /*************************************************************************
141 * DragQueryPoint [SHELL.13]
143 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
145 LPDROPFILESTRUCT16 lpDropFileStruct;
148 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
150 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
151 bRet = lpDropFileStruct->fInNonClientArea;
153 GlobalUnlock16(hDrop);
157 /*************************************************************************
158 * FindExecutable (SHELL.21)
160 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
162 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
165 /*************************************************************************
166 * AboutDlgProc (SHELL.33)
168 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
170 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
174 /*************************************************************************
175 * ShellAbout (SHELL.22)
177 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
179 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
182 /*************************************************************************
183 * InternalExtractIcon [SHELL.39]
185 * This abortion is called directly by Progman
187 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
188 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
191 HICON16 *RetPtr = NULL;
193 TRACE("(%04x,file %s,start %d,extract %d\n",
194 hInstance, lpszExeFileName, nIconIndex, n);
199 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
200 RetPtr = GlobalLock16(hRet);
202 if (nIconIndex == (UINT16)-1) /* get number of icons */
204 RetPtr[0] = PrivateExtractIconsA(lpszExeFileName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
211 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
212 ret = PrivateExtractIconsA(lpszExeFileName, nIconIndex,
213 GetSystemMetrics(SM_CXICON),
214 GetSystemMetrics(SM_CYICON),
215 icons, NULL, n, LR_DEFAULTCOLOR);
216 if ((ret != 0xffffffff) && ret)
219 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
226 HeapFree(GetProcessHeap(), 0, icons);
231 /*************************************************************************
232 * ExtractIcon (SHELL.34)
234 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
237 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
240 /*************************************************************************
241 * ExtractIconEx (SHELL.40)
243 HICON16 WINAPI ExtractIconEx16(
244 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
245 HICON16 *phiconSmall, UINT16 nIcons
247 HICON *ilarge,*ismall;
252 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
256 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
259 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
261 for (i=0;i<nIcons;i++)
262 phiconLarge[i]=HICON_16(ilarge[i]);
263 HeapFree(GetProcessHeap(),0,ilarge);
266 for (i=0;i<nIcons;i++)
267 phiconSmall[i]=HICON_16(ismall[i]);
268 HeapFree(GetProcessHeap(),0,ismall);
273 /*************************************************************************
274 * ExtractAssociatedIcon [SHELL.36]
276 * Return icon for given file (either from file itself or from associated
277 * executable) and patch parameters if needed.
279 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
281 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
285 /*************************************************************************
286 * FindEnvironmentString [SHELL.38]
288 * Returns a pointer into the DOS environment... Ugh.
290 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
296 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
297 { if( strncasecmp(lpEnv, entry, l) )
300 return (lpEnv + l); /* empty entry */
301 else if ( *(lpEnv+l)== '=' )
302 return (lpEnv + l + 1);
307 /**********************************************************************/
309 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
311 LPSTR lpEnv,lpString;
314 spEnv = GetDOSEnvironment16();
316 lpEnv = MapSL(spEnv);
317 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
319 if( lpString ) /* offset should be small enough */
320 return spEnv + (lpString - lpEnv);
324 /*************************************************************************
325 * DoEnvironmentSubst [SHELL.37]
327 * Replace %KEYWORD% in the str with the value of variable KEYWORD
328 * from "DOS" environment. If it is not found the %KEYWORD% is left
329 * intact. If the buffer is too small, str is not modified.
332 * str [I] '\0' terminated string with %keyword%.
333 * [O] '\0' terminated string with %keyword% substituted.
334 * length [I] size of str.
337 * str length in the LOWORD and 1 in HIWORD if subst was successful.
339 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
341 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
344 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
349 WORD retLength = length;
353 TRACE("accept %s\n", str);
355 while( *lpstr && bufCnt <= length - 1 ) {
356 if ( *lpstr != '%' ) {
357 lpBuffer[bufCnt++] = *lpstr++;
361 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
363 envKeyLen = lpend - lpstr - 1;
364 if( *lpend != '%' || envKeyLen == 0)
365 goto err; /* "%\0" or "%%" found; back off and whine */
368 lpKey = SHELL_FindString(lpEnv, lpstr+1);
371 int l = strlen(lpKey);
373 if( bufCnt + l > length - 1 )
376 memcpy(lpBuffer + bufCnt, lpKey, l);
378 } else { /* Keyword not found; Leave the %KEYWORD% intact */
379 if( bufCnt + envKeyLen + 2 > length - 1 )
382 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
383 bufCnt += envKeyLen + 2;
389 if (!*lpstr && bufCnt <= length - 1) {
390 memcpy(str,lpBuffer, bufCnt);
392 retLength = bufCnt + 1;
398 WARN("-- Env subst aborted - string too short or invalid input\n");
399 TRACE("-- return %s\n", str);
402 HeapFree( GetProcessHeap(), 0, lpBuffer);
404 return (DWORD)MAKELONG(retLength, retStatus);
407 /*************************************************************************
410 * 32-bit version of the system-wide WH_SHELL hook.
412 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
414 TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
420 case HSHELL_WINDOWCREATED:
421 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
423 case HSHELL_WINDOWDESTROYED:
424 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
426 case HSHELL_ACTIVATESHELLWINDOW:
427 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
431 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
434 /*************************************************************************
435 * ShellHookProc [SHELL.103]
436 * System-wide WH_SHELL hook.
438 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
440 return SHELL_HookProc( code, wParam, lParam );
443 /*************************************************************************
444 * RegisterShellHook [SHELL.102]
446 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
448 TRACE("%04x [%u]\n", hWnd, uAction );
452 case 2: /* register hWnd as a shell window */
455 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
456 GetModuleHandleA("shell32.dll"), 0 );
459 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
460 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
461 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
464 WARN("-- unable to install ShellHookProc()!\n");
468 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
472 WARN("-- unknown code %i\n", uAction );
473 SHELL_hWnd = 0; /* just in case */
479 /***********************************************************************
480 * DriveType (SHELL.262)
482 UINT16 WINAPI DriveType16( UINT16 drive )
485 char path[] = "A:\\";
487 ret = GetDriveTypeA(path);
488 switch(ret) /* some values are not supported in Win16 */
493 case DRIVE_NO_ROOT_DIR:
501 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
502 * some programs. Do not remove those cases. -MM
504 static inline void fix_win16_hkey( HKEY *hkey )
506 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
509 /******************************************************************************
510 * RegOpenKey [SHELL.1]
512 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
514 fix_win16_hkey( &hkey );
515 return RegOpenKeyA( hkey, name, retkey );
518 /******************************************************************************
519 * RegCreateKey [SHELL.2]
521 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
523 fix_win16_hkey( &hkey );
524 return RegCreateKeyA( hkey, name, retkey );
527 /******************************************************************************
528 * RegCloseKey [SHELL.3]
530 DWORD WINAPI RegCloseKey16( HKEY hkey )
532 fix_win16_hkey( &hkey );
533 return RegCloseKey( hkey );
536 /******************************************************************************
537 * RegDeleteKey [SHELL.4]
539 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
541 fix_win16_hkey( &hkey );
542 return RegDeleteKeyA( hkey, name );
545 /******************************************************************************
546 * RegSetValue [SHELL.5]
548 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
550 fix_win16_hkey( &hkey );
551 return RegSetValueA( hkey, name, type, data, count );
554 /******************************************************************************
555 * RegQueryValue [SHELL.6]
558 * Is this HACK still applicable?
561 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
562 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
565 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
568 fix_win16_hkey( &hkey );
569 if (count) *count &= 0xffff;
570 return RegQueryValueA( hkey, name, data, (LONG*) count );
573 /******************************************************************************
574 * RegEnumKey [SHELL.7]
576 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
578 fix_win16_hkey( &hkey );
579 return RegEnumKeyA( hkey, index, name, name_len );
582 /*************************************************************************
583 * SHELL_Execute16 [Internal]
585 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
586 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
590 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
591 ret = WinExec16(sCmd, (UINT16)psei->nShow);
592 psei_out->hInstApp = HINSTANCE_32(ret);
596 /*************************************************************************
597 * ShellExecute [SHELL.20]
599 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
600 LPCSTR lpFile, LPCSTR lpParameters,
601 LPCSTR lpDirectory, INT16 iShowCmd )
603 SHELLEXECUTEINFOW seiW;
604 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
607 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
608 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
609 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
610 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
612 seiW.cbSize = sizeof(seiW);
614 seiW.hwnd = HWND_32(hWnd);
615 seiW.nShow = iShowCmd;
620 seiW.hProcess = hProcess;
622 SHELL_execute( &seiW, SHELL_Execute16 );
629 return HINSTANCE_16(seiW.hInstApp);