Authors: Aric Stewart <aric@codeweavers.com>, Huw D M Davies <hdavies@codeweavers...
[wine] / dlls / shell32 / shell.c
1 /*
2  *                              Shell Library Functions
3  *
4  *  1998 Marcus Meissner
5  */
6
7 #include "config.h"
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <ctype.h>
13
14 #include "windef.h"
15 #include "winerror.h"
16 #include "winreg.h"
17 #include "dlgs.h"
18 #include "shellapi.h"
19 #include "shlobj.h"
20 #include "shlwapi.h"
21
22 #include "wine/winbase16.h"
23 #include "wine/winuser16.h"
24 #include "shell32_main.h"
25
26 #include "debugtools.h"
27
28 DEFAULT_DEBUG_CHANNEL(shell);
29 DECLARE_DEBUG_CHANNEL(exec);
30
31
32 typedef struct {     /* structure for dropped files */
33  WORD     wSize;
34  POINT16  ptMousePos;
35  BOOL16   fInNonClientArea;
36  /* memory block with filenames follows */
37 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
38
39 static const char*      lpstrMsgWndCreated = "OTHERWINDOWCREATED";
40 static const char*      lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
41 static const char*      lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
42
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;
51
52 /***********************************************************************
53  * DllEntryPoint [SHELL.101]
54  *
55  * Initialization code for shell.dll. Automatically loads the
56  * 32-bit shell32.dll to allow thunking up to 32-bit code.
57  *
58  * RETURNS:
59  */
60 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
61                                 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
62 {
63     TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
64           Reason, hInst, ds, HeapSize, res1, res2);
65
66     switch(Reason)
67     {
68     case DLL_PROCESS_ATTACH:
69         if (SHELL_Attach++) break;
70         SHELL_hInstance = hInst;
71         if(!SHELL_hInstance32)
72         {
73             if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
74             {
75                 ERR("Could not load sibling shell32.dll\n");
76                 return FALSE;
77             }
78         }
79         break;
80
81     case DLL_PROCESS_DETACH:
82         if(!--SHELL_Attach)
83         {
84             SHELL_hInstance = 0;
85             if(SHELL_hInstance32)
86                 FreeLibrary(SHELL_hInstance32);
87         }
88         break;
89     }
90     return TRUE;
91 }
92
93 /*************************************************************************
94  *                              DragAcceptFiles         [SHELL.9]
95  */
96 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
97 {
98   DragAcceptFiles(hWnd, b);
99 }
100
101 /*************************************************************************
102  *                              DragQueryFile           [SHELL.11]
103  */
104 UINT16 WINAPI DragQueryFile16(
105         HDROP16 hDrop,
106         WORD wFile,
107         LPSTR lpszFile,
108         WORD wLength)
109 {
110         LPSTR lpDrop;
111         UINT i = 0;
112         LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop); 
113    
114         TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
115     
116         if(!lpDropFileStruct) goto end;
117     
118         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
119         wFile = (wFile==0xffff) ? 0xffffffff : wFile;
120
121         while (i++ < wFile)
122         {
123           while (*lpDrop++); /* skip filename */
124           if (!*lpDrop) 
125           {
126             i = (wFile == 0xFFFFFFFF) ? i : 0; 
127             goto end;
128           }
129         }
130     
131         i = strlen(lpDrop);
132         i++;
133         if (!lpszFile ) goto end;   /* needed buffer size */
134         i = (wLength > i) ? i : wLength;
135         lstrcpynA (lpszFile,  lpDrop,  i);
136 end:
137         GlobalUnlock16(hDrop);
138         return i;
139 }
140
141 /*************************************************************************
142  *                              DragFinish              [SHELL.12]
143  */
144 void WINAPI DragFinish16(HDROP16 h)
145 {
146     TRACE("\n");
147     GlobalFree16((HGLOBAL16)h);
148 }
149
150
151 /*************************************************************************
152  *                              DragQueryPoint          [SHELL.13]
153  */
154 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
155 {
156   LPDROPFILESTRUCT16 lpDropFileStruct;  
157   BOOL16           bRet;
158   TRACE("\n");
159   lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
160   
161   memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
162   bRet = lpDropFileStruct->fInNonClientArea;
163   
164   GlobalUnlock16(hDrop);
165   return bRet;
166 }
167
168 /*************************************************************************
169  *      SHELL_FindExecutable [Internal]
170  *
171  * Utility for code sharing between FindExecutable and ShellExecute
172  */
173 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile, 
174                                          LPCSTR lpOperation,
175                                          LPSTR lpResult)
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 */
187
188   TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
189
190     lpResult[0]='\0'; /* Start off with an empty return string */
191
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. */
197     }
198
199     if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
200   { TRACE("SearchPathA returned non-zero\n");
201         lpFile = xlpFile;
202     }
203
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 */
207                                         /* .\FILE.EXE :( */
208   TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
209
210     if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
211   { WARN("Returning 31 - No association\n");
212         return 31; /* no association */
213     }
214
215     /* Make local copy & lowercase it for reg & 'programs=' lookup */
216     lstrcpynA( tmpext, extension, 5 );
217     CharLowerA( tmpext );
218   TRACE("%s file\n", tmpext);
219     
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. */
227
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]);
234
235                 tok = strtok(buffer, " \t"); /* ? */
236                 while( tok!= NULL)
237                   {
238                         if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
239                           {
240                                 strcpy(lpResult, xlpFile);
241                                 /* Need to perhaps check that the file has a path
242                                  * attached */
243         TRACE("found %s\n", lpResult);
244         return 33;
245
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... */
250                           }
251                         tok=strtok(NULL, " \t");
252                   }
253           }
254
255     /* Check registry */
256     if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
257                          &filetypelen ) == ERROR_SUCCESS )
258     {
259         filetype[filetypelen]='\0';
260         TRACE("File type: %s\n", filetype);
261
262         /* Looking for ...buffer\shell\lpOperation\command */
263         strcat( filetype, "\\shell\\" );
264         strcat( filetype, lpOperation );
265         strcat( filetype, "\\command" );
266         
267         if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
268                              &commandlen ) == ERROR_SUCCESS )
269         {
270             LPSTR tmp;
271             char param[256];
272             LONG paramlen = 256;
273
274
275             /* Get the parameters needed by the application 
276                from the associated ddeexec key */ 
277             tmp = strstr(filetype,"command");
278             tmp[0] = '\0';
279             strcat(filetype,"ddeexec");
280
281             if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,&paramlen ) == ERROR_SUCCESS)
282             {
283               strcat(command," ");
284               strcat(command,param);
285               commandlen += paramlen;
286             }
287
288             /* Is there a replace() function anywhere? */
289             command[commandlen]='\0';
290             strcpy( lpResult, command );
291             tok=strstr( lpResult, "%1" );
292             if (tok != NULL)
293             {
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))
298                 {
299                     strcat( lpResult, &tok[2] );
300                 }
301             }
302             retval=33; /* FIXME see above */
303         }
304     }
305     else /* Check win.ini */
306     {
307         /* Toss the leading dot */
308         extension++;
309         if ( GetProfileStringA( "extensions", extension, "", command,
310                                   sizeof(command)) > 0)
311           {
312                 if (strlen(command)!=0)
313                   {
314                         strcpy( lpResult, command );
315                         tok=strstr( lpResult, "^" ); /* should be ^.extension? */
316                         if (tok != NULL)
317                           {
318                                 tok[0]='\0';
319                                 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
320                                 tok=strstr( command, "^" ); /* see above */
321                                 if ((tok != NULL) && (strlen(tok)>5))
322                                   {
323                                         strcat( lpResult, &tok[5]);
324                                   }
325                           }
326                         retval=33; /* FIXME - see above */
327                   }
328           }
329         }
330
331     TRACE("returning %s\n", lpResult);
332     return retval;
333 }
334
335 /*************************************************************************
336  *                              ShellExecute            [SHELL.20]
337  */
338 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
339                                    LPCSTR lpFile, LPCSTR lpParameters,
340                                    LPCSTR lpDirectory, INT16 iShowCmd )
341 {   HINSTANCE16 retval=31;
342     char old_dir[1024];
343     char cmd[1024] = "";
344
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);
349
350     if (lpFile==NULL) return 0; /* should not happen */
351     if (lpOperation==NULL) /* default is open */
352       lpOperation="open";
353
354     if (lpDirectory)
355     { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
356         SetCurrentDirectoryA( lpDirectory );
357     }
358
359     /* First try to execute lpFile with lpParameters directly */ 
360     strcpy(cmd,lpFile);
361     strcat(cmd,lpParameters ? lpParameters : "");
362
363     retval = WinExec16( cmd, iShowCmd );
364
365     /* Unable to execute lpFile directly
366        Check if we can match an application to lpFile */
367     if(retval < 32)
368     { 
369       cmd[0] = '\0';
370       retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
371
372       if (retval > 32)  /* Found */
373       {
374         if (lpParameters)
375         {
376             strcat(cmd," ");
377             strcat(cmd,lpParameters);
378         }
379         retval = WinExec16( cmd, iShowCmd );
380       }
381       else if(PathIsURLA((LPSTR)lpFile))    /* File not found, check for URL */
382       {
383         char lpstrProtocol[256];
384         LONG cmdlen = 512;
385         LPSTR lpstrRes;
386         INT iSize;
387       
388         lpstrRes = strchr(lpFile,':');
389         iSize = lpstrRes - lpFile;
390         
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" );
397         
398         /* Remove File Protocol from lpFile */
399         /* In the case file://path/file     */
400         if(!strncasecmp(lpFile,"file",iSize))
401         {
402           lpFile += iSize;
403           while(*lpFile == ':') lpFile++;
404         }
405         
406
407         /* Get the application for the protocol and execute it */
408         if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
409                              &cmdlen ) == ERROR_SUCCESS )
410         {
411             LPSTR tok;
412             LPSTR tmp;
413             char param[256] = "";
414             LONG paramlen = 256;
415
416             /* Get the parameters needed by the application 
417                from the associated ddeexec key */ 
418             tmp = strstr(lpstrProtocol,"command");
419             tmp[0] = '\0';
420             strcat(lpstrProtocol,"ddeexec");
421
422             if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,&paramlen ) == ERROR_SUCCESS)
423             {
424               strcat(cmd," ");
425               strcat(cmd,param);
426               cmdlen += paramlen;
427             }
428             
429             /* Is there a replace() function anywhere? */
430             cmd[cmdlen]='\0';
431
432             tok=strstr( cmd, "%1" );
433             if (tok != NULL)
434             {
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))
439                 {
440                     strcat( cmd, &tok[2] );
441                 }
442             }
443  
444             retval = WinExec16( cmd, iShowCmd );
445         }
446       }
447     /* Check if file specified is in the form www.??????.*** */
448       else if(!strncasecmp(lpFile,"www",3))
449       {
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);
454       }
455     }
456     if (lpDirectory)
457       SetCurrentDirectoryA( old_dir );
458     return retval;
459 }
460
461 /*************************************************************************
462  *             FindExecutable   (SHELL.21)
463  */
464 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
465                                      LPSTR lpResult )
466 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
467 }
468
469
470 /*************************************************************************
471  *             AboutDlgProc   (SHELL.33)
472  */
473 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
474                                LPARAM lParam )
475 { return AboutDlgProc( hWnd, msg, wParam, lParam );
476 }
477
478
479 /*************************************************************************
480  *             ShellAbout   (SHELL.22)
481  */
482 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
483                             HICON16 hIcon )
484 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
485 }
486
487 /*************************************************************************
488  *                      InternalExtractIcon             [SHELL.39]
489  *
490  * This abortion is called directly by Progman
491  */
492 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
493                                      LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
494 {
495     HGLOBAL16 hRet = 0;
496     HICON16 *RetPtr = NULL;
497     OFSTRUCT ofs;
498     HFILE hFile;
499
500         TRACE("(%04x,file %s,start %d,extract %d\n", 
501                        hInstance, lpszExeFileName, nIconIndex, n);
502
503         if( !n )
504           return 0;
505
506         hFile = OpenFile( lpszExeFileName, &ofs, OF_READ|OF_EXIST );
507
508         hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
509         RetPtr = (HICON16*)GlobalLock16(hRet);
510
511         if (hFile == HFILE_ERROR)
512         { /* not found - load from builtin module if available */
513           HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
514
515           if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
516             hInst = LoadLibraryA(lpszExeFileName);
517           if (hInst)
518           {
519             int i;
520             for (i=nIconIndex; i < nIconIndex + n; i++)
521               RetPtr[i-nIconIndex] =
522                       (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
523             FreeLibrary(hInst);
524             return hRet;
525           }
526           GlobalFree16( hRet );
527           return 0;
528         }
529
530         if (nIconIndex == (UINT16)-1)  /* get number of icons */
531         {
532             RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
533         }
534         else
535         {
536             HRESULT res;
537             HICON *icons;
538             icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
539             res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
540                                         GetSystemMetrics(SM_CXICON),
541                                         GetSystemMetrics(SM_CYICON),
542                                         icons, 0, n, 0 );
543             if (!res)
544             {
545                 int i;
546                 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
547             }
548             else
549             {
550                 GlobalFree16( hRet );
551                 hRet = 0;
552             }
553             HeapFree( GetProcessHeap(), 0, icons );
554         }
555         return hRet;
556 }
557
558 /*************************************************************************
559  *             ExtractIcon   (SHELL.34)
560  */
561 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
562         UINT16 nIconIndex )
563 {   TRACE("\n");
564     return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
565 }
566
567 /*************************************************************************
568  *             ExtractIconEx   (SHELL.40)
569  */
570 HICON16 WINAPI ExtractIconEx16(
571         LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
572         HICON16 *phiconSmall, UINT16 nIcons
573 ) {
574     HICON       *ilarge,*ismall;
575     UINT16      ret;
576     int         i;
577
578     if (phiconLarge)
579         ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
580     else
581         ilarge = NULL;
582     if (phiconSmall)
583         ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
584     else
585         ismall = NULL;
586     ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
587     if (ilarge) {
588         for (i=0;i<nIcons;i++)
589             phiconLarge[i]=ilarge[i];
590         HeapFree(GetProcessHeap(),0,ilarge);
591     }
592     if (ismall) {
593         for (i=0;i<nIcons;i++)
594             phiconSmall[i]=ismall[i];
595         HeapFree(GetProcessHeap(),0,ismall);
596     }
597     return ret;
598 }
599
600 /*************************************************************************
601  *                              ExtractAssociatedIcon   [SHELL.36]
602  * 
603  * Return icon for given file (either from file itself or from associated
604  * executable) and patch parameters if needed.
605  */
606 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
607 {       HICON16 hIcon;
608         WORD wDummyIcon = 0;
609
610         TRACE("\n");
611
612         if(lpiIcon == NULL)
613             lpiIcon = &wDummyIcon;
614
615         hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
616
617         if( hIcon < 2 )
618         { if( hIcon == 1 ) /* no icons found in given file */
619           { char  tempPath[0x80];
620             UINT16  uRet = FindExecutable16(lpIconPath,NULL,tempPath);
621
622             if( uRet > 32 && tempPath[0] )
623             { strcpy(lpIconPath,tempPath);
624               hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
625               if( hIcon > 2 ) 
626                 return hIcon;
627             }
628             else hIcon = 0;
629           }
630
631           if( hIcon == 1 ) 
632             *lpiIcon = 2;   /* MSDOS icon - we found .exe but no icons in it */
633           else
634             *lpiIcon = 6;   /* generic icon - found nothing */
635
636           GetModuleFileName16(hInst, lpIconPath, 0x80);
637           hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
638         }
639         return hIcon;
640 }
641
642 /*************************************************************************
643  *                              ExtractAssociatedIconA (SHELL32.@)
644  * 
645  * Return icon for given file (either from file itself or from associated
646  * executable) and patch parameters if needed.
647  */
648 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
649 {       TRACE("\n");
650         return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
651 }
652
653 HICON WINAPI ExtractAssociatedIconExW(DWORD d1, DWORD d2, DWORD d3, DWORD d4)
654 {
655   FIXME("(%lx %lx %lx %lx): stub\n", d1, d2, d3, d4);
656   return 0;
657 }
658
659 /*************************************************************************
660  *                              FindEnvironmentString   [SHELL.38]
661  *
662  * Returns a pointer into the DOS environment... Ugh.
663  */
664 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
665 { UINT16 l;
666
667   TRACE("\n");
668
669   l = strlen(entry); 
670   for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
671   { if( strncasecmp(lpEnv, entry, l) ) 
672       continue;
673         if( !*(lpEnv+l) )
674             return (lpEnv + l);                 /* empty entry */
675         else if ( *(lpEnv+l)== '=' )
676             return (lpEnv + l + 1);
677     }
678     return NULL;
679 }
680
681 /**********************************************************************/
682
683 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
684 { SEGPTR  spEnv;
685   LPSTR lpEnv,lpString;
686   TRACE("\n");
687     
688   spEnv = GetDOSEnvironment16();
689
690   lpEnv = MapSL(spEnv);
691   lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL; 
692
693     if( lpString )              /*  offset should be small enough */
694         return spEnv + (lpString - lpEnv);
695     return (SEGPTR)NULL;
696 }
697
698 /*************************************************************************
699  *                              DoEnvironmentSubst      [SHELL.37]
700  *
701  * Replace %KEYWORD% in the str with the value of variable KEYWORD
702  * from "DOS" environment.
703  */
704 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
705 {
706   LPSTR   lpEnv = MapSL(GetDOSEnvironment16());
707   LPSTR   lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
708   LPSTR   lpstr = str;
709   LPSTR   lpbstr = lpBuffer;
710
711   CharToOemA(str,str);
712
713   TRACE("accept %s\n", str);
714
715   while( *lpstr && lpbstr - lpBuffer < length )
716    {
717      LPSTR lpend = lpstr;
718
719      if( *lpstr == '%' )
720        {
721           do { lpend++; } while( *lpend && *lpend != '%' );
722           if( *lpend == '%' && lpend - lpstr > 1 )      /* found key */
723             {
724                LPSTR lpKey;
725               *lpend = '\0';  
726                lpKey = SHELL_FindString(lpEnv, lpstr+1);
727                if( lpKey )                              /* found key value */
728                  {
729                    int l = strlen(lpKey);
730
731                    if( l > length - (lpbstr - lpBuffer) - 1 )
732                      {
733            WARN("-- Env subst aborted - string too short\n");
734                       *lpend = '%';
735                        break;
736                      }
737                    strcpy(lpbstr, lpKey);
738                    lpbstr += l;
739                  }
740                else break;
741               *lpend = '%';
742                lpstr = lpend + 1;
743             }
744           else break;                                   /* back off and whine */
745
746           continue;
747        } 
748
749      *lpbstr++ = *lpstr++;
750    }
751
752  *lpbstr = '\0';
753   if( lpstr - str == strlen(str) )
754     {
755       strncpy(str, lpBuffer, length);
756       length = 1;
757     }
758   else
759       length = 0;
760
761   TRACE("-- return %s\n", str);
762
763   OemToCharA(str,str);
764   HeapFree( GetProcessHeap(), 0, lpBuffer);
765
766   /*  Return str length in the LOWORD
767    *  and 1 in HIWORD if subst was successful.
768    */
769  return (DWORD)MAKELONG(strlen(str), length);
770 }
771
772 /*************************************************************************
773  *                              ShellHookProc           [SHELL.103]
774  * System-wide WH_SHELL hook.
775  */
776 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
777 {
778     TRACE("%i, %04x, %08x\n", code, wParam, 
779                                                       (unsigned)lParam );
780     if( SHELL_hHook && SHELL_hWnd )
781     {
782         UINT16  uMsg = 0;
783         switch( code )
784         {
785             case HSHELL_WINDOWCREATED:          uMsg = uMsgWndCreated;   break;
786             case HSHELL_WINDOWDESTROYED:        uMsg = uMsgWndDestroyed; break;
787             case HSHELL_ACTIVATESHELLWINDOW:    uMsg = uMsgShellActivate;
788         }
789         PostMessageA( SHELL_hWnd, uMsg, wParam, 0 );
790     }
791     return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
792 }
793
794 /*************************************************************************
795  *                              RegisterShellHook       [SHELL.102]
796  */
797 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
798
799     TRACE("%04x [%u]\n", hWnd, uAction );
800
801     switch( uAction )
802     { 
803     case 2:  /* register hWnd as a shell window */
804         if( !SHELL_hHook )
805         { 
806             HMODULE16 hShell = GetModuleHandle16( "SHELL" );
807             HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
808             SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
809             if ( SHELL_hHook )
810             { 
811                 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
812                 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
813                 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
814             } 
815             else 
816                 WARN("-- unable to install ShellHookProc()!\n");
817         }
818
819         if ( SHELL_hHook )
820             return ((SHELL_hWnd = hWnd) != 0);
821         break;
822
823     default:
824         WARN("-- unknown code %i\n", uAction );
825         SHELL_hWnd = 0; /* just in case */
826     }
827     return FALSE;
828 }
829
830
831 /***********************************************************************
832  *           DriveType   (SHELL.262)
833  */
834 UINT16 WINAPI DriveType16( UINT16 drive )
835 {
836     UINT ret;
837     char path[] = "A:\\";
838     path[0] += drive;
839     ret = GetDriveTypeA(path);
840     switch(ret)  /* some values are not supported in Win16 */
841     {
842     case DRIVE_CDROM:
843         ret = DRIVE_REMOTE;
844         break;
845     case DRIVE_NO_ROOT_DIR:
846         ret = DRIVE_UNKNOWN;
847         break;
848     }
849     return ret;
850 }