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;
194 TRACE("(%04x,file %s,start %d,extract %d\n",
195 hInstance, lpszExeFileName, nIconIndex, n);
200 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
201 RetPtr = (HICON16*)GlobalLock16(hRet);
203 if (nIconIndex == (UINT16)-1) /* get number of icons */
205 RetPtr[0] = PrivateExtractIconsA(ofs.szPathName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
212 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
213 ret = PrivateExtractIconsA(ofs.szPathName, nIconIndex,
214 GetSystemMetrics(SM_CXICON),
215 GetSystemMetrics(SM_CYICON),
216 icons, NULL, n, LR_DEFAULTCOLOR);
217 if ((ret != 0xffffffff) && ret)
220 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
227 HeapFree(GetProcessHeap(), 0, icons);
232 /*************************************************************************
233 * ExtractIcon (SHELL.34)
235 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
238 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
241 /*************************************************************************
242 * ExtractIconEx (SHELL.40)
244 HICON16 WINAPI ExtractIconEx16(
245 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
246 HICON16 *phiconSmall, UINT16 nIcons
248 HICON *ilarge,*ismall;
253 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
257 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
260 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
262 for (i=0;i<nIcons;i++)
263 phiconLarge[i]=HICON_16(ilarge[i]);
264 HeapFree(GetProcessHeap(),0,ilarge);
267 for (i=0;i<nIcons;i++)
268 phiconSmall[i]=HICON_16(ismall[i]);
269 HeapFree(GetProcessHeap(),0,ismall);
274 /*************************************************************************
275 * ExtractAssociatedIcon [SHELL.36]
277 * Return icon for given file (either from file itself or from associated
278 * executable) and patch parameters if needed.
280 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
282 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
286 /*************************************************************************
287 * FindEnvironmentString [SHELL.38]
289 * Returns a pointer into the DOS environment... Ugh.
291 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
297 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
298 { if( strncasecmp(lpEnv, entry, l) )
301 return (lpEnv + l); /* empty entry */
302 else if ( *(lpEnv+l)== '=' )
303 return (lpEnv + l + 1);
308 /**********************************************************************/
310 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
312 LPSTR lpEnv,lpString;
315 spEnv = GetDOSEnvironment16();
317 lpEnv = MapSL(spEnv);
318 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
320 if( lpString ) /* offset should be small enough */
321 return spEnv + (lpString - lpEnv);
325 /*************************************************************************
326 * DoEnvironmentSubst [SHELL.37]
328 * Replace %KEYWORD% in the str with the value of variable KEYWORD
329 * from "DOS" environment. If it is not found the %KEYWORD% is left
330 * intact. If the buffer is too small, str is not modified.
333 * str [I] '\0' terminated string with %keyword%.
334 * [O] '\0' terminated string with %keyword% substituted.
335 * length [I] size of str.
338 * str length in the LOWORD and 1 in HIWORD if subst was successful.
340 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
342 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
345 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
350 WORD retLength = length;
354 TRACE("accept %s\n", str);
356 while( *lpstr && bufCnt <= length - 1 ) {
357 if ( *lpstr != '%' ) {
358 lpBuffer[bufCnt++] = *lpstr++;
362 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
364 envKeyLen = lpend - lpstr - 1;
365 if( *lpend != '%' || envKeyLen == 0)
366 goto err; /* "%\0" or "%%" found; back off and whine */
369 lpKey = SHELL_FindString(lpEnv, lpstr+1);
372 int l = strlen(lpKey);
374 if( bufCnt + l > length - 1 )
377 memcpy(lpBuffer + bufCnt, lpKey, l);
379 } else { /* Keyword not found; Leave the %KEYWORD% intact */
380 if( bufCnt + envKeyLen + 2 > length - 1 )
383 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
384 bufCnt += envKeyLen + 2;
390 if (!*lpstr && bufCnt <= length - 1) {
391 memcpy(str,lpBuffer, bufCnt);
393 retLength = bufCnt + 1;
399 WARN("-- Env subst aborted - string too short or invalid input\n");
400 TRACE("-- return %s\n", str);
403 HeapFree( GetProcessHeap(), 0, lpBuffer);
405 return (DWORD)MAKELONG(retLength, retStatus);
408 /*************************************************************************
411 * 32-bit version of the system-wide WH_SHELL hook.
413 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
415 TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
421 case HSHELL_WINDOWCREATED:
422 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
424 case HSHELL_WINDOWDESTROYED:
425 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
427 case HSHELL_ACTIVATESHELLWINDOW:
428 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
432 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
435 /*************************************************************************
436 * ShellHookProc [SHELL.103]
437 * System-wide WH_SHELL hook.
439 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
441 return SHELL_HookProc( code, wParam, lParam );
444 /*************************************************************************
445 * RegisterShellHook [SHELL.102]
447 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
449 TRACE("%04x [%u]\n", hWnd, uAction );
453 case 2: /* register hWnd as a shell window */
456 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
457 GetModuleHandleA("shell32.dll"), 0 );
460 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
461 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
462 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
465 WARN("-- unable to install ShellHookProc()!\n");
469 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
473 WARN("-- unknown code %i\n", uAction );
474 SHELL_hWnd = 0; /* just in case */
480 /***********************************************************************
481 * DriveType (SHELL.262)
483 UINT16 WINAPI DriveType16( UINT16 drive )
486 char path[] = "A:\\";
488 ret = GetDriveTypeA(path);
489 switch(ret) /* some values are not supported in Win16 */
494 case DRIVE_NO_ROOT_DIR:
502 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
503 * some programs. Do not remove those cases. -MM
505 static inline void fix_win16_hkey( HKEY *hkey )
507 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
510 /******************************************************************************
511 * RegOpenKey [SHELL.1]
513 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
515 fix_win16_hkey( &hkey );
516 return RegOpenKeyA( hkey, name, retkey );
519 /******************************************************************************
520 * RegCreateKey [SHELL.2]
522 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
524 fix_win16_hkey( &hkey );
525 return RegCreateKeyA( hkey, name, retkey );
528 /******************************************************************************
529 * RegCloseKey [SHELL.3]
531 DWORD WINAPI RegCloseKey16( HKEY hkey )
533 fix_win16_hkey( &hkey );
534 return RegCloseKey( hkey );
537 /******************************************************************************
538 * RegDeleteKey [SHELL.4]
540 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
542 fix_win16_hkey( &hkey );
543 return RegDeleteKeyA( hkey, name );
546 /******************************************************************************
547 * RegSetValue [SHELL.5]
549 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
551 fix_win16_hkey( &hkey );
552 return RegSetValueA( hkey, name, type, data, count );
555 /******************************************************************************
556 * RegQueryValue [SHELL.6]
559 * Is this HACK still applicable?
562 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
563 * mask out the high 16 bit. This (not so much incidently) hopefully fixes
566 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
569 fix_win16_hkey( &hkey );
570 if (count) *count &= 0xffff;
571 return RegQueryValueA( hkey, name, data, (LONG*) count );
574 /******************************************************************************
575 * RegEnumKey [SHELL.7]
577 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
579 fix_win16_hkey( &hkey );
580 return RegEnumKeyA( hkey, index, name, name_len );
583 /*************************************************************************
584 * SHELL_Execute16 [Internal]
586 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
587 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
591 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
592 ret = WinExec16(sCmd, (UINT16)psei->nShow);
593 psei_out->hInstApp = HINSTANCE_32(ret);
597 /*************************************************************************
598 * ShellExecute [SHELL.20]
600 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
601 LPCSTR lpFile, LPCSTR lpParameters,
602 LPCSTR lpDirectory, INT16 iShowCmd )
604 SHELLEXECUTEINFOW seiW;
605 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
608 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
609 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
610 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
611 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
613 seiW.cbSize = sizeof(seiW);
615 seiW.hwnd = HWND_32(hWnd);
616 seiW.nShow = iShowCmd;
621 seiW.hProcess = hProcess;
623 SHELL_execute( &seiW, SHELL_Execute16 );
630 return HINSTANCE_16(seiW.hInstApp);