2 * Shell Library Functions
24 #include "wine/winbase16.h"
25 #include "wine/winuser16.h"
26 #include "shell32_main.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(shell);
31 DECLARE_DEBUG_CHANNEL(exec);
34 typedef struct { /* structure for dropped files */
37 BOOL16 fInNonClientArea;
38 /* memory block with filenames follows */
39 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
41 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
42 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
43 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
45 static HWND16 SHELL_hWnd = 0;
46 static HHOOK SHELL_hHook = 0;
47 static UINT16 uMsgWndCreated = 0;
48 static UINT16 uMsgWndDestroyed = 0;
49 static UINT16 uMsgShellActivate = 0;
50 HINSTANCE16 SHELL_hInstance = 0;
51 HINSTANCE SHELL_hInstance32;
52 static int SHELL_Attach = 0;
54 /***********************************************************************
55 * DllEntryPoint [SHELL.101]
57 * Initialization code for shell.dll. Automatically loads the
58 * 32-bit shell32.dll to allow thunking up to 32-bit code.
62 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
63 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
65 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
66 Reason, hInst, ds, HeapSize, res1, res2);
70 case DLL_PROCESS_ATTACH:
71 if (SHELL_Attach++) break;
72 SHELL_hInstance = hInst;
73 if(!SHELL_hInstance32)
75 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
77 ERR("Could not load sibling shell32.dll\n");
83 case DLL_PROCESS_DETACH:
88 FreeLibrary(SHELL_hInstance32);
95 /*************************************************************************
96 * DragAcceptFiles [SHELL.9]
98 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
100 DragAcceptFiles(hWnd, b);
103 /*************************************************************************
104 * DragQueryFile [SHELL.11]
106 UINT16 WINAPI DragQueryFile16(
114 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
116 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
118 if(!lpDropFileStruct) goto end;
120 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
121 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
125 while (*lpDrop++); /* skip filename */
128 i = (wFile == 0xFFFFFFFF) ? i : 0;
135 if (!lpszFile ) goto end; /* needed buffer size */
136 i = (wLength > i) ? i : wLength;
137 lstrcpynA (lpszFile, lpDrop, i);
139 GlobalUnlock16(hDrop);
143 /*************************************************************************
144 * DragFinish [SHELL.12]
146 void WINAPI DragFinish16(HDROP16 h)
149 GlobalFree16((HGLOBAL16)h);
153 /*************************************************************************
154 * DragQueryPoint [SHELL.13]
156 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
158 LPDROPFILESTRUCT16 lpDropFileStruct;
161 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
163 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
164 bRet = lpDropFileStruct->fInNonClientArea;
166 GlobalUnlock16(hDrop);
170 /*************************************************************************
171 * FindExecutable (SHELL.21)
173 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
175 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
179 /*************************************************************************
180 * AboutDlgProc (SHELL.33)
182 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
184 { return AboutDlgProc( hWnd, msg, wParam, lParam );
188 /*************************************************************************
189 * ShellAbout (SHELL.22)
191 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
193 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
196 /*************************************************************************
197 * InternalExtractIcon [SHELL.39]
199 * This abortion is called directly by Progman
201 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
202 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
205 HICON16 *RetPtr = NULL;
209 TRACE("(%04x,file %s,start %d,extract %d\n",
210 hInstance, lpszExeFileName, nIconIndex, n);
215 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ|OF_EXIST );
217 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
218 RetPtr = (HICON16*)GlobalLock16(hRet);
220 if (hFile == HFILE_ERROR)
221 { /* not found - load from builtin module if available */
222 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
224 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
225 hInst = LoadLibraryA(lpszExeFileName);
229 for (i=nIconIndex; i < nIconIndex + n; i++)
230 RetPtr[i-nIconIndex] =
231 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
235 GlobalFree16( hRet );
239 if (nIconIndex == (UINT16)-1) /* get number of icons */
241 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
247 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
248 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
249 GetSystemMetrics(SM_CXICON),
250 GetSystemMetrics(SM_CYICON),
255 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
259 GlobalFree16( hRet );
262 HeapFree( GetProcessHeap(), 0, icons );
267 /*************************************************************************
268 * ExtractIcon (SHELL.34)
270 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
273 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
276 /*************************************************************************
277 * ExtractIconEx (SHELL.40)
279 HICON16 WINAPI ExtractIconEx16(
280 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
281 HICON16 *phiconSmall, UINT16 nIcons
283 HICON *ilarge,*ismall;
288 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
292 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
295 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
297 for (i=0;i<nIcons;i++)
298 phiconLarge[i]=ilarge[i];
299 HeapFree(GetProcessHeap(),0,ilarge);
302 for (i=0;i<nIcons;i++)
303 phiconSmall[i]=ismall[i];
304 HeapFree(GetProcessHeap(),0,ismall);
309 /*************************************************************************
310 * ExtractAssociatedIcon [SHELL.36]
312 * Return icon for given file (either from file itself or from associated
313 * executable) and patch parameters if needed.
315 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
322 lpiIcon = &wDummyIcon;
324 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
327 { if( hIcon == 1 ) /* no icons found in given file */
328 { char tempPath[0x80];
329 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
331 if( uRet > 32 && tempPath[0] )
332 { strcpy(lpIconPath,tempPath);
333 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
341 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
343 *lpiIcon = 6; /* generic icon - found nothing */
345 GetModuleFileName16(hInst, lpIconPath, 0x80);
346 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
351 /*************************************************************************
352 * ExtractAssociatedIconA (SHELL32.@)
354 * Return icon for given file (either from file itself or from associated
355 * executable) and patch parameters if needed.
357 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
359 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
362 /*************************************************************************
363 * ExtractAssociatedIconExW (SHELL32.@)
365 * Return icon for given file (either from file itself or from associated
366 * executable) and patch parameters if needed.
368 HICON WINAPI ExtractAssociatedIconExW(DWORD d1, DWORD d2, DWORD d3, DWORD d4)
370 FIXME("(%lx %lx %lx %lx): stub\n", d1, d2, d3, d4);
374 /*************************************************************************
375 * FindEnvironmentString [SHELL.38]
377 * Returns a pointer into the DOS environment... Ugh.
379 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
385 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
386 { if( strncasecmp(lpEnv, entry, l) )
389 return (lpEnv + l); /* empty entry */
390 else if ( *(lpEnv+l)== '=' )
391 return (lpEnv + l + 1);
396 /**********************************************************************/
398 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
400 LPSTR lpEnv,lpString;
403 spEnv = GetDOSEnvironment16();
405 lpEnv = MapSL(spEnv);
406 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
408 if( lpString ) /* offset should be small enough */
409 return spEnv + (lpString - lpEnv);
413 /*************************************************************************
414 * DoEnvironmentSubst [SHELL.37]
416 * Replace %KEYWORD% in the str with the value of variable KEYWORD
417 * from "DOS" environment.
419 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
421 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
422 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
424 LPSTR lpbstr = lpBuffer;
428 TRACE("accept %s\n", str);
430 while( *lpstr && lpbstr - lpBuffer < length )
436 do { lpend++; } while( *lpend && *lpend != '%' );
437 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
441 lpKey = SHELL_FindString(lpEnv, lpstr+1);
442 if( lpKey ) /* found key value */
444 int l = strlen(lpKey);
446 if( l > length - (lpbstr - lpBuffer) - 1 )
448 WARN("-- Env subst aborted - string too short\n");
452 strcpy(lpbstr, lpKey);
459 else break; /* back off and whine */
464 *lpbstr++ = *lpstr++;
468 if( lpstr - str == strlen(str) )
470 strncpy(str, lpBuffer, length);
476 TRACE("-- return %s\n", str);
479 HeapFree( GetProcessHeap(), 0, lpBuffer);
481 /* Return str length in the LOWORD
482 * and 1 in HIWORD if subst was successful.
484 return (DWORD)MAKELONG(strlen(str), length);
487 /*************************************************************************
488 * ShellHookProc [SHELL.103]
489 * System-wide WH_SHELL hook.
491 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
493 TRACE("%i, %04x, %08x\n", code, wParam,
495 if( SHELL_hHook && SHELL_hWnd )
500 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
501 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
502 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
504 PostMessageA( SHELL_hWnd, uMsg, wParam, 0 );
506 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
509 /*************************************************************************
510 * RegisterShellHook [SHELL.102]
512 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
514 TRACE("%04x [%u]\n", hWnd, uAction );
518 case 2: /* register hWnd as a shell window */
521 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
522 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
523 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
526 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
527 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
528 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
531 WARN("-- unable to install ShellHookProc()!\n");
535 return ((SHELL_hWnd = hWnd) != 0);
539 WARN("-- unknown code %i\n", uAction );
540 SHELL_hWnd = 0; /* just in case */
546 /***********************************************************************
547 * DriveType (SHELL.262)
549 UINT16 WINAPI DriveType16( UINT16 drive )
552 char path[] = "A:\\";
554 ret = GetDriveTypeA(path);
555 switch(ret) /* some values are not supported in Win16 */
560 case DRIVE_NO_ROOT_DIR: