2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
41 #include "wine/winbase16.h"
42 #include "wine/winuser16.h"
43 #include "shell32_main.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(shell);
48 WINE_DECLARE_DEBUG_CHANNEL(exec);
51 typedef struct { /* structure for dropped files */
54 BOOL16 fInNonClientArea;
55 /* memory block with filenames follows */
56 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
58 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
59 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
60 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
62 static HWND16 SHELL_hWnd = 0;
63 static HHOOK SHELL_hHook = 0;
64 static UINT16 uMsgWndCreated = 0;
65 static UINT16 uMsgWndDestroyed = 0;
66 static UINT16 uMsgShellActivate = 0;
67 HINSTANCE16 SHELL_hInstance = 0;
68 HINSTANCE SHELL_hInstance32;
69 static int SHELL_Attach = 0;
71 /***********************************************************************
72 * DllEntryPoint [SHELL.101]
74 * Initialization code for shell.dll. Automatically loads the
75 * 32-bit shell32.dll to allow thunking up to 32-bit code.
79 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
80 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
82 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
83 Reason, hInst, ds, HeapSize, res1, res2);
87 case DLL_PROCESS_ATTACH:
88 if (SHELL_Attach++) break;
89 SHELL_hInstance = hInst;
90 if(!SHELL_hInstance32)
92 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
94 ERR("Could not load sibling shell32.dll\n");
100 case DLL_PROCESS_DETACH:
104 if(SHELL_hInstance32)
105 FreeLibrary(SHELL_hInstance32);
112 /*************************************************************************
113 * DragAcceptFiles [SHELL.9]
115 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
117 DragAcceptFiles(HWND_32(hWnd), b);
120 /*************************************************************************
121 * DragQueryFile [SHELL.11]
123 UINT16 WINAPI DragQueryFile16(
131 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
133 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
135 if(!lpDropFileStruct) goto end;
137 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
138 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
142 while (*lpDrop++); /* skip filename */
145 i = (wFile == 0xFFFFFFFF) ? i : 0;
152 if (!lpszFile ) goto end; /* needed buffer size */
153 i = (wLength > i) ? i : wLength;
154 lstrcpynA (lpszFile, lpDrop, i);
156 GlobalUnlock16(hDrop);
160 /*************************************************************************
161 * DragFinish [SHELL.12]
163 void WINAPI DragFinish16(HDROP16 h)
166 GlobalFree16((HGLOBAL16)h);
170 /*************************************************************************
171 * DragQueryPoint [SHELL.13]
173 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
175 LPDROPFILESTRUCT16 lpDropFileStruct;
178 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
180 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
181 bRet = lpDropFileStruct->fInNonClientArea;
183 GlobalUnlock16(hDrop);
187 /*************************************************************************
188 * FindExecutable (SHELL.21)
190 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
192 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
196 /*************************************************************************
197 * AboutDlgProc (SHELL.33)
199 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
201 { return AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
205 /*************************************************************************
206 * ShellAbout (SHELL.22)
208 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
210 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, hIcon );
213 /*************************************************************************
214 * InternalExtractIcon [SHELL.39]
216 * This abortion is called directly by Progman
218 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
219 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
222 HICON16 *RetPtr = NULL;
226 TRACE("(%04x,file %s,start %d,extract %d\n",
227 hInstance, lpszExeFileName, nIconIndex, n);
232 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ|OF_EXIST );
234 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
235 RetPtr = (HICON16*)GlobalLock16(hRet);
237 if (hFile == HFILE_ERROR)
238 { /* not found - load from builtin module if available */
239 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
241 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
242 hInst = LoadLibraryA(lpszExeFileName);
246 for (i=nIconIndex; i < nIconIndex + n; i++)
247 RetPtr[i-nIconIndex] =
248 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
252 GlobalFree16( hRet );
256 if (nIconIndex == (UINT16)-1) /* get number of icons */
258 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
264 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
265 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
266 GetSystemMetrics(SM_CXICON),
267 GetSystemMetrics(SM_CYICON),
272 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
276 GlobalFree16( hRet );
279 HeapFree( GetProcessHeap(), 0, icons );
284 /*************************************************************************
285 * ExtractIcon (SHELL.34)
287 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
290 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
293 /*************************************************************************
294 * ExtractIconEx (SHELL.40)
296 HICON16 WINAPI ExtractIconEx16(
297 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
298 HICON16 *phiconSmall, UINT16 nIcons
300 HICON *ilarge,*ismall;
305 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
309 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
312 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
314 for (i=0;i<nIcons;i++)
315 phiconLarge[i]=ilarge[i];
316 HeapFree(GetProcessHeap(),0,ilarge);
319 for (i=0;i<nIcons;i++)
320 phiconSmall[i]=ismall[i];
321 HeapFree(GetProcessHeap(),0,ismall);
326 /*************************************************************************
327 * ExtractAssociatedIcon [SHELL.36]
329 * Return icon for given file (either from file itself or from associated
330 * executable) and patch parameters if needed.
332 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
339 lpiIcon = &wDummyIcon;
341 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
344 { if( hIcon == 1 ) /* no icons found in given file */
345 { char tempPath[0x80];
346 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
348 if( uRet > 32 && tempPath[0] )
349 { strcpy(lpIconPath,tempPath);
350 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
358 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
360 *lpiIcon = 6; /* generic icon - found nothing */
362 GetModuleFileName16(hInst, lpIconPath, 0x80);
363 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
368 /*************************************************************************
369 * FindEnvironmentString [SHELL.38]
371 * Returns a pointer into the DOS environment... Ugh.
373 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
379 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
380 { if( strncasecmp(lpEnv, entry, l) )
383 return (lpEnv + l); /* empty entry */
384 else if ( *(lpEnv+l)== '=' )
385 return (lpEnv + l + 1);
390 /**********************************************************************/
392 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
394 LPSTR lpEnv,lpString;
397 spEnv = GetDOSEnvironment16();
399 lpEnv = MapSL(spEnv);
400 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
402 if( lpString ) /* offset should be small enough */
403 return spEnv + (lpString - lpEnv);
407 /*************************************************************************
408 * DoEnvironmentSubst [SHELL.37]
410 * Replace %KEYWORD% in the str with the value of variable KEYWORD
411 * from "DOS" environment.
413 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
415 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
416 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
418 LPSTR lpbstr = lpBuffer;
422 TRACE("accept %s\n", str);
424 while( *lpstr && lpbstr - lpBuffer < length )
430 do { lpend++; } while( *lpend && *lpend != '%' );
431 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
435 lpKey = SHELL_FindString(lpEnv, lpstr+1);
436 if( lpKey ) /* found key value */
438 int l = strlen(lpKey);
440 if( l > length - (lpbstr - lpBuffer) - 1 )
442 WARN("-- Env subst aborted - string too short\n");
446 strcpy(lpbstr, lpKey);
453 else break; /* back off and whine */
458 *lpbstr++ = *lpstr++;
462 if( lpstr - str == strlen(str) )
464 strncpy(str, lpBuffer, length);
470 TRACE("-- return %s\n", str);
473 HeapFree( GetProcessHeap(), 0, lpBuffer);
475 /* Return str length in the LOWORD
476 * and 1 in HIWORD if subst was successful.
478 return (DWORD)MAKELONG(strlen(str), length);
481 /*************************************************************************
482 * ShellHookProc [SHELL.103]
483 * System-wide WH_SHELL hook.
485 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
487 TRACE("%i, %04x, %08x\n", code, wParam,
489 if( SHELL_hHook && SHELL_hWnd )
494 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
495 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
496 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
498 PostMessageA( HWND_32(SHELL_hWnd), uMsg, wParam, 0 );
500 return CallNextHookEx16( SHELL_hHook, code, wParam, lParam );
503 /*************************************************************************
504 * RegisterShellHook [SHELL.102]
506 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
508 TRACE("%04x [%u]\n", hWnd, uAction );
512 case 2: /* register hWnd as a shell window */
515 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
516 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
517 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
520 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
521 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
522 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
525 WARN("-- unable to install ShellHookProc()!\n");
529 return ((SHELL_hWnd = hWnd) != 0);
533 WARN("-- unknown code %i\n", uAction );
534 SHELL_hWnd = 0; /* just in case */
540 /***********************************************************************
541 * DriveType (SHELL.262)
543 UINT16 WINAPI DriveType16( UINT16 drive )
546 char path[] = "A:\\";
548 ret = GetDriveTypeA(path);
549 switch(ret) /* some values are not supported in Win16 */
554 case DRIVE_NO_ROOT_DIR: