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:
64 if (SHELL_Attach++) break;
65 SHELL_hInstance = hInst;
66 if(!SHELL_hInstance32)
68 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
70 ERR("Could not load sibling shell32.dll\n");
76 case DLL_PROCESS_DETACH:
81 FreeLibrary(SHELL_hInstance32);
88 /*************************************************************************
89 * DragAcceptFiles16 [SHELL.9]
91 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
93 DragAcceptFiles(hWnd, b);
96 /*************************************************************************
97 * DragQueryFile16 [SHELL.11]
99 UINT16 WINAPI DragQueryFile16(
107 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
109 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
111 if(!lpDropFileStruct) goto end;
113 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
114 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
118 while (*lpDrop++); /* skip filename */
121 i = (wFile == 0xFFFFFFFF) ? i : 0;
128 if (!lpszFile ) goto end; /* needed buffer size */
129 i = (wLength > i) ? i : wLength;
130 lstrcpynA (lpszFile, lpDrop, i);
132 GlobalUnlock16(hDrop);
136 /*************************************************************************
137 * DragFinish16 [SHELL.12]
139 void WINAPI DragFinish16(HDROP16 h)
142 GlobalFree16((HGLOBAL16)h);
146 /*************************************************************************
147 * DragQueryPoint16 [SHELL.13]
149 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
151 LPDROPFILESTRUCT16 lpDropFileStruct;
154 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
156 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
157 bRet = lpDropFileStruct->fInNonClientArea;
159 GlobalUnlock16(hDrop);
163 /*************************************************************************
164 * SHELL_FindExecutable [Internal]
166 * Utility for code sharing between FindExecutable and ShellExecute
168 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
171 { char *extension = NULL; /* pointer to file extension */
172 char tmpext[5]; /* local copy to mung as we please */
173 char filetype[256]; /* registry name for this filetype */
174 LONG filetypelen=256; /* length of above */
175 char command[256]; /* command from registry */
176 LONG commandlen=256; /* This is the most DOS can handle :) */
177 char buffer[256]; /* Used to GetProfileString */
178 HINSTANCE retval=31; /* default - 'No association was found' */
179 char *tok; /* token pointer */
180 int i; /* random counter */
181 char xlpFile[256] = ""; /* result of SearchPath */
183 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
185 lpResult[0]='\0'; /* Start off with an empty return string */
187 /* trap NULL parameters on entry */
188 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
189 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
190 lpFile, lpOperation, lpResult);
191 return 2; /* File not found. Close enough, I guess. */
194 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
195 { TRACE("SearchPathA returned non-zero\n");
199 /* First thing we need is the file's extension */
200 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
201 /* File->Run in progman uses */
203 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
205 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
206 { WARN("Returning 31 - No association\n");
207 return 31; /* no association */
210 /* Make local copy & lowercase it for reg & 'programs=' lookup */
211 lstrcpynA( tmpext, extension, 5 );
212 CharLowerA( tmpext );
213 TRACE("%s file\n", tmpext);
215 /* Three places to check: */
216 /* 1. win.ini, [windows], programs (NB no leading '.') */
217 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
218 /* 3. win.ini, [extensions], extension (NB no leading '.' */
219 /* All I know of the order is that registry is checked before */
220 /* extensions; however, it'd make sense to check the programs */
221 /* section first, so that's what happens here. */
223 /* See if it's a program - if GetProfileString fails, we skip this
224 * section. Actually, if GetProfileString fails, we've probably
225 * got a lot more to worry about than running a program... */
226 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
227 buffer, sizeof(buffer)) > 0 )
228 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
230 tok = strtok(buffer, " \t"); /* ? */
233 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
235 strcpy(lpResult, xlpFile);
236 /* Need to perhaps check that the file has a path
238 TRACE("found %s\n", lpResult);
241 /* Greater than 32 to indicate success FIXME According to the
242 * docs, I should be returning a handle for the
243 * executable. Does this mean I'm supposed to open the
244 * executable file or something? More RTFM, I guess... */
246 tok=strtok(NULL, " \t");
251 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
252 &filetypelen ) == ERROR_SUCCESS )
254 filetype[filetypelen]='\0';
255 TRACE("File type: %s\n", filetype);
257 /* Looking for ...buffer\shell\lpOperation\command */
258 strcat( filetype, "\\shell\\" );
259 strcat( filetype, lpOperation );
260 strcat( filetype, "\\command" );
262 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
263 &commandlen ) == ERROR_SUCCESS )
270 /* Get the parameters needed by the application
271 from the associated ddeexec key */
272 tmp = strstr(filetype,"command");
274 strcat(filetype,"ddeexec");
276 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,¶mlen ) == ERROR_SUCCESS)
279 strcat(command,param);
280 commandlen += paramlen;
283 /* Is there a replace() function anywhere? */
284 command[commandlen]='\0';
285 strcpy( lpResult, command );
286 tok=strstr( lpResult, "%1" );
289 tok[0]='\0'; /* truncate string at the percent */
290 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
291 tok=strstr( command, "%1" );
292 if ((tok!=NULL) && (strlen(tok)>2))
294 strcat( lpResult, &tok[2] );
297 retval=33; /* FIXME see above */
300 else /* Check win.ini */
302 /* Toss the leading dot */
304 if ( GetProfileStringA( "extensions", extension, "", command,
305 sizeof(command)) > 0)
307 if (strlen(command)!=0)
309 strcpy( lpResult, command );
310 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
314 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
315 tok=strstr( command, "^" ); /* see above */
316 if ((tok != NULL) && (strlen(tok)>5))
318 strcat( lpResult, &tok[5]);
321 retval=33; /* FIXME - see above */
326 TRACE("returning %s\n", lpResult);
330 /*************************************************************************
331 * ShellExecute16 [SHELL.20]
333 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
334 LPCSTR lpFile, LPCSTR lpParameters,
335 LPCSTR lpDirectory, INT16 iShowCmd )
336 { HINSTANCE16 retval=31;
340 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
341 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
342 lpParameters ? lpParameters : "<null>",
343 lpDirectory ? lpDirectory : "<null>", iShowCmd);
345 if (lpFile==NULL) return 0; /* should not happen */
346 if (lpOperation==NULL) /* default is open */
350 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
351 SetCurrentDirectoryA( lpDirectory );
354 /* First try to execute lpFile with lpParameters directly */
356 strcat(cmd,lpParameters ? lpParameters : "");
358 retval = WinExec16( cmd, iShowCmd );
360 /* Unable to execute lpFile directly
361 Check if we can match an application to lpFile */
365 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
367 if (retval > 32) /* Found */
372 strcat(cmd,lpParameters);
374 retval = WinExec16( cmd, iShowCmd );
376 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
378 char lpstrProtocol[256];
383 lpstrRes = strchr(lpFile,':');
384 iSize = lpstrRes - lpFile;
386 /* Looking for ...protocol\shell\lpOperation\command */
387 strncpy(lpstrProtocol,lpFile,iSize);
388 lpstrProtocol[iSize]='\0';
389 strcat( lpstrProtocol, "\\shell\\" );
390 strcat( lpstrProtocol, lpOperation );
391 strcat( lpstrProtocol, "\\command" );
393 /* Remove File Protocol from lpFile */
394 /* In the case file://path/file */
395 if(!strncasecmp(lpFile,"file",iSize))
398 while(*lpFile == ':') lpFile++;
402 /* Get the application for the protocol and execute it */
403 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
404 &cmdlen ) == ERROR_SUCCESS )
408 char param[256] = "";
411 /* Get the parameters needed by the application
412 from the associated ddeexec key */
413 tmp = strstr(lpstrProtocol,"command");
415 strcat(lpstrProtocol,"ddeexec");
417 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,¶mlen ) == ERROR_SUCCESS)
424 /* Is there a replace() function anywhere? */
427 tok=strstr( cmd, "%1" );
430 tok[0]='\0'; /* truncate string at the percent */
431 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
432 tok=strstr( cmd, "%1" );
433 if ((tok!=NULL) && (strlen(tok)>2))
435 strcat( cmd, &tok[2] );
439 retval = WinExec16( cmd, iShowCmd );
442 /* Check if file specified is in the form www.??????.*** */
443 else if(!strncasecmp(lpFile,"www",3))
445 /* if so, append lpFile http:// and call ShellExecute */
446 char lpstrTmpFile[256] = "http://" ;
447 strcat(lpstrTmpFile,lpFile);
448 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
452 SetCurrentDirectoryA( old_dir );
456 /*************************************************************************
457 * FindExecutable16 (SHELL.21)
459 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
461 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
465 /*************************************************************************
466 * AboutDlgProc16 (SHELL.33)
468 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
470 { return AboutDlgProc( hWnd, msg, wParam, lParam );
474 /*************************************************************************
475 * ShellAbout16 (SHELL.22)
477 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
479 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
482 /*************************************************************************
483 * InternalExtractIcon [SHELL.39]
485 * This abortion is called directly by Progman
487 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
488 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
491 HICON16 *RetPtr = NULL;
495 TRACE("(%04x,file %s,start %d,extract %d\n",
496 hInstance, lpszExeFileName, nIconIndex, n);
501 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
503 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
504 RetPtr = (HICON16*)GlobalLock16(hRet);
506 if (hFile == HFILE_ERROR)
507 { /* not found - load from builtin module if available */
508 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
510 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
511 hInst = LoadLibraryA(lpszExeFileName);
515 for (i=nIconIndex; i < nIconIndex + n; i++)
516 RetPtr[i-nIconIndex] =
517 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
521 GlobalFree16( hRet );
525 if (nIconIndex == (UINT16)-1) /* get number of icons */
527 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
533 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
534 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
535 GetSystemMetrics(SM_CXICON),
536 GetSystemMetrics(SM_CYICON),
541 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
545 GlobalFree16( hRet );
548 HeapFree( GetProcessHeap(), 0, icons );
553 /*************************************************************************
554 * ExtractIcon16 (SHELL.34)
556 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
559 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
562 /*************************************************************************
563 * ExtractIconEx16 (SHELL.40)
565 HICON16 WINAPI ExtractIconEx16(
566 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
567 HICON16 *phiconSmall, UINT16 nIcons
569 HICON *ilarge,*ismall;
574 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
578 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
581 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
583 for (i=0;i<nIcons;i++)
584 phiconLarge[i]=ilarge[i];
585 HeapFree(GetProcessHeap(),0,ilarge);
588 for (i=0;i<nIcons;i++)
589 phiconSmall[i]=ismall[i];
590 HeapFree(GetProcessHeap(),0,ismall);
595 /*************************************************************************
596 * ExtractAssociatedIcon [SHELL.36]
598 * Return icon for given file (either from file itself or from associated
599 * executable) and patch parameters if needed.
601 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
608 lpiIcon = &wDummyIcon;
610 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
613 { if( hIcon == 1 ) /* no icons found in given file */
614 { char tempPath[0x80];
615 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
617 if( uRet > 32 && tempPath[0] )
618 { strcpy(lpIconPath,tempPath);
619 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
627 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
629 *lpiIcon = 6; /* generic icon - found nothing */
631 GetModuleFileName16(hInst, lpIconPath, 0x80);
632 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
637 /*************************************************************************
638 * ExtractAssociatedIconA
640 * Return icon for given file (either from file itself or from associated
641 * executable) and patch parameters if needed.
643 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
645 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
648 /*************************************************************************
649 * FindEnvironmentString [SHELL.38]
651 * Returns a pointer into the DOS environment... Ugh.
653 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
659 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
660 { if( strncasecmp(lpEnv, entry, l) )
663 return (lpEnv + l); /* empty entry */
664 else if ( *(lpEnv+l)== '=' )
665 return (lpEnv + l + 1);
670 /**********************************************************************/
672 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
674 LPSTR lpEnv,lpString;
677 spEnv = GetDOSEnvironment16();
679 lpEnv = MapSL(spEnv);
680 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
682 if( lpString ) /* offset should be small enough */
683 return spEnv + (lpString - lpEnv);
687 /*************************************************************************
688 * DoEnvironmentSubst [SHELL.37]
690 * Replace %KEYWORD% in the str with the value of variable KEYWORD
691 * from "DOS" environment.
693 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
695 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
696 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
698 LPSTR lpbstr = lpBuffer;
702 TRACE("accept %s\n", str);
704 while( *lpstr && lpbstr - lpBuffer < length )
710 do { lpend++; } while( *lpend && *lpend != '%' );
711 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
715 lpKey = SHELL_FindString(lpEnv, lpstr+1);
716 if( lpKey ) /* found key value */
718 int l = strlen(lpKey);
720 if( l > length - (lpbstr - lpBuffer) - 1 )
722 WARN("-- Env subst aborted - string too short\n");
726 strcpy(lpbstr, lpKey);
733 else break; /* back off and whine */
738 *lpbstr++ = *lpstr++;
742 if( lpstr - str == strlen(str) )
744 strncpy(str, lpBuffer, length);
750 TRACE("-- return %s\n", str);
753 HeapFree( GetProcessHeap(), 0, lpBuffer);
755 /* Return str length in the LOWORD
756 * and 1 in HIWORD if subst was successful.
758 return (DWORD)MAKELONG(strlen(str), length);
761 /*************************************************************************
762 * ShellHookProc [SHELL.103]
763 * System-wide WH_SHELL hook.
765 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
767 TRACE("%i, %04x, %08x\n", code, wParam,
769 if( SHELL_hHook && SHELL_hWnd )
774 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
775 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
776 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
778 PostMessageA( SHELL_hWnd, uMsg, wParam, 0 );
780 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
783 /*************************************************************************
784 * RegisterShellHook [SHELL.102]
786 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
788 TRACE("%04x [%u]\n", hWnd, uAction );
792 case 2: /* register hWnd as a shell window */
795 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
796 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
797 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
800 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
801 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
802 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
805 WARN("-- unable to install ShellHookProc()!\n");
809 return ((SHELL_hWnd = hWnd) != 0);
813 WARN("-- unknown code %i\n", uAction );
814 SHELL_hWnd = 0; /* just in case */
820 /***********************************************************************
821 * DriveType16 (SHELL.262)
823 UINT16 WINAPI DriveType16( UINT16 drive )
826 char path[] = "A:\\";
828 ret = GetDriveTypeA(path);
829 switch(ret) /* some values are not supported in Win16 */
834 case DRIVE_NO_ROOT_DIR: