2 * Shell Library Functions
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "wine/shell16.h"
20 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(shell);
25 DECLARE_DEBUG_CHANNEL(exec);
28 typedef struct { /* structure for dropped files */
31 BOOL16 fInNonClientArea;
32 /* memory block with filenames follows */
33 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
35 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
36 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
37 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
39 static HWND16 SHELL_hWnd = 0;
40 static HHOOK SHELL_hHook = 0;
41 static UINT16 uMsgWndCreated = 0;
42 static UINT16 uMsgWndDestroyed = 0;
43 static UINT16 uMsgShellActivate = 0;
44 HINSTANCE16 SHELL_hInstance = 0;
45 HINSTANCE SHELL_hInstance32;
46 static int SHELL_Attach = 0;
48 /***********************************************************************
49 * SHELL_DllEntryPoint [SHELL.entry]
51 * Initialization code for shell.dll. Automatically loads the
52 * 32-bit shell32.dll to allow thunking up to 32-bit code.
56 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
57 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
59 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
60 Reason, hInst, ds, HeapSize, res1, res2);
64 case DLL_PROCESS_ATTACH:
68 ERR("shell.dll instantiated twice!\n");
70 * We should return FALSE here, but that will break
71 * most apps that use CreateProcess because we do
72 * not yet support seperate address-spaces.
77 SHELL_hInstance = hInst;
78 if(!SHELL_hInstance32)
80 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
82 ERR("Could not load sibling shell32.dll\n");
88 case DLL_PROCESS_DETACH:
93 FreeLibrary(SHELL_hInstance32);
100 /*************************************************************************
101 * DragAcceptFiles16 [SHELL.9]
103 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
105 DragAcceptFiles(hWnd, b);
108 /*************************************************************************
109 * DragQueryFile16 [SHELL.11]
111 UINT16 WINAPI DragQueryFile16(
119 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
121 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
123 if(!lpDropFileStruct) goto end;
125 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
126 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
130 while (*lpDrop++); /* skip filename */
133 i = (wFile == 0xFFFFFFFF) ? i : 0;
140 if (!lpszFile ) goto end; /* needed buffer size */
141 i = (wLength > i) ? i : wLength;
142 lstrcpynA (lpszFile, lpDrop, i);
144 GlobalUnlock16(hDrop);
148 /*************************************************************************
149 * DragFinish16 [SHELL.12]
151 void WINAPI DragFinish16(HDROP16 h)
154 GlobalFree16((HGLOBAL16)h);
158 /*************************************************************************
159 * DragQueryPoint16 [SHELL.13]
161 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
163 LPDROPFILESTRUCT16 lpDropFileStruct;
166 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
168 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
169 bRet = lpDropFileStruct->fInNonClientArea;
171 GlobalUnlock16(hDrop);
175 /*************************************************************************
176 * SHELL_FindExecutable [Internal]
178 * Utility for code sharing between FindExecutable and ShellExecute
180 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
183 { char *extension = NULL; /* pointer to file extension */
184 char tmpext[5]; /* local copy to mung as we please */
185 char filetype[256]; /* registry name for this filetype */
186 LONG filetypelen=256; /* length of above */
187 char command[256]; /* command from registry */
188 LONG commandlen=256; /* This is the most DOS can handle :) */
189 char buffer[256]; /* Used to GetProfileString */
190 HINSTANCE retval=31; /* default - 'No association was found' */
191 char *tok; /* token pointer */
192 int i; /* random counter */
193 char xlpFile[256] = ""; /* result of SearchPath */
195 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
197 lpResult[0]='\0'; /* Start off with an empty return string */
199 /* trap NULL parameters on entry */
200 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
201 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
202 lpFile, lpOperation, lpResult);
203 return 2; /* File not found. Close enough, I guess. */
206 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
207 { TRACE("SearchPathA returned non-zero\n");
211 /* First thing we need is the file's extension */
212 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
213 /* File->Run in progman uses */
215 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
217 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
218 { WARN("Returning 31 - No association\n");
219 return 31; /* no association */
222 /* Make local copy & lowercase it for reg & 'programs=' lookup */
223 lstrcpynA( tmpext, extension, 5 );
224 CharLowerA( tmpext );
225 TRACE("%s file\n", tmpext);
227 /* Three places to check: */
228 /* 1. win.ini, [windows], programs (NB no leading '.') */
229 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
230 /* 3. win.ini, [extensions], extension (NB no leading '.' */
231 /* All I know of the order is that registry is checked before */
232 /* extensions; however, it'd make sense to check the programs */
233 /* section first, so that's what happens here. */
235 /* See if it's a program - if GetProfileString fails, we skip this
236 * section. Actually, if GetProfileString fails, we've probably
237 * got a lot more to worry about than running a program... */
238 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
239 buffer, sizeof(buffer)) > 0 )
240 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
242 tok = strtok(buffer, " \t"); /* ? */
245 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
247 strcpy(lpResult, xlpFile);
248 /* Need to perhaps check that the file has a path
250 TRACE("found %s\n", lpResult);
253 /* Greater than 32 to indicate success FIXME According to the
254 * docs, I should be returning a handle for the
255 * executable. Does this mean I'm supposed to open the
256 * executable file or something? More RTFM, I guess... */
258 tok=strtok(NULL, " \t");
263 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
264 &filetypelen ) == ERROR_SUCCESS )
266 filetype[filetypelen]='\0';
267 TRACE("File type: %s\n", filetype);
269 /* Looking for ...buffer\shell\lpOperation\command */
270 strcat( filetype, "\\shell\\" );
271 strcat( filetype, lpOperation );
272 strcat( filetype, "\\command" );
274 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
275 &commandlen ) == ERROR_SUCCESS )
282 /* Get the parameters needed by the application
283 from the associated ddeexec key */
284 tmp = strstr(filetype,"command");
286 strcat(filetype,"ddeexec");
288 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,¶mlen ) == ERROR_SUCCESS)
291 strcat(command,param);
292 commandlen += paramlen;
295 /* Is there a replace() function anywhere? */
296 command[commandlen]='\0';
297 strcpy( lpResult, command );
298 tok=strstr( lpResult, "%1" );
301 tok[0]='\0'; /* truncate string at the percent */
302 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
303 tok=strstr( command, "%1" );
304 if ((tok!=NULL) && (strlen(tok)>2))
306 strcat( lpResult, &tok[2] );
309 retval=33; /* FIXME see above */
312 else /* Check win.ini */
314 /* Toss the leading dot */
316 if ( GetProfileStringA( "extensions", extension, "", command,
317 sizeof(command)) > 0)
319 if (strlen(command)!=0)
321 strcpy( lpResult, command );
322 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
326 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
327 tok=strstr( command, "^" ); /* see above */
328 if ((tok != NULL) && (strlen(tok)>5))
330 strcat( lpResult, &tok[5]);
333 retval=33; /* FIXME - see above */
338 TRACE("returning %s\n", lpResult);
342 /*************************************************************************
343 * ShellExecute16 [SHELL.20]
345 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
346 LPCSTR lpFile, LPCSTR lpParameters,
347 LPCSTR lpDirectory, INT16 iShowCmd )
348 { HINSTANCE16 retval=31;
352 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
353 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
354 lpParameters ? lpParameters : "<null>",
355 lpDirectory ? lpDirectory : "<null>", iShowCmd);
357 if (lpFile==NULL) return 0; /* should not happen */
358 if (lpOperation==NULL) /* default is open */
362 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
363 SetCurrentDirectoryA( lpDirectory );
366 /* First try to execute lpFile with lpParameters directly */
368 strcat(cmd,lpParameters ? lpParameters : "");
370 retval = WinExec16( cmd, iShowCmd );
372 /* Unable to execute lpFile directly
373 Check if we can match an application to lpFile */
377 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
379 if (retval > 32) /* Found */
384 strcat(cmd,lpParameters);
386 retval = WinExec16( cmd, iShowCmd );
388 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
390 char lpstrProtocol[256];
395 lpstrRes = strchr(lpFile,':');
396 iSize = lpstrRes - lpFile;
398 /* Looking for ...protocol\shell\lpOperation\command */
399 strncpy(lpstrProtocol,lpFile,iSize);
400 lpstrProtocol[iSize]='\0';
401 strcat( lpstrProtocol, "\\shell\\" );
402 strcat( lpstrProtocol, lpOperation );
403 strcat( lpstrProtocol, "\\command" );
405 /* Remove File Protocol from lpFile */
406 /* In the case file://path/file */
407 if(!strncasecmp(lpFile,"file",iSize))
410 while(*lpFile == ':') lpFile++;
414 /* Get the application for the protocol and execute it */
415 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
416 &cmdlen ) == ERROR_SUCCESS )
420 char param[256] = "";
423 /* Get the parameters needed by the application
424 from the associated ddeexec key */
425 tmp = strstr(lpstrProtocol,"command");
427 strcat(lpstrProtocol,"ddeexec");
429 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,¶mlen ) == ERROR_SUCCESS)
436 /* Is there a replace() function anywhere? */
439 tok=strstr( cmd, "%1" );
442 tok[0]='\0'; /* truncate string at the percent */
443 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
444 tok=strstr( cmd, "%1" );
445 if ((tok!=NULL) && (strlen(tok)>2))
447 strcat( cmd, &tok[2] );
451 retval = WinExec16( cmd, iShowCmd );
454 /* Check if file specified is in the form www.??????.*** */
455 else if(!strncasecmp(lpFile,"www",3))
457 /* if so, append lpFile http:// and call ShellExecute */
458 char lpstrTmpFile[256] = "http://" ;
459 strcat(lpstrTmpFile,lpFile);
460 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
464 SetCurrentDirectoryA( old_dir );
468 /*************************************************************************
469 * FindExecutable16 (SHELL.21)
471 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
473 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
477 /*************************************************************************
478 * AboutDlgProc16 (SHELL.33)
480 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
482 { return AboutDlgProc( hWnd, msg, wParam, lParam );
486 /*************************************************************************
487 * ShellAbout16 (SHELL.22)
489 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
491 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
494 /*************************************************************************
495 * InternalExtractIcon [SHELL.39]
497 * This abortion is called directly by Progman
499 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
500 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
503 HICON16 *RetPtr = NULL;
507 TRACE("(%04x,file %s,start %d,extract %d\n",
508 hInstance, lpszExeFileName, nIconIndex, n);
513 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
515 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
516 RetPtr = (HICON16*)GlobalLock16(hRet);
518 if (hFile == HFILE_ERROR)
519 { /* not found - load from builtin module if available */
520 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
522 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
523 hInst = LoadLibraryA(lpszExeFileName);
527 for (i=nIconIndex; i < nIconIndex + n; i++)
528 RetPtr[i-nIconIndex] =
529 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
533 GlobalFree16( hRet );
537 if (nIconIndex == (UINT16)-1) /* get number of icons */
539 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
545 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
546 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
547 GetSystemMetrics(SM_CXICON),
548 GetSystemMetrics(SM_CYICON),
553 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
557 GlobalFree16( hRet );
560 HeapFree( GetProcessHeap(), 0, icons );
565 /*************************************************************************
566 * ExtractIcon16 (SHELL.34)
568 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
571 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
574 /*************************************************************************
575 * ExtractIconEx16 (SHELL.40)
577 HICON16 WINAPI ExtractIconEx16(
578 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
579 HICON16 *phiconSmall, UINT16 nIcons
581 HICON *ilarge,*ismall;
586 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
590 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
593 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
595 for (i=0;i<nIcons;i++)
596 phiconLarge[i]=ilarge[i];
597 HeapFree(GetProcessHeap(),0,ilarge);
600 for (i=0;i<nIcons;i++)
601 phiconSmall[i]=ismall[i];
602 HeapFree(GetProcessHeap(),0,ismall);
607 /*************************************************************************
608 * ExtractAssociatedIcon [SHELL.36]
610 * Return icon for given file (either from file itself or from associated
611 * executable) and patch parameters if needed.
613 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
620 lpiIcon = &wDummyIcon;
622 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
625 { if( hIcon == 1 ) /* no icons found in given file */
626 { char tempPath[0x80];
627 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
629 if( uRet > 32 && tempPath[0] )
630 { strcpy(lpIconPath,tempPath);
631 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
639 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
641 *lpiIcon = 6; /* generic icon - found nothing */
643 GetModuleFileName16(hInst, lpIconPath, 0x80);
644 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
649 /*************************************************************************
650 * ExtractAssociatedIconA
652 * Return icon for given file (either from file itself or from associated
653 * executable) and patch parameters if needed.
655 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
657 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
660 /*************************************************************************
661 * FindEnvironmentString [SHELL.38]
663 * Returns a pointer into the DOS environment... Ugh.
665 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
671 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
672 { if( strncasecmp(lpEnv, entry, l) )
675 return (lpEnv + l); /* empty entry */
676 else if ( *(lpEnv+l)== '=' )
677 return (lpEnv + l + 1);
682 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
684 LPSTR lpEnv,lpString;
687 spEnv = GetDOSEnvironment16();
689 lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
690 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
692 if( lpString ) /* offset should be small enough */
693 return spEnv + (lpString - lpEnv);
697 /*************************************************************************
698 * DoEnvironmentSubst [SHELL.37]
700 * Replace %KEYWORD% in the str with the value of variable KEYWORD
701 * from "DOS" environment.
703 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
705 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment16());
706 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
708 LPSTR lpbstr = lpBuffer;
712 TRACE("accept %s\n", str);
714 while( *lpstr && lpbstr - lpBuffer < length )
720 do { lpend++; } while( *lpend && *lpend != '%' );
721 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
725 lpKey = SHELL_FindString(lpEnv, lpstr+1);
726 if( lpKey ) /* found key value */
728 int l = strlen(lpKey);
730 if( l > length - (lpbstr - lpBuffer) - 1 )
732 WARN("-- Env subst aborted - string too short\n");
736 strcpy(lpbstr, lpKey);
743 else break; /* back off and whine */
748 *lpbstr++ = *lpstr++;
752 if( lpstr - str == strlen(str) )
754 strncpy(str, lpBuffer, length);
760 TRACE("-- return %s\n", str);
763 HeapFree( GetProcessHeap(), 0, lpBuffer);
765 /* Return str length in the LOWORD
766 * and 1 in HIWORD if subst was successful.
768 return (DWORD)MAKELONG(strlen(str), length);
771 /*************************************************************************
772 * ShellHookProc [SHELL.103]
773 * System-wide WH_SHELL hook.
775 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
777 TRACE("%i, %04x, %08x\n", code, wParam,
779 if( SHELL_hHook && SHELL_hWnd )
784 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
785 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
786 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
788 PostMessage16( SHELL_hWnd, uMsg, wParam, 0 );
790 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
793 /*************************************************************************
794 * RegisterShellHook [SHELL.102]
796 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
798 TRACE("%04x [%u]\n", hWnd, uAction );
802 case 2: /* register hWnd as a shell window */
805 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
806 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (SEGPTR)103 );
807 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
810 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
811 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
812 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
815 WARN("-- unable to install ShellHookProc()!\n");
819 return ((SHELL_hWnd = hWnd) != 0);
823 WARN("-- unknown code %i\n", uAction );
824 SHELL_hWnd = 0; /* just in case */