2 * Shell Library Functions
22 #include "wine/winbase16.h"
23 #include "wine/winuser16.h"
24 #include "shell32_main.h"
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(shell);
29 DECLARE_DEBUG_CHANNEL(exec);
32 typedef struct { /* structure for dropped files */
35 BOOL16 fInNonClientArea;
36 /* memory block with filenames follows */
37 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
39 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
40 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
41 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
43 static HWND16 SHELL_hWnd = 0;
44 static HHOOK SHELL_hHook = 0;
45 static UINT16 uMsgWndCreated = 0;
46 static UINT16 uMsgWndDestroyed = 0;
47 static UINT16 uMsgShellActivate = 0;
48 HINSTANCE16 SHELL_hInstance = 0;
49 HINSTANCE SHELL_hInstance32;
50 static int SHELL_Attach = 0;
52 /***********************************************************************
53 * DllEntryPoint [SHELL.101]
55 * Initialization code for shell.dll. Automatically loads the
56 * 32-bit shell32.dll to allow thunking up to 32-bit code.
60 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
61 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
63 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
64 Reason, hInst, ds, HeapSize, res1, res2);
68 case DLL_PROCESS_ATTACH:
69 if (SHELL_Attach++) break;
70 SHELL_hInstance = hInst;
71 if(!SHELL_hInstance32)
73 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
75 ERR("Could not load sibling shell32.dll\n");
81 case DLL_PROCESS_DETACH:
86 FreeLibrary(SHELL_hInstance32);
93 /*************************************************************************
94 * DragAcceptFiles [SHELL.9]
96 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
98 DragAcceptFiles(hWnd, b);
101 /*************************************************************************
102 * DragQueryFile [SHELL.11]
104 UINT16 WINAPI DragQueryFile16(
112 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
114 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
116 if(!lpDropFileStruct) goto end;
118 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
119 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
123 while (*lpDrop++); /* skip filename */
126 i = (wFile == 0xFFFFFFFF) ? i : 0;
133 if (!lpszFile ) goto end; /* needed buffer size */
134 i = (wLength > i) ? i : wLength;
135 lstrcpynA (lpszFile, lpDrop, i);
137 GlobalUnlock16(hDrop);
141 /*************************************************************************
142 * DragFinish [SHELL.12]
144 void WINAPI DragFinish16(HDROP16 h)
147 GlobalFree16((HGLOBAL16)h);
151 /*************************************************************************
152 * DragQueryPoint [SHELL.13]
154 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
156 LPDROPFILESTRUCT16 lpDropFileStruct;
159 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
161 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
162 bRet = lpDropFileStruct->fInNonClientArea;
164 GlobalUnlock16(hDrop);
168 /*************************************************************************
169 * SHELL_FindExecutable [Internal]
171 * Utility for code sharing between FindExecutable and ShellExecute
173 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
176 { char *extension = NULL; /* pointer to file extension */
177 char tmpext[5]; /* local copy to mung as we please */
178 char filetype[256]; /* registry name for this filetype */
179 LONG filetypelen=256; /* length of above */
180 char command[256]; /* command from registry */
181 LONG commandlen=256; /* This is the most DOS can handle :) */
182 char buffer[256]; /* Used to GetProfileString */
183 HINSTANCE retval=31; /* default - 'No association was found' */
184 char *tok; /* token pointer */
185 int i; /* random counter */
186 char xlpFile[256] = ""; /* result of SearchPath */
188 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
190 lpResult[0]='\0'; /* Start off with an empty return string */
192 /* trap NULL parameters on entry */
193 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
194 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
195 lpFile, lpOperation, lpResult);
196 return 2; /* File not found. Close enough, I guess. */
199 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
200 { TRACE("SearchPathA returned non-zero\n");
204 /* First thing we need is the file's extension */
205 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
206 /* File->Run in progman uses */
208 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
210 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
211 { WARN("Returning 31 - No association\n");
212 return 31; /* no association */
215 /* Make local copy & lowercase it for reg & 'programs=' lookup */
216 lstrcpynA( tmpext, extension, 5 );
217 CharLowerA( tmpext );
218 TRACE("%s file\n", tmpext);
220 /* Three places to check: */
221 /* 1. win.ini, [windows], programs (NB no leading '.') */
222 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
223 /* 3. win.ini, [extensions], extension (NB no leading '.' */
224 /* All I know of the order is that registry is checked before */
225 /* extensions; however, it'd make sense to check the programs */
226 /* section first, so that's what happens here. */
228 /* See if it's a program - if GetProfileString fails, we skip this
229 * section. Actually, if GetProfileString fails, we've probably
230 * got a lot more to worry about than running a program... */
231 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
232 buffer, sizeof(buffer)) > 0 )
233 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
235 tok = strtok(buffer, " \t"); /* ? */
238 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
240 strcpy(lpResult, xlpFile);
241 /* Need to perhaps check that the file has a path
243 TRACE("found %s\n", lpResult);
246 /* Greater than 32 to indicate success FIXME According to the
247 * docs, I should be returning a handle for the
248 * executable. Does this mean I'm supposed to open the
249 * executable file or something? More RTFM, I guess... */
251 tok=strtok(NULL, " \t");
256 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
257 &filetypelen ) == ERROR_SUCCESS )
259 filetype[filetypelen]='\0';
260 TRACE("File type: %s\n", filetype);
262 /* Looking for ...buffer\shell\lpOperation\command */
263 strcat( filetype, "\\shell\\" );
264 strcat( filetype, lpOperation );
265 strcat( filetype, "\\command" );
267 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
268 &commandlen ) == ERROR_SUCCESS )
275 /* Get the parameters needed by the application
276 from the associated ddeexec key */
277 tmp = strstr(filetype,"command");
279 strcat(filetype,"ddeexec");
281 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,¶mlen ) == ERROR_SUCCESS)
284 strcat(command,param);
285 commandlen += paramlen;
288 /* Is there a replace() function anywhere? */
289 command[commandlen]='\0';
290 strcpy( lpResult, command );
291 tok=strstr( lpResult, "%1" );
294 tok[0]='\0'; /* truncate string at the percent */
295 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
296 tok=strstr( command, "%1" );
297 if ((tok!=NULL) && (strlen(tok)>2))
299 strcat( lpResult, &tok[2] );
302 retval=33; /* FIXME see above */
305 else /* Check win.ini */
307 /* Toss the leading dot */
309 if ( GetProfileStringA( "extensions", extension, "", command,
310 sizeof(command)) > 0)
312 if (strlen(command)!=0)
314 strcpy( lpResult, command );
315 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
319 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
320 tok=strstr( command, "^" ); /* see above */
321 if ((tok != NULL) && (strlen(tok)>5))
323 strcat( lpResult, &tok[5]);
326 retval=33; /* FIXME - see above */
331 TRACE("returning %s\n", lpResult);
335 /*************************************************************************
336 * ShellExecute [SHELL.20]
338 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
339 LPCSTR lpFile, LPCSTR lpParameters,
340 LPCSTR lpDirectory, INT16 iShowCmd )
341 { HINSTANCE16 retval=31;
345 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
346 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
347 lpParameters ? lpParameters : "<null>",
348 lpDirectory ? lpDirectory : "<null>", iShowCmd);
350 if (lpFile==NULL) return 0; /* should not happen */
351 if (lpOperation==NULL) /* default is open */
355 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
356 SetCurrentDirectoryA( lpDirectory );
359 /* First try to execute lpFile with lpParameters directly */
361 strcat(cmd,lpParameters ? lpParameters : "");
363 retval = WinExec16( cmd, iShowCmd );
365 /* Unable to execute lpFile directly
366 Check if we can match an application to lpFile */
370 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
372 if (retval > 32) /* Found */
377 strcat(cmd,lpParameters);
379 retval = WinExec16( cmd, iShowCmd );
381 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
383 char lpstrProtocol[256];
388 lpstrRes = strchr(lpFile,':');
389 iSize = lpstrRes - lpFile;
391 /* Looking for ...protocol\shell\lpOperation\command */
392 strncpy(lpstrProtocol,lpFile,iSize);
393 lpstrProtocol[iSize]='\0';
394 strcat( lpstrProtocol, "\\shell\\" );
395 strcat( lpstrProtocol, lpOperation );
396 strcat( lpstrProtocol, "\\command" );
398 /* Remove File Protocol from lpFile */
399 /* In the case file://path/file */
400 if(!strncasecmp(lpFile,"file",iSize))
403 while(*lpFile == ':') lpFile++;
407 /* Get the application for the protocol and execute it */
408 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
409 &cmdlen ) == ERROR_SUCCESS )
413 char param[256] = "";
416 /* Get the parameters needed by the application
417 from the associated ddeexec key */
418 tmp = strstr(lpstrProtocol,"command");
420 strcat(lpstrProtocol,"ddeexec");
422 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,¶mlen ) == ERROR_SUCCESS)
429 /* Is there a replace() function anywhere? */
432 tok=strstr( cmd, "%1" );
435 tok[0]='\0'; /* truncate string at the percent */
436 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
437 tok=strstr( cmd, "%1" );
438 if ((tok!=NULL) && (strlen(tok)>2))
440 strcat( cmd, &tok[2] );
444 retval = WinExec16( cmd, iShowCmd );
447 /* Check if file specified is in the form www.??????.*** */
448 else if(!strncasecmp(lpFile,"www",3))
450 /* if so, append lpFile http:// and call ShellExecute */
451 char lpstrTmpFile[256] = "http://" ;
452 strcat(lpstrTmpFile,lpFile);
453 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
457 SetCurrentDirectoryA( old_dir );
461 /*************************************************************************
462 * FindExecutable (SHELL.21)
464 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
466 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
470 /*************************************************************************
471 * AboutDlgProc (SHELL.33)
473 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
475 { return AboutDlgProc( hWnd, msg, wParam, lParam );
479 /*************************************************************************
480 * ShellAbout (SHELL.22)
482 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
484 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
487 /*************************************************************************
488 * InternalExtractIcon [SHELL.39]
490 * This abortion is called directly by Progman
492 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
493 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
496 HICON16 *RetPtr = NULL;
500 TRACE("(%04x,file %s,start %d,extract %d\n",
501 hInstance, lpszExeFileName, nIconIndex, n);
506 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
508 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
509 RetPtr = (HICON16*)GlobalLock16(hRet);
511 if (hFile == HFILE_ERROR)
512 { /* not found - load from builtin module if available */
513 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
515 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
516 hInst = LoadLibraryA(lpszExeFileName);
520 for (i=nIconIndex; i < nIconIndex + n; i++)
521 RetPtr[i-nIconIndex] =
522 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
526 GlobalFree16( hRet );
530 if (nIconIndex == (UINT16)-1) /* get number of icons */
532 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
538 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
539 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
540 GetSystemMetrics(SM_CXICON),
541 GetSystemMetrics(SM_CYICON),
546 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
550 GlobalFree16( hRet );
553 HeapFree( GetProcessHeap(), 0, icons );
558 /*************************************************************************
559 * ExtractIcon (SHELL.34)
561 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
564 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
567 /*************************************************************************
568 * ExtractIconEx (SHELL.40)
570 HICON16 WINAPI ExtractIconEx16(
571 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
572 HICON16 *phiconSmall, UINT16 nIcons
574 HICON *ilarge,*ismall;
579 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
583 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
586 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
588 for (i=0;i<nIcons;i++)
589 phiconLarge[i]=ilarge[i];
590 HeapFree(GetProcessHeap(),0,ilarge);
593 for (i=0;i<nIcons;i++)
594 phiconSmall[i]=ismall[i];
595 HeapFree(GetProcessHeap(),0,ismall);
600 /*************************************************************************
601 * ExtractAssociatedIcon [SHELL.36]
603 * Return icon for given file (either from file itself or from associated
604 * executable) and patch parameters if needed.
606 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
613 lpiIcon = &wDummyIcon;
615 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
618 { if( hIcon == 1 ) /* no icons found in given file */
619 { char tempPath[0x80];
620 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
622 if( uRet > 32 && tempPath[0] )
623 { strcpy(lpIconPath,tempPath);
624 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
632 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
634 *lpiIcon = 6; /* generic icon - found nothing */
636 GetModuleFileName16(hInst, lpIconPath, 0x80);
637 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
642 /*************************************************************************
643 * ExtractAssociatedIconA (SHELL32.@)
645 * Return icon for given file (either from file itself or from associated
646 * executable) and patch parameters if needed.
648 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
650 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
653 /*************************************************************************
654 * FindEnvironmentString [SHELL.38]
656 * Returns a pointer into the DOS environment... Ugh.
658 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
664 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
665 { if( strncasecmp(lpEnv, entry, l) )
668 return (lpEnv + l); /* empty entry */
669 else if ( *(lpEnv+l)== '=' )
670 return (lpEnv + l + 1);
675 /**********************************************************************/
677 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
679 LPSTR lpEnv,lpString;
682 spEnv = GetDOSEnvironment16();
684 lpEnv = MapSL(spEnv);
685 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
687 if( lpString ) /* offset should be small enough */
688 return spEnv + (lpString - lpEnv);
692 /*************************************************************************
693 * DoEnvironmentSubst [SHELL.37]
695 * Replace %KEYWORD% in the str with the value of variable KEYWORD
696 * from "DOS" environment.
698 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
700 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
701 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
703 LPSTR lpbstr = lpBuffer;
707 TRACE("accept %s\n", str);
709 while( *lpstr && lpbstr - lpBuffer < length )
715 do { lpend++; } while( *lpend && *lpend != '%' );
716 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
720 lpKey = SHELL_FindString(lpEnv, lpstr+1);
721 if( lpKey ) /* found key value */
723 int l = strlen(lpKey);
725 if( l > length - (lpbstr - lpBuffer) - 1 )
727 WARN("-- Env subst aborted - string too short\n");
731 strcpy(lpbstr, lpKey);
738 else break; /* back off and whine */
743 *lpbstr++ = *lpstr++;
747 if( lpstr - str == strlen(str) )
749 strncpy(str, lpBuffer, length);
755 TRACE("-- return %s\n", str);
758 HeapFree( GetProcessHeap(), 0, lpBuffer);
760 /* Return str length in the LOWORD
761 * and 1 in HIWORD if subst was successful.
763 return (DWORD)MAKELONG(strlen(str), length);
766 /*************************************************************************
767 * ShellHookProc [SHELL.103]
768 * System-wide WH_SHELL hook.
770 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
772 TRACE("%i, %04x, %08x\n", code, wParam,
774 if( SHELL_hHook && SHELL_hWnd )
779 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
780 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
781 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
783 PostMessageA( SHELL_hWnd, uMsg, wParam, 0 );
785 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
788 /*************************************************************************
789 * RegisterShellHook [SHELL.102]
791 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
793 TRACE("%04x [%u]\n", hWnd, uAction );
797 case 2: /* register hWnd as a shell window */
800 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
801 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
802 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
805 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
806 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
807 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
810 WARN("-- unable to install ShellHookProc()!\n");
814 return ((SHELL_hWnd = hWnd) != 0);
818 WARN("-- unknown code %i\n", uAction );
819 SHELL_hWnd = 0; /* just in case */
825 /***********************************************************************
826 * DriveType (SHELL.262)
828 UINT16 WINAPI DriveType16( UINT16 drive )
831 char path[] = "A:\\";
833 ret = GetDriveTypeA(path);
834 switch(ret) /* some values are not supported in Win16 */
839 case DRIVE_NO_ROOT_DIR: