Cleaned up dll startup routines now that we have separate address
[wine/multimedia.git] / dlls / shell32 / shell.c
blob4402b1fdd42291b84f972fc9aadbf6e7e2cfa343
1 /*
2 * Shell Library Functions
4 * 1998 Marcus Meissner
5 */
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <ctype.h>
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "wine/shell16.h"
15 #include "winerror.h"
16 #include "dlgs.h"
17 #include "shellapi.h"
18 #include "shlobj.h"
19 #include "debugtools.h"
20 #include "winreg.h"
21 #include "shlwapi.h"
23 DEFAULT_DEBUG_CHANNEL(shell);
24 DECLARE_DEBUG_CHANNEL(exec);
27 typedef struct { /* structure for dropped files */
28 WORD wSize;
29 POINT16 ptMousePos;
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.
53 * RETURNS:
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);
61 switch(Reason)
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");
71 return FALSE;
74 break;
76 case DLL_PROCESS_DETACH:
77 if(!--SHELL_Attach)
79 SHELL_hInstance = 0;
80 if(SHELL_hInstance32)
81 FreeLibrary(SHELL_hInstance32);
83 break;
85 return TRUE;
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(
100 HDROP16 hDrop,
101 WORD wFile,
102 LPSTR lpszFile,
103 WORD wLength)
105 LPSTR lpDrop;
106 UINT i = 0;
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;
116 while (i++ < wFile)
118 while (*lpDrop++); /* skip filename */
119 if (!*lpDrop)
121 i = (wFile == 0xFFFFFFFF) ? i : 0;
122 goto end;
126 i = strlen(lpDrop);
127 i++;
128 if (!lpszFile ) goto end; /* needed buffer size */
129 i = (wLength > i) ? i : wLength;
130 lstrcpynA (lpszFile, lpDrop, i);
131 end:
132 GlobalUnlock16(hDrop);
133 return i;
136 /*************************************************************************
137 * DragFinish16 [SHELL.12]
139 void WINAPI DragFinish16(HDROP16 h)
141 TRACE("\n");
142 GlobalFree16((HGLOBAL16)h);
146 /*************************************************************************
147 * DragQueryPoint16 [SHELL.13]
149 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
151 LPDROPFILESTRUCT16 lpDropFileStruct;
152 BOOL16 bRet;
153 TRACE("\n");
154 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
156 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
157 bRet = lpDropFileStruct->fInNonClientArea;
159 GlobalUnlock16(hDrop);
160 return bRet;
163 /*************************************************************************
164 * SHELL_FindExecutable [Internal]
166 * Utility for code sharing between FindExecutable and ShellExecute
168 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
169 LPCSTR lpOperation,
170 LPSTR lpResult)
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");
196 lpFile = xlpFile;
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 */
202 /* .\FILE.EXE :( */
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"); /* ? */
231 while( tok!= NULL)
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
237 * attached */
238 TRACE("found %s\n", lpResult);
239 return 33;
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");
250 /* Check registry */
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 )
265 LPSTR tmp;
266 char param[256];
267 LONG paramlen = 256;
270 /* Get the parameters needed by the application
271 from the associated ddeexec key */
272 tmp = strstr(filetype,"command");
273 tmp[0] = '\0';
274 strcat(filetype,"ddeexec");
276 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,&paramlen ) == ERROR_SUCCESS)
278 strcat(command," ");
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" );
287 if (tok != NULL)
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 */
303 extension++;
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? */
311 if (tok != NULL)
313 tok[0]='\0';
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);
327 return retval;
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;
337 char old_dir[1024];
338 char cmd[1024] = "";
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 */
347 lpOperation="open";
349 if (lpDirectory)
350 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
351 SetCurrentDirectoryA( lpDirectory );
354 /* First try to execute lpFile with lpParameters directly */
355 strcpy(cmd,lpFile);
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 */
362 if(retval < 32)
364 cmd[0] = '\0';
365 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
367 if (retval > 32) /* Found */
369 if (lpParameters)
371 strcat(cmd," ");
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];
379 LONG cmdlen = 512;
380 LPSTR lpstrRes;
381 INT iSize;
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))
397 lpFile += 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 )
406 LPSTR tok;
407 LPSTR tmp;
408 char param[256] = "";
409 LONG paramlen = 256;
411 /* Get the parameters needed by the application
412 from the associated ddeexec key */
413 tmp = strstr(lpstrProtocol,"command");
414 tmp[0] = '\0';
415 strcat(lpstrProtocol,"ddeexec");
417 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,&paramlen ) == ERROR_SUCCESS)
419 strcat(cmd," ");
420 strcat(cmd,param);
421 cmdlen += paramlen;
424 /* Is there a replace() function anywhere? */
425 cmd[cmdlen]='\0';
427 tok=strstr( cmd, "%1" );
428 if (tok != NULL)
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);
451 if (lpDirectory)
452 SetCurrentDirectoryA( old_dir );
453 return retval;
456 /*************************************************************************
457 * FindExecutable16 (SHELL.21)
459 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
460 LPSTR lpResult )
461 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
465 /*************************************************************************
466 * AboutDlgProc16 (SHELL.33)
468 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
469 LPARAM lParam )
470 { return AboutDlgProc( hWnd, msg, wParam, lParam );
474 /*************************************************************************
475 * ShellAbout16 (SHELL.22)
477 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
478 HICON16 hIcon )
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 )
490 HGLOBAL16 hRet = 0;
491 HICON16 *RetPtr = NULL;
492 OFSTRUCT ofs;
493 HFILE hFile;
495 TRACE("(%04x,file %s,start %d,extract %d\n",
496 hInstance, lpszExeFileName, nIconIndex, n);
498 if( !n )
499 return 0;
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);
512 if (hInst)
514 int i;
515 for (i=nIconIndex; i < nIconIndex + n; i++)
516 RetPtr[i-nIconIndex] =
517 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
518 FreeLibrary(hInst);
519 return hRet;
521 GlobalFree16( hRet );
522 return 0;
525 if (nIconIndex == (UINT16)-1) /* get number of icons */
527 RetPtr[0] = PrivateExtractIconsA( ofs.szPathName, -1, 0, 0, NULL, 0, 0, 0 );
529 else
531 HRESULT res;
532 HICON *icons;
533 icons = HeapAlloc( GetProcessHeap(), 0, n * sizeof(*icons) );
534 res = PrivateExtractIconsA( ofs.szPathName, nIconIndex,
535 GetSystemMetrics(SM_CXICON),
536 GetSystemMetrics(SM_CYICON),
537 icons, 0, n, 0 );
538 if (!res)
540 int i;
541 for (i = 0; i < n; i++) RetPtr[i] = (HICON16)icons[i];
543 else
545 GlobalFree16( hRet );
546 hRet = 0;
548 HeapFree( GetProcessHeap(), 0, icons );
550 return hRet;
553 /*************************************************************************
554 * ExtractIcon16 (SHELL.34)
556 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
557 UINT16 nIconIndex )
558 { TRACE("\n");
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;
570 UINT16 ret;
571 int i;
573 if (phiconLarge)
574 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
575 else
576 ilarge = NULL;
577 if (phiconSmall)
578 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
579 else
580 ismall = NULL;
581 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
582 if (ilarge) {
583 for (i=0;i<nIcons;i++)
584 phiconLarge[i]=ilarge[i];
585 HeapFree(GetProcessHeap(),0,ilarge);
587 if (ismall) {
588 for (i=0;i<nIcons;i++)
589 phiconSmall[i]=ismall[i];
590 HeapFree(GetProcessHeap(),0,ismall);
592 return ret;
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)
602 { HICON16 hIcon;
603 WORD wDummyIcon = 0;
605 TRACE("\n");
607 if(lpiIcon == NULL)
608 lpiIcon = &wDummyIcon;
610 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
612 if( hIcon < 2 )
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);
620 if( hIcon > 2 )
621 return hIcon;
623 else hIcon = 0;
626 if( hIcon == 1 )
627 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
628 else
629 *lpiIcon = 6; /* generic icon - found nothing */
631 GetModuleFileName16(hInst, lpIconPath, 0x80);
632 hIcon = LoadIconA( hInst, MAKEINTRESOURCEA(*lpiIcon));
634 return hIcon;
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)
644 { TRACE("\n");
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)
654 { UINT16 l;
656 TRACE("\n");
658 l = strlen(entry);
659 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
660 { if( strncasecmp(lpEnv, entry, l) )
661 continue;
662 if( !*(lpEnv+l) )
663 return (lpEnv + l); /* empty entry */
664 else if ( *(lpEnv+l)== '=' )
665 return (lpEnv + l + 1);
667 return NULL;
670 /**********************************************************************/
672 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
673 { SEGPTR spEnv;
674 LPSTR lpEnv,lpString;
675 TRACE("\n");
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);
684 return (SEGPTR)NULL;
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);
697 LPSTR lpstr = str;
698 LPSTR lpbstr = lpBuffer;
700 CharToOemA(str,str);
702 TRACE("accept %s\n", str);
704 while( *lpstr && lpbstr - lpBuffer < length )
706 LPSTR lpend = lpstr;
708 if( *lpstr == '%' )
710 do { lpend++; } while( *lpend && *lpend != '%' );
711 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
713 LPSTR lpKey;
714 *lpend = '\0';
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");
723 *lpend = '%';
724 break;
726 strcpy(lpbstr, lpKey);
727 lpbstr += l;
729 else break;
730 *lpend = '%';
731 lpstr = lpend + 1;
733 else break; /* back off and whine */
735 continue;
738 *lpbstr++ = *lpstr++;
741 *lpbstr = '\0';
742 if( lpstr - str == strlen(str) )
744 strncpy(str, lpBuffer, length);
745 length = 1;
747 else
748 length = 0;
750 TRACE("-- return %s\n", str);
752 OemToCharA(str,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,
768 (unsigned)lParam );
769 if( SHELL_hHook && SHELL_hWnd )
771 UINT16 uMsg = 0;
772 switch( code )
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 );
790 switch( uAction )
792 case 2: /* register hWnd as a shell window */
793 if( !SHELL_hHook )
795 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
796 HOOKPROC16 hookProc = (HOOKPROC16)GetProcAddress16( hShell, (LPCSTR)103 );
797 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
798 if ( SHELL_hHook )
800 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
801 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
802 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
804 else
805 WARN("-- unable to install ShellHookProc()!\n");
808 if ( SHELL_hHook )
809 return ((SHELL_hWnd = hWnd) != 0);
810 break;
812 default:
813 WARN("-- unknown code %i\n", uAction );
814 SHELL_hWnd = 0; /* just in case */
816 return FALSE;
820 /***********************************************************************
821 * DriveType16 (SHELL.262)
823 UINT16 WINAPI DriveType16( UINT16 drive )
825 UINT ret;
826 char path[] = "A:\\";
827 path[0] += drive;
828 ret = GetDriveTypeA(path);
829 switch(ret) /* some values are not supported in Win16 */
831 case DRIVE_CDROM:
832 ret = DRIVE_REMOTE;
833 break;
834 case DRIVE_NO_ROOT_DIR:
835 ret = DRIVE_UNKNOWN;
836 break;
838 return ret;