2 * Shell Library Functions
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "wine/shell16.h"
19 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(shell);
24 DECLARE_DEBUG_CHANNEL(exec);
27 typedef struct { /* structure for dropped files */
30 BOOL16 fInNonClientArea;
31 /* memory block with filenames follows */
32 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
34 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
35 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
36 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
38 static HWND16 SHELL_hWnd = 0;
39 static HHOOK SHELL_hHook = 0;
40 static UINT16 uMsgWndCreated = 0;
41 static UINT16 uMsgWndDestroyed = 0;
42 static UINT16 uMsgShellActivate = 0;
43 HINSTANCE16 SHELL_hInstance = 0;
44 HINSTANCE SHELL_hInstance32;
45 static int SHELL_Attach = 0;
47 /***********************************************************************
48 * SHELL_DllEntryPoint [SHELL.entry]
50 * Initialization code for shell.dll. Automatically loads the
51 * 32-bit shell32.dll to allow thunking up to 32-bit code.
55 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
56 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
58 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
59 Reason, hInst, ds, HeapSize, res1, res2);
63 case DLL_PROCESS_ATTACH:
67 ERR("shell.dll instantiated twice!\n");
69 * We should return FALSE here, but that will break
70 * most apps that use CreateProcess because we do
71 * not yet support seperate address-spaces.
76 SHELL_hInstance = hInst;
77 if(!SHELL_hInstance32)
79 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
81 ERR("Could not load sibling shell32.dll\n");
87 case DLL_PROCESS_DETACH:
92 FreeLibrary(SHELL_hInstance32);
99 /*************************************************************************
100 * DragAcceptFiles16 [SHELL.9]
102 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
104 DragAcceptFiles(hWnd, b);
107 /*************************************************************************
108 * DragQueryFile16 [SHELL.11]
110 UINT16 WINAPI DragQueryFile16(
118 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
120 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
122 if(!lpDropFileStruct) goto end;
124 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
125 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
129 while (*lpDrop++); /* skip filename */
132 i = (wFile == 0xFFFFFFFF) ? i : 0;
139 if (!lpszFile ) goto end; /* needed buffer size */
140 i = (wLength > i) ? i : wLength;
141 lstrcpynA (lpszFile, lpDrop, i);
143 GlobalUnlock16(hDrop);
147 /*************************************************************************
148 * DragFinish16 [SHELL.12]
150 void WINAPI DragFinish16(HDROP16 h)
153 GlobalFree16((HGLOBAL16)h);
157 /*************************************************************************
158 * DragQueryPoint16 [SHELL.13]
160 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
162 LPDROPFILESTRUCT16 lpDropFileStruct;
165 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
167 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
168 bRet = lpDropFileStruct->fInNonClientArea;
170 GlobalUnlock16(hDrop);
174 /*************************************************************************
175 * SHELL_FindExecutable [Internal]
177 * Utility for code sharing between FindExecutable and ShellExecute
179 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
182 { char *extension = NULL; /* pointer to file extension */
183 char tmpext[5]; /* local copy to mung as we please */
184 char filetype[256]; /* registry name for this filetype */
185 LONG filetypelen=256; /* length of above */
186 char command[256]; /* command from registry */
187 LONG commandlen=256; /* This is the most DOS can handle :) */
188 char buffer[256]; /* Used to GetProfileString */
189 HINSTANCE retval=31; /* default - 'No association was found' */
190 char *tok; /* token pointer */
191 int i; /* random counter */
192 char xlpFile[256] = ""; /* result of SearchPath */
194 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
196 lpResult[0]='\0'; /* Start off with an empty return string */
198 /* trap NULL parameters on entry */
199 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
200 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
201 lpFile, lpOperation, lpResult);
202 return 2; /* File not found. Close enough, I guess. */
205 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
206 { TRACE("SearchPathA returned non-zero\n");
210 /* First thing we need is the file's extension */
211 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
212 /* File->Run in progman uses */
214 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
216 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
217 { WARN("Returning 31 - No association\n");
218 return 31; /* no association */
221 /* Make local copy & lowercase it for reg & 'programs=' lookup */
222 lstrcpynA( tmpext, extension, 5 );
223 CharLowerA( tmpext );
224 TRACE("%s file\n", tmpext);
226 /* Three places to check: */
227 /* 1. win.ini, [windows], programs (NB no leading '.') */
228 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
229 /* 3. win.ini, [extensions], extension (NB no leading '.' */
230 /* All I know of the order is that registry is checked before */
231 /* extensions; however, it'd make sense to check the programs */
232 /* section first, so that's what happens here. */
234 /* See if it's a program - if GetProfileString fails, we skip this
235 * section. Actually, if GetProfileString fails, we've probably
236 * got a lot more to worry about than running a program... */
237 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
238 buffer, sizeof(buffer)) > 0 )
239 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
241 tok = strtok(buffer, " \t"); /* ? */
244 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
246 strcpy(lpResult, xlpFile);
247 /* Need to perhaps check that the file has a path
249 TRACE("found %s\n", lpResult);
252 /* Greater than 32 to indicate success FIXME According to the
253 * docs, I should be returning a handle for the
254 * executable. Does this mean I'm supposed to open the
255 * executable file or something? More RTFM, I guess... */
257 tok=strtok(NULL, " \t");
262 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
263 &filetypelen ) == ERROR_SUCCESS )
265 filetype[filetypelen]='\0';
266 TRACE("File type: %s\n", filetype);
268 /* Looking for ...buffer\shell\lpOperation\command */
269 strcat( filetype, "\\shell\\" );
270 strcat( filetype, lpOperation );
271 strcat( filetype, "\\command" );
273 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
274 &commandlen ) == ERROR_SUCCESS )
281 /* Get the parameters needed by the application
282 from the associated ddeexec key */
283 tmp = strstr(filetype,"command");
285 strcat(filetype,"ddeexec");
287 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,¶mlen ) == ERROR_SUCCESS)
290 strcat(command,param);
291 commandlen += paramlen;
294 /* Is there a replace() function anywhere? */
295 command[commandlen]='\0';
296 strcpy( lpResult, command );
297 tok=strstr( lpResult, "%1" );
300 tok[0]='\0'; /* truncate string at the percent */
301 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
302 tok=strstr( command, "%1" );
303 if ((tok!=NULL) && (strlen(tok)>2))
305 strcat( lpResult, &tok[2] );
308 retval=33; /* FIXME see above */
311 else /* Check win.ini */
313 /* Toss the leading dot */
315 if ( GetProfileStringA( "extensions", extension, "", command,
316 sizeof(command)) > 0)
318 if (strlen(command)!=0)
320 strcpy( lpResult, command );
321 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
325 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
326 tok=strstr( command, "^" ); /* see above */
327 if ((tok != NULL) && (strlen(tok)>5))
329 strcat( lpResult, &tok[5]);
332 retval=33; /* FIXME - see above */
337 TRACE("returning %s\n", lpResult);
341 /*************************************************************************
342 * ShellExecute16 [SHELL.20]
344 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
345 LPCSTR lpFile, LPCSTR lpParameters,
346 LPCSTR lpDirectory, INT16 iShowCmd )
347 { HINSTANCE16 retval=31;
351 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
352 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
353 lpParameters ? lpParameters : "<null>",
354 lpDirectory ? lpDirectory : "<null>", iShowCmd);
356 if (lpFile==NULL) return 0; /* should not happen */
357 if (lpOperation==NULL) /* default is open */
361 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
362 SetCurrentDirectoryA( lpDirectory );
365 /* First try to execute lpFile with lpParameters directly */
367 strcat(cmd,lpParameters ? lpParameters : "");
369 retval = WinExec16( cmd, iShowCmd );
371 /* Unable to execute lpFile directly
372 Check if we can match an application to lpFile */
376 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
378 if (retval > 32) /* Found */
383 strcat(cmd,lpParameters);
385 retval = WinExec16( cmd, iShowCmd );
387 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
389 char lpstrProtocol[256];
394 lpstrRes = strchr(lpFile,':');
395 iSize = lpstrRes - lpFile;
397 /* Looking for ...protocol\shell\lpOperation\command */
398 strncpy(lpstrProtocol,lpFile,iSize);
399 lpstrProtocol[iSize]='\0';
400 strcat( lpstrProtocol, "\\shell\\" );
401 strcat( lpstrProtocol, lpOperation );
402 strcat( lpstrProtocol, "\\command" );
404 /* Remove File Protocol from lpFile */
405 /* In the case file://path/file */
406 if(!strncasecmp(lpFile,"file",iSize))
409 while(*lpFile == ':') lpFile++;
413 /* Get the application for the protocol and execute it */
414 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
415 &cmdlen ) == ERROR_SUCCESS )
419 char param[256] = "";
422 /* Get the parameters needed by the application
423 from the associated ddeexec key */
424 tmp = strstr(lpstrProtocol,"command");
426 strcat(lpstrProtocol,"ddeexec");
428 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,¶mlen ) == ERROR_SUCCESS)
435 /* Is there a replace() function anywhere? */
438 tok=strstr( cmd, "%1" );
441 tok[0]='\0'; /* truncate string at the percent */
442 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
443 tok=strstr( cmd, "%1" );
444 if ((tok!=NULL) && (strlen(tok)>2))
446 strcat( cmd, &tok[2] );
450 retval = WinExec16( cmd, iShowCmd );
453 /* Check if file specified is in the form www.??????.*** */
454 else if(!strncasecmp(lpFile,"www",3))
456 /* if so, append lpFile http:// and call ShellExecute */
457 char lpstrTmpFile[256] = "http://" ;
458 strcat(lpstrTmpFile,lpFile);
459 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
463 SetCurrentDirectoryA( old_dir );
467 /*************************************************************************
468 * FindExecutable16 (SHELL.21)
470 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
472 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
476 /*************************************************************************
477 * AboutDlgProc16 (SHELL.33)
479 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
481 { return AboutDlgProc( hWnd, msg, wParam, lParam );
485 /*************************************************************************
486 * ShellAbout16 (SHELL.22)
488 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
490 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
493 /*************************************************************************
494 * InternalExtractIcon [SHELL.39]
496 * This abortion is called directly by Progman
498 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
499 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
502 HICON16 *RetPtr = NULL;
506 TRACE("(%04x,file %s,start %d,extract %d\n",
507 hInstance, lpszExeFileName, nIconIndex, n);
512 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
514 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
515 RetPtr = (HICON16*)GlobalLock16(hRet);
517 if (hFile == HFILE_ERROR)
518 { /* not found - load from builtin module if available */
519 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
521 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
522 hInst = LoadLibraryA(lpszExeFileName);
526 for (i=nIconIndex; i < nIconIndex + n; i++)
527 RetPtr[i-nIconIndex] =
528 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
532 GlobalFree16( hRet );
536 if (nIconIndex == (UINT16)-1) /* get number of icons */
538 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
544 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
545 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
546 GetSystemMetrics(SM_CXICON),
547 GetSystemMetrics(SM_CYICON),
552 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
556 GlobalFree16( hRet );
559 HeapFree( GetProcessHeap(), 0, icons );
564 /*************************************************************************
565 * ExtractIcon16 (SHELL.34)
567 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
570 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
573 /*************************************************************************
574 * ExtractIconEx16 (SHELL.40)
576 HICON16 WINAPI ExtractIconEx16(
577 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
578 HICON16 *phiconSmall, UINT16 nIcons
580 HICON *ilarge,*ismall;
585 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
589 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
592 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
594 for (i=0;i<nIcons;i++)
595 phiconLarge[i]=ilarge[i];
596 HeapFree(GetProcessHeap(),0,ilarge);
599 for (i=0;i<nIcons;i++)
600 phiconSmall[i]=ismall[i];
601 HeapFree(GetProcessHeap(),0,ismall);
606 /*************************************************************************
607 * ExtractAssociatedIcon [SHELL.36]
609 * Return icon for given file (either from file itself or from associated
610 * executable) and patch parameters if needed.
612 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
619 lpiIcon = &wDummyIcon;
621 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
624 { if( hIcon == 1 ) /* no icons found in given file */
625 { char tempPath[0x80];
626 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
628 if( uRet > 32 && tempPath[0] )
629 { strcpy(lpIconPath,tempPath);
630 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
638 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
640 *lpiIcon = 6; /* generic icon - found nothing */
642 GetModuleFileName16(hInst, lpIconPath, 0x80);
643 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
648 /*************************************************************************
649 * ExtractAssociatedIconA
651 * Return icon for given file (either from file itself or from associated
652 * executable) and patch parameters if needed.
654 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
656 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
659 /*************************************************************************
660 * FindEnvironmentString [SHELL.38]
662 * Returns a pointer into the DOS environment... Ugh.
664 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
670 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
671 { if( strncasecmp(lpEnv, entry, l) )
674 return (lpEnv + l); /* empty entry */
675 else if ( *(lpEnv+l)== '=' )
676 return (lpEnv + l + 1);
681 /**********************************************************************/
683 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
685 LPSTR lpEnv,lpString;
688 spEnv = GetDOSEnvironment16();
690 lpEnv = MapSL(spEnv);
691 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
693 if( lpString ) /* offset should be small enough */
694 return spEnv + (lpString - lpEnv);
698 /*************************************************************************
699 * DoEnvironmentSubst [SHELL.37]
701 * Replace %KEYWORD% in the str with the value of variable KEYWORD
702 * from "DOS" environment.
704 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
706 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
707 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
709 LPSTR lpbstr = lpBuffer;
713 TRACE("accept %s\n", str);
715 while( *lpstr && lpbstr - lpBuffer < length )
721 do { lpend++; } while( *lpend && *lpend != '%' );
722 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
726 lpKey = SHELL_FindString(lpEnv, lpstr+1);
727 if( lpKey ) /* found key value */
729 int l = strlen(lpKey);
731 if( l > length - (lpbstr - lpBuffer) - 1 )
733 WARN("-- Env subst aborted - string too short\n");
737 strcpy(lpbstr, lpKey);
744 else break; /* back off and whine */
749 *lpbstr++ = *lpstr++;
753 if( lpstr - str == strlen(str) )
755 strncpy(str, lpBuffer, length);
761 TRACE("-- return %s\n", str);
764 HeapFree( GetProcessHeap(), 0, lpBuffer);
766 /* Return str length in the LOWORD
767 * and 1 in HIWORD if subst was successful.
769 return (DWORD)MAKELONG(strlen(str), length);
772 /*************************************************************************
773 * ShellHookProc [SHELL.103]
774 * System-wide WH_SHELL hook.
776 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
778 TRACE("%i, %04x, %08x\n", code, wParam,
780 if( SHELL_hHook && SHELL_hWnd )
785 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
786 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
787 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
789 PostMessageA( SHELL_hWnd, uMsg, wParam, 0 );
791 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
794 /*************************************************************************
795 * RegisterShellHook [SHELL.102]
797 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
799 TRACE("%04x [%u]\n", hWnd, uAction );
803 case 2: /* register hWnd as a shell window */
806 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
807 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
808 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
811 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
812 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
813 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
816 WARN("-- unable to install ShellHookProc()!\n");
820 return ((SHELL_hWnd = hWnd) != 0);
824 WARN("-- unknown code %i\n", uAction );
825 SHELL_hWnd = 0; /* just in case */
831 /***********************************************************************
832 * DriveType16 (SHELL.262)
834 UINT16 WINAPI DriveType16( UINT16 drive )
837 char path[] = "A:\\";
839 ret = GetDriveTypeA(path);
840 switch(ret) /* some values are not supported in Win16 */
845 case DRIVE_NO_ROOT_DIR: