2 * Shell Library Functions
22 #include "wine/shell16.h"
23 #include "wine/winbase16.h"
24 #include "wine/winuser16.h"
25 #include "shell32_main.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(shell);
30 DECLARE_DEBUG_CHANNEL(exec);
33 typedef struct { /* structure for dropped files */
36 BOOL16 fInNonClientArea;
37 /* memory block with filenames follows */
38 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
40 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
41 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
42 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
44 static HWND16 SHELL_hWnd = 0;
45 static HHOOK SHELL_hHook = 0;
46 static UINT16 uMsgWndCreated = 0;
47 static UINT16 uMsgWndDestroyed = 0;
48 static UINT16 uMsgShellActivate = 0;
49 HINSTANCE16 SHELL_hInstance = 0;
50 HINSTANCE SHELL_hInstance32;
51 static int SHELL_Attach = 0;
53 /***********************************************************************
54 * DllEntryPoint [SHELL.101]
56 * Initialization code for shell.dll. Automatically loads the
57 * 32-bit shell32.dll to allow thunking up to 32-bit code.
61 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
62 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
64 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
65 Reason, hInst, ds, HeapSize, res1, res2);
69 case DLL_PROCESS_ATTACH:
70 if (SHELL_Attach++) break;
71 SHELL_hInstance = hInst;
72 if(!SHELL_hInstance32)
74 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
76 ERR("Could not load sibling shell32.dll\n");
82 case DLL_PROCESS_DETACH:
87 FreeLibrary(SHELL_hInstance32);
94 /*************************************************************************
95 * DragAcceptFiles [SHELL.9]
97 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
99 DragAcceptFiles(hWnd, b);
102 /*************************************************************************
103 * DragQueryFile [SHELL.11]
105 UINT16 WINAPI DragQueryFile16(
113 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
115 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
117 if(!lpDropFileStruct) goto end;
119 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
120 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
124 while (*lpDrop++); /* skip filename */
127 i = (wFile == 0xFFFFFFFF) ? i : 0;
134 if (!lpszFile ) goto end; /* needed buffer size */
135 i = (wLength > i) ? i : wLength;
136 lstrcpynA (lpszFile, lpDrop, i);
138 GlobalUnlock16(hDrop);
142 /*************************************************************************
143 * DragFinish [SHELL.12]
145 void WINAPI DragFinish16(HDROP16 h)
148 GlobalFree16((HGLOBAL16)h);
152 /*************************************************************************
153 * DragQueryPoint [SHELL.13]
155 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
157 LPDROPFILESTRUCT16 lpDropFileStruct;
160 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
162 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
163 bRet = lpDropFileStruct->fInNonClientArea;
165 GlobalUnlock16(hDrop);
169 /*************************************************************************
170 * SHELL_FindExecutable [Internal]
172 * Utility for code sharing between FindExecutable and ShellExecute
174 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
177 { char *extension = NULL; /* pointer to file extension */
178 char tmpext[5]; /* local copy to mung as we please */
179 char filetype[256]; /* registry name for this filetype */
180 LONG filetypelen=256; /* length of above */
181 char command[256]; /* command from registry */
182 LONG commandlen=256; /* This is the most DOS can handle :) */
183 char buffer[256]; /* Used to GetProfileString */
184 HINSTANCE retval=31; /* default - 'No association was found' */
185 char *tok; /* token pointer */
186 int i; /* random counter */
187 char xlpFile[256] = ""; /* result of SearchPath */
189 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
191 lpResult[0]='\0'; /* Start off with an empty return string */
193 /* trap NULL parameters on entry */
194 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
195 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
196 lpFile, lpOperation, lpResult);
197 return 2; /* File not found. Close enough, I guess. */
200 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
201 { TRACE("SearchPathA returned non-zero\n");
205 /* First thing we need is the file's extension */
206 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
207 /* File->Run in progman uses */
209 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
211 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
212 { WARN("Returning 31 - No association\n");
213 return 31; /* no association */
216 /* Make local copy & lowercase it for reg & 'programs=' lookup */
217 lstrcpynA( tmpext, extension, 5 );
218 CharLowerA( tmpext );
219 TRACE("%s file\n", tmpext);
221 /* Three places to check: */
222 /* 1. win.ini, [windows], programs (NB no leading '.') */
223 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
224 /* 3. win.ini, [extensions], extension (NB no leading '.' */
225 /* All I know of the order is that registry is checked before */
226 /* extensions; however, it'd make sense to check the programs */
227 /* section first, so that's what happens here. */
229 /* See if it's a program - if GetProfileString fails, we skip this
230 * section. Actually, if GetProfileString fails, we've probably
231 * got a lot more to worry about than running a program... */
232 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
233 buffer, sizeof(buffer)) > 0 )
234 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
236 tok = strtok(buffer, " \t"); /* ? */
239 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
241 strcpy(lpResult, xlpFile);
242 /* Need to perhaps check that the file has a path
244 TRACE("found %s\n", lpResult);
247 /* Greater than 32 to indicate success FIXME According to the
248 * docs, I should be returning a handle for the
249 * executable. Does this mean I'm supposed to open the
250 * executable file or something? More RTFM, I guess... */
252 tok=strtok(NULL, " \t");
257 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
258 &filetypelen ) == ERROR_SUCCESS )
260 filetype[filetypelen]='\0';
261 TRACE("File type: %s\n", filetype);
263 /* Looking for ...buffer\shell\lpOperation\command */
264 strcat( filetype, "\\shell\\" );
265 strcat( filetype, lpOperation );
266 strcat( filetype, "\\command" );
268 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
269 &commandlen ) == ERROR_SUCCESS )
276 /* Get the parameters needed by the application
277 from the associated ddeexec key */
278 tmp = strstr(filetype,"command");
280 strcat(filetype,"ddeexec");
282 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,¶mlen ) == ERROR_SUCCESS)
285 strcat(command,param);
286 commandlen += paramlen;
289 /* Is there a replace() function anywhere? */
290 command[commandlen]='\0';
291 strcpy( lpResult, command );
292 tok=strstr( lpResult, "%1" );
295 tok[0]='\0'; /* truncate string at the percent */
296 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
297 tok=strstr( command, "%1" );
298 if ((tok!=NULL) && (strlen(tok)>2))
300 strcat( lpResult, &tok[2] );
303 retval=33; /* FIXME see above */
306 else /* Check win.ini */
308 /* Toss the leading dot */
310 if ( GetProfileStringA( "extensions", extension, "", command,
311 sizeof(command)) > 0)
313 if (strlen(command)!=0)
315 strcpy( lpResult, command );
316 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
320 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
321 tok=strstr( command, "^" ); /* see above */
322 if ((tok != NULL) && (strlen(tok)>5))
324 strcat( lpResult, &tok[5]);
327 retval=33; /* FIXME - see above */
332 TRACE("returning %s\n", lpResult);
336 /*************************************************************************
337 * ShellExecute [SHELL.20]
339 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
340 LPCSTR lpFile, LPCSTR lpParameters,
341 LPCSTR lpDirectory, INT16 iShowCmd )
342 { HINSTANCE16 retval=31;
346 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
347 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
348 lpParameters ? lpParameters : "<null>",
349 lpDirectory ? lpDirectory : "<null>", iShowCmd);
351 if (lpFile==NULL) return 0; /* should not happen */
352 if (lpOperation==NULL) /* default is open */
356 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
357 SetCurrentDirectoryA( lpDirectory );
360 /* First try to execute lpFile with lpParameters directly */
362 strcat(cmd,lpParameters ? lpParameters : "");
364 retval = WinExec16( cmd, iShowCmd );
366 /* Unable to execute lpFile directly
367 Check if we can match an application to lpFile */
371 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
373 if (retval > 32) /* Found */
378 strcat(cmd,lpParameters);
380 retval = WinExec16( cmd, iShowCmd );
382 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
384 char lpstrProtocol[256];
389 lpstrRes = strchr(lpFile,':');
390 iSize = lpstrRes - lpFile;
392 /* Looking for ...protocol\shell\lpOperation\command */
393 strncpy(lpstrProtocol,lpFile,iSize);
394 lpstrProtocol[iSize]='\0';
395 strcat( lpstrProtocol, "\\shell\\" );
396 strcat( lpstrProtocol, lpOperation );
397 strcat( lpstrProtocol, "\\command" );
399 /* Remove File Protocol from lpFile */
400 /* In the case file://path/file */
401 if(!strncasecmp(lpFile,"file",iSize))
404 while(*lpFile == ':') lpFile++;
408 /* Get the application for the protocol and execute it */
409 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
410 &cmdlen ) == ERROR_SUCCESS )
414 char param[256] = "";
417 /* Get the parameters needed by the application
418 from the associated ddeexec key */
419 tmp = strstr(lpstrProtocol,"command");
421 strcat(lpstrProtocol,"ddeexec");
423 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,¶mlen ) == ERROR_SUCCESS)
430 /* Is there a replace() function anywhere? */
433 tok=strstr( cmd, "%1" );
436 tok[0]='\0'; /* truncate string at the percent */
437 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
438 tok=strstr( cmd, "%1" );
439 if ((tok!=NULL) && (strlen(tok)>2))
441 strcat( cmd, &tok[2] );
445 retval = WinExec16( cmd, iShowCmd );
448 /* Check if file specified is in the form www.??????.*** */
449 else if(!strncasecmp(lpFile,"www",3))
451 /* if so, append lpFile http:// and call ShellExecute */
452 char lpstrTmpFile[256] = "http://" ;
453 strcat(lpstrTmpFile,lpFile);
454 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
458 SetCurrentDirectoryA( old_dir );
462 /*************************************************************************
463 * FindExecutable (SHELL.21)
465 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
467 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
471 /*************************************************************************
472 * AboutDlgProc (SHELL.33)
474 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
476 { return AboutDlgProc( hWnd, msg, wParam, lParam );
480 /*************************************************************************
481 * ShellAbout (SHELL.22)
483 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
485 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
488 /*************************************************************************
489 * InternalExtractIcon [SHELL.39]
491 * This abortion is called directly by Progman
493 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
494 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
497 HICON16 *RetPtr = NULL;
501 TRACE("(%04x,file %s,start %d,extract %d\n",
502 hInstance, lpszExeFileName, nIconIndex, n);
507 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
509 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
510 RetPtr = (HICON16*)GlobalLock16(hRet);
512 if (hFile == HFILE_ERROR)
513 { /* not found - load from builtin module if available */
514 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
516 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
517 hInst = LoadLibraryA(lpszExeFileName);
521 for (i=nIconIndex; i < nIconIndex + n; i++)
522 RetPtr[i-nIconIndex] =
523 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
527 GlobalFree16( hRet );
531 if (nIconIndex == (UINT16)-1) /* get number of icons */
533 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
539 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
540 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
541 GetSystemMetrics(SM_CXICON),
542 GetSystemMetrics(SM_CYICON),
547 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
551 GlobalFree16( hRet );
554 HeapFree( GetProcessHeap(), 0, icons );
559 /*************************************************************************
560 * ExtractIcon (SHELL.34)
562 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
565 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
568 /*************************************************************************
569 * ExtractIconEx (SHELL.40)
571 HICON16 WINAPI ExtractIconEx16(
572 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
573 HICON16 *phiconSmall, UINT16 nIcons
575 HICON *ilarge,*ismall;
580 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
584 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
587 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
589 for (i=0;i<nIcons;i++)
590 phiconLarge[i]=ilarge[i];
591 HeapFree(GetProcessHeap(),0,ilarge);
594 for (i=0;i<nIcons;i++)
595 phiconSmall[i]=ismall[i];
596 HeapFree(GetProcessHeap(),0,ismall);
601 /*************************************************************************
602 * ExtractAssociatedIcon [SHELL.36]
604 * Return icon for given file (either from file itself or from associated
605 * executable) and patch parameters if needed.
607 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
614 lpiIcon = &wDummyIcon;
616 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
619 { if( hIcon == 1 ) /* no icons found in given file */
620 { char tempPath[0x80];
621 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
623 if( uRet > 32 && tempPath[0] )
624 { strcpy(lpIconPath,tempPath);
625 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
633 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
635 *lpiIcon = 6; /* generic icon - found nothing */
637 GetModuleFileName16(hInst, lpIconPath, 0x80);
638 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
643 /*************************************************************************
644 * ExtractAssociatedIconA (SHELL32.@)
646 * Return icon for given file (either from file itself or from associated
647 * executable) and patch parameters if needed.
649 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
651 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
654 /*************************************************************************
655 * FindEnvironmentString [SHELL.38]
657 * Returns a pointer into the DOS environment... Ugh.
659 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
665 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
666 { if( strncasecmp(lpEnv, entry, l) )
669 return (lpEnv + l); /* empty entry */
670 else if ( *(lpEnv+l)== '=' )
671 return (lpEnv + l + 1);
676 /**********************************************************************/
678 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
680 LPSTR lpEnv,lpString;
683 spEnv = GetDOSEnvironment16();
685 lpEnv = MapSL(spEnv);
686 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
688 if( lpString ) /* offset should be small enough */
689 return spEnv + (lpString - lpEnv);
693 /*************************************************************************
694 * DoEnvironmentSubst [SHELL.37]
696 * Replace %KEYWORD% in the str with the value of variable KEYWORD
697 * from "DOS" environment.
699 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
701 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
702 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
704 LPSTR lpbstr = lpBuffer;
708 TRACE("accept %s\n", str);
710 while( *lpstr && lpbstr - lpBuffer < length )
716 do { lpend++; } while( *lpend && *lpend != '%' );
717 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
721 lpKey = SHELL_FindString(lpEnv, lpstr+1);
722 if( lpKey ) /* found key value */
724 int l = strlen(lpKey);
726 if( l > length - (lpbstr - lpBuffer) - 1 )
728 WARN("-- Env subst aborted - string too short\n");
732 strcpy(lpbstr, lpKey);
739 else break; /* back off and whine */
744 *lpbstr++ = *lpstr++;
748 if( lpstr - str == strlen(str) )
750 strncpy(str, lpBuffer, length);
756 TRACE("-- return %s\n", str);
759 HeapFree( GetProcessHeap(), 0, lpBuffer);
761 /* Return str length in the LOWORD
762 * and 1 in HIWORD if subst was successful.
764 return (DWORD)MAKELONG(strlen(str), length);
767 /*************************************************************************
768 * ShellHookProc [SHELL.103]
769 * System-wide WH_SHELL hook.
771 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
773 TRACE("%i, %04x, %08x\n", code, wParam,
775 if( SHELL_hHook && SHELL_hWnd )
780 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
781 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
782 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
784 PostMessageA( SHELL_hWnd, uMsg, wParam, 0 );
786 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
789 /*************************************************************************
790 * RegisterShellHook [SHELL.102]
792 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
794 TRACE("%04x [%u]\n", hWnd, uAction );
798 case 2: /* register hWnd as a shell window */
801 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
802 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
803 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
806 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
807 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
808 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
811 WARN("-- unable to install ShellHookProc()!\n");
815 return ((SHELL_hWnd = hWnd) != 0);
819 WARN("-- unknown code %i\n", uAction );
820 SHELL_hWnd = 0; /* just in case */
826 /***********************************************************************
827 * DriveType (SHELL.262)
829 UINT16 WINAPI DriveType16( UINT16 drive )
832 char path[] = "A:\\";
834 ret = GetDriveTypeA(path);
835 switch(ret) /* some values are not supported in Win16 */
840 case DRIVE_NO_ROOT_DIR: