- Add proper message reply mechanism and sp player data storage
[wine/wine-kai.git] / dlls / shell32 / shell.c
blob526ba8aa98a3f3db426e2730c8472f2d5fada079
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 "heap.h"
17 #include "ldt.h"
18 #include "module.h"
19 #include "neexe.h"
20 #include "dlgs.h"
21 #include "cursoricon.h"
22 #include "shellapi.h"
23 #include "shlobj.h"
24 #include "debugtools.h"
25 #include "winreg.h"
26 #include "syslevel.h"
27 #include "shlwapi.h"
29 DEFAULT_DEBUG_CHANNEL(shell);
30 DECLARE_DEBUG_CHANNEL(exec);
32 /* .ICO file ICONDIR definitions */
34 #include "pshpack1.h"
36 typedef struct
38 BYTE bWidth; /* Width, in pixels, of the image */
39 BYTE bHeight; /* Height, in pixels, of the image */
40 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
41 BYTE bReserved; /* Reserved ( must be 0) */
42 WORD wPlanes; /* Color Planes */
43 WORD wBitCount; /* Bits per pixel */
44 DWORD dwBytesInRes; /* How many bytes in this resource? */
45 DWORD dwImageOffset; /* Where in the file is this image? */
46 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
48 typedef struct
50 WORD idReserved; /* Reserved (must be 0) */
51 WORD idType; /* Resource Type (1 for icons) */
52 WORD idCount; /* How many images? */
53 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
54 } icoICONDIR, *LPicoICONDIR;
56 #include "poppack.h"
58 typedef struct { /* structure for dropped files */
59 WORD wSize;
60 POINT16 ptMousePos;
61 BOOL16 fInNonClientArea;
62 /* memory block with filenames follows */
63 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
65 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
66 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
67 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
69 static HWND16 SHELL_hWnd = 0;
70 static HHOOK SHELL_hHook = 0;
71 static UINT16 uMsgWndCreated = 0;
72 static UINT16 uMsgWndDestroyed = 0;
73 static UINT16 uMsgShellActivate = 0;
74 HINSTANCE16 SHELL_hInstance = 0;
75 HINSTANCE SHELL_hInstance32;
76 static int SHELL_Attach = 0;
78 /***********************************************************************
79 * SHELL_DllEntryPoint [SHELL.entry]
81 * Initialization code for shell.dll. Automatically loads the
82 * 32-bit shell32.dll to allow thunking up to 32-bit code.
84 * RETURNS:
86 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
87 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
89 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
90 Reason, hInst, ds, HeapSize, res1, res2);
92 switch(Reason)
94 case DLL_PROCESS_ATTACH:
95 SHELL_Attach++;
96 if (SHELL_hInstance)
98 ERR("shell.dll instantiated twice!\n");
100 * We should return FALSE here, but that will break
101 * most apps that use CreateProcess because we do
102 * not yet support seperate address-spaces.
104 return TRUE;
107 SHELL_hInstance = hInst;
108 if(!SHELL_hInstance32)
110 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
112 ERR("Could not load sibling shell32.dll\n");
113 return FALSE;
116 break;
118 case DLL_PROCESS_DETACH:
119 if(!--SHELL_Attach)
121 SHELL_hInstance = 0;
122 if(SHELL_hInstance32)
123 FreeLibrary(SHELL_hInstance32);
125 break;
127 return TRUE;
130 /*************************************************************************
131 * DragAcceptFiles16 [SHELL.9]
133 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
135 DragAcceptFiles(hWnd, b);
138 /*************************************************************************
139 * DragQueryFile16 [SHELL.11]
141 UINT16 WINAPI DragQueryFile16(
142 HDROP16 hDrop,
143 WORD wFile,
144 LPSTR lpszFile,
145 WORD wLength)
147 LPSTR lpDrop;
148 UINT i = 0;
149 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
151 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
153 if(!lpDropFileStruct) goto end;
155 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
156 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
158 while (i++ < wFile)
160 while (*lpDrop++); /* skip filename */
161 if (!*lpDrop)
163 i = (wFile == 0xFFFFFFFF) ? i : 0;
164 goto end;
168 i = strlen(lpDrop);
169 i++;
170 if (!lpszFile ) goto end; /* needed buffer size */
171 i = (wLength > i) ? i : wLength;
172 lstrcpynA (lpszFile, lpDrop, i);
173 end:
174 GlobalUnlock16(hDrop);
175 return i;
178 /*************************************************************************
179 * DragFinish16 [SHELL.12]
181 void WINAPI DragFinish16(HDROP16 h)
183 TRACE("\n");
184 GlobalFree16((HGLOBAL16)h);
188 /*************************************************************************
189 * DragQueryPoint16 [SHELL.13]
191 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
193 LPDROPFILESTRUCT16 lpDropFileStruct;
194 BOOL16 bRet;
195 TRACE("\n");
196 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
198 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
199 bRet = lpDropFileStruct->fInNonClientArea;
201 GlobalUnlock16(hDrop);
202 return bRet;
205 /*************************************************************************
206 * SHELL_FindExecutable [Internal]
208 * Utility for code sharing between FindExecutable and ShellExecute
210 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
211 LPCSTR lpOperation,
212 LPSTR lpResult)
213 { char *extension = NULL; /* pointer to file extension */
214 char tmpext[5]; /* local copy to mung as we please */
215 char filetype[256]; /* registry name for this filetype */
216 LONG filetypelen=256; /* length of above */
217 char command[256]; /* command from registry */
218 LONG commandlen=256; /* This is the most DOS can handle :) */
219 char buffer[256]; /* Used to GetProfileString */
220 HINSTANCE retval=31; /* default - 'No association was found' */
221 char *tok; /* token pointer */
222 int i; /* random counter */
223 char xlpFile[256] = ""; /* result of SearchPath */
225 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
227 lpResult[0]='\0'; /* Start off with an empty return string */
229 /* trap NULL parameters on entry */
230 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
231 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
232 lpFile, lpOperation, lpResult);
233 return 2; /* File not found. Close enough, I guess. */
236 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
237 { TRACE("SearchPathA returned non-zero\n");
238 lpFile = xlpFile;
241 /* First thing we need is the file's extension */
242 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
243 /* File->Run in progman uses */
244 /* .\FILE.EXE :( */
245 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
247 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
248 { WARN("Returning 31 - No association\n");
249 return 31; /* no association */
252 /* Make local copy & lowercase it for reg & 'programs=' lookup */
253 lstrcpynA( tmpext, extension, 5 );
254 CharLowerA( tmpext );
255 TRACE("%s file\n", tmpext);
257 /* Three places to check: */
258 /* 1. win.ini, [windows], programs (NB no leading '.') */
259 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
260 /* 3. win.ini, [extensions], extension (NB no leading '.' */
261 /* All I know of the order is that registry is checked before */
262 /* extensions; however, it'd make sense to check the programs */
263 /* section first, so that's what happens here. */
265 /* See if it's a program - if GetProfileString fails, we skip this
266 * section. Actually, if GetProfileString fails, we've probably
267 * got a lot more to worry about than running a program... */
268 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
269 buffer, sizeof(buffer)) > 0 )
270 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
272 tok = strtok(buffer, " \t"); /* ? */
273 while( tok!= NULL)
275 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
277 strcpy(lpResult, xlpFile);
278 /* Need to perhaps check that the file has a path
279 * attached */
280 TRACE("found %s\n", lpResult);
281 return 33;
283 /* Greater than 32 to indicate success FIXME According to the
284 * docs, I should be returning a handle for the
285 * executable. Does this mean I'm supposed to open the
286 * executable file or something? More RTFM, I guess... */
288 tok=strtok(NULL, " \t");
292 /* Check registry */
293 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
294 &filetypelen ) == ERROR_SUCCESS )
296 filetype[filetypelen]='\0';
297 TRACE("File type: %s\n", filetype);
299 /* Looking for ...buffer\shell\lpOperation\command */
300 strcat( filetype, "\\shell\\" );
301 strcat( filetype, lpOperation );
302 strcat( filetype, "\\command" );
304 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
305 &commandlen ) == ERROR_SUCCESS )
307 LPSTR tmp;
308 char param[256];
309 LONG paramlen = 256;
312 /* Get the parameters needed by the application
313 from the associated ddeexec key */
314 tmp = strstr(filetype,"command");
315 tmp[0] = '\0';
316 strcat(filetype,"ddeexec");
318 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,&paramlen ) == ERROR_SUCCESS)
320 strcat(command," ");
321 strcat(command,param);
322 commandlen += paramlen;
325 /* Is there a replace() function anywhere? */
326 command[commandlen]='\0';
327 strcpy( lpResult, command );
328 tok=strstr( lpResult, "%1" );
329 if (tok != NULL)
331 tok[0]='\0'; /* truncate string at the percent */
332 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
333 tok=strstr( command, "%1" );
334 if ((tok!=NULL) && (strlen(tok)>2))
336 strcat( lpResult, &tok[2] );
339 retval=33; /* FIXME see above */
342 else /* Check win.ini */
344 /* Toss the leading dot */
345 extension++;
346 if ( GetProfileStringA( "extensions", extension, "", command,
347 sizeof(command)) > 0)
349 if (strlen(command)!=0)
351 strcpy( lpResult, command );
352 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
353 if (tok != NULL)
355 tok[0]='\0';
356 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
357 tok=strstr( command, "^" ); /* see above */
358 if ((tok != NULL) && (strlen(tok)>5))
360 strcat( lpResult, &tok[5]);
363 retval=33; /* FIXME - see above */
368 TRACE("returning %s\n", lpResult);
369 return retval;
372 /*************************************************************************
373 * ShellExecute16 [SHELL.20]
375 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
376 LPCSTR lpFile, LPCSTR lpParameters,
377 LPCSTR lpDirectory, INT16 iShowCmd )
378 { HINSTANCE16 retval=31;
379 char old_dir[1024];
380 char cmd[1024] = "";
382 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
383 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
384 lpParameters ? lpParameters : "<null>",
385 lpDirectory ? lpDirectory : "<null>", iShowCmd);
387 if (lpFile==NULL) return 0; /* should not happen */
388 if (lpOperation==NULL) /* default is open */
389 lpOperation="open";
391 if (lpDirectory)
392 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
393 SetCurrentDirectoryA( lpDirectory );
396 /* First try to execute lpFile with lpParameters directly */
397 strcpy(cmd,lpFile);
398 strcat(cmd,lpParameters ? lpParameters : "");
400 retval = WinExec16( cmd, iShowCmd );
402 /* Unable to execute lpFile directly
403 Check if we can match an application to lpFile */
404 if(retval < 32)
406 cmd[0] = '\0';
407 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
409 if (retval > 32) /* Found */
411 if (lpParameters)
413 strcat(cmd," ");
414 strcat(cmd,lpParameters);
416 retval = WinExec16( cmd, iShowCmd );
418 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
420 char lpstrProtocol[256];
421 LONG cmdlen = 512;
422 LPSTR lpstrRes;
423 INT iSize;
425 lpstrRes = strchr(lpFile,':');
426 iSize = lpstrRes - lpFile;
428 /* Looking for ...protocol\shell\lpOperation\command */
429 strncpy(lpstrProtocol,lpFile,iSize);
430 lpstrProtocol[iSize]='\0';
431 strcat( lpstrProtocol, "\\shell\\" );
432 strcat( lpstrProtocol, lpOperation );
433 strcat( lpstrProtocol, "\\command" );
435 /* Remove File Protocol from lpFile */
436 /* In the case file://path/file */
437 if(!strncasecmp(lpFile,"file",iSize))
439 lpFile += iSize;
440 while(*lpFile == ':') lpFile++;
444 /* Get the application for the protocol and execute it */
445 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
446 &cmdlen ) == ERROR_SUCCESS )
448 LPSTR tok;
449 LPSTR tmp;
450 char param[256] = "";
451 LONG paramlen = 256;
453 /* Get the parameters needed by the application
454 from the associated ddeexec key */
455 tmp = strstr(lpstrProtocol,"command");
456 tmp[0] = '\0';
457 strcat(lpstrProtocol,"ddeexec");
459 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,&paramlen ) == ERROR_SUCCESS)
461 strcat(cmd," ");
462 strcat(cmd,param);
463 cmdlen += paramlen;
466 /* Is there a replace() function anywhere? */
467 cmd[cmdlen]='\0';
469 tok=strstr( cmd, "%1" );
470 if (tok != NULL)
472 tok[0]='\0'; /* truncate string at the percent */
473 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
474 tok=strstr( cmd, "%1" );
475 if ((tok!=NULL) && (strlen(tok)>2))
477 strcat( cmd, &tok[2] );
481 retval = WinExec16( cmd, iShowCmd );
484 /* Check if file specified is in the form www.??????.*** */
485 else if(!strncasecmp(lpFile,"www",3))
487 /* if so, append lpFile http:// and call ShellExecute */
488 char lpstrTmpFile[256] = "http://" ;
489 strcat(lpstrTmpFile,lpFile);
490 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
493 if (lpDirectory)
494 SetCurrentDirectoryA( old_dir );
495 return retval;
498 /*************************************************************************
499 * FindExecutable16 (SHELL.21)
501 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
502 LPSTR lpResult )
503 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
507 /*************************************************************************
508 * AboutDlgProc16 (SHELL.33)
510 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
511 LPARAM lParam )
512 { return AboutDlgProc( hWnd, msg, wParam, lParam );
516 /*************************************************************************
517 * ShellAbout16 (SHELL.22)
519 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
520 HICON16 hIcon )
521 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
524 /*************************************************************************
525 * SHELL_GetResourceTable
527 static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
528 { IMAGE_DOS_HEADER mz_header;
529 char magic[4];
530 int size;
532 TRACE("\n");
534 *retptr = NULL;
535 _llseek( hFile, 0, SEEK_SET );
536 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
537 { /* .ICO file ? */
538 if (mz_header.e_cblp == 1)
539 { /* ICONHEADER.idType, must be 1 */
540 *retptr = (LPBYTE)-1;
541 return 1;
543 else
544 return 0; /* failed */
546 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
548 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
549 return 0;
551 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
553 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
554 return IMAGE_NT_SIGNATURE;
556 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
557 { IMAGE_OS2_HEADER ne_header;
558 LPBYTE pTypeInfo = (LPBYTE)-1;
560 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
561 return 0;
563 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
564 return 0;
566 size = ne_header.ne_restab - ne_header.ne_rsrctab;
568 if( size > sizeof(NE_TYPEINFO) )
569 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
570 if( pTypeInfo )
571 { _llseek(hFile, mz_header.e_lfanew+ne_header.ne_rsrctab, SEEK_SET);
572 if( _lread( hFile, (char*)pTypeInfo, size) != size )
573 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
574 pTypeInfo = NULL;
578 *retptr = pTypeInfo;
579 return IMAGE_OS2_SIGNATURE;
581 return 0; /* failed */
584 /*************************************************************************
585 * SHELL_LoadResource
587 static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
588 { BYTE* ptr;
589 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
591 TRACE("\n");
593 if( (ptr = (BYTE*)GlobalLock16( handle )) )
594 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
595 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
596 return handle;
598 return 0;
601 /*************************************************************************
602 * ICO_LoadIcon
604 static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
605 { BYTE* ptr;
606 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
607 TRACE("\n");
608 if( (ptr = (BYTE*)GlobalLock16( handle )) )
609 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
610 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
611 return handle;
613 return 0;
616 /*************************************************************************
617 * ICO_GetIconDirectory
619 * Read .ico file and build phony ICONDIR struct for GetIconID
621 static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIR* lplpiID )
622 { WORD id[3]; /* idReserved, idType, idCount */
623 LPicoICONDIR lpiID;
624 int i;
626 TRACE("\n");
627 _llseek( hFile, 0, SEEK_SET );
628 if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
630 /* check .ICO header
632 * - see http://www.microsoft.com/win32dev/ui/icons.htm
635 if( id[0] || id[1] != 1 || !id[2] ) return 0;
637 i = id[2]*sizeof(icoICONDIRENTRY) ;
639 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, i + sizeof(id));
641 if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
642 { HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10,
643 id[2]*sizeof(CURSORICONDIRENTRY) + sizeof(id) );
644 if( handle )
645 { CURSORICONDIR* lpID = (CURSORICONDIR*)GlobalLock16( handle );
646 lpID->idReserved = lpiID->idReserved = id[0];
647 lpID->idType = lpiID->idType = id[1];
648 lpID->idCount = lpiID->idCount = id[2];
649 for( i=0; i < lpiID->idCount; i++ )
650 { memcpy((void*)(lpID->idEntries + i),
651 (void*)(lpiID->idEntries + i), sizeof(CURSORICONDIRENTRY) - 2);
652 lpID->idEntries[i].wResId = i;
654 *lplpiID = lpiID;
655 return handle;
658 /* fail */
660 HeapFree( GetProcessHeap(), 0, lpiID);
661 return 0;
664 /*************************************************************************
665 * InternalExtractIcon [SHELL.39]
667 * This abortion is called directly by Progman
669 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
670 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
671 { HGLOBAL16 hRet = 0;
672 HGLOBAL16* RetPtr = NULL;
673 LPBYTE pData;
674 OFSTRUCT ofs;
675 DWORD sig;
676 HFILE hFile;
677 UINT16 iconDirCount = 0,iconCount = 0;
678 LPBYTE peimage;
679 HANDLE fmapping;
681 TRACE("(%04x,file %s,start %d,extract %d\n",
682 hInstance, lpszExeFileName, nIconIndex, n);
684 if( !n )
685 return 0;
687 hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
689 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
690 RetPtr = (HICON16*)GlobalLock16(hRet);
692 if (hFile == HFILE_ERROR)
693 { /* not found - load from builtin module if available */
694 HINSTANCE hInst = (HINSTANCE)LoadLibrary16(lpszExeFileName);
696 if (hInst < 32) /* hmm, no Win16 module - try Win32 :-) */
697 hInst = LoadLibraryA(lpszExeFileName);
698 if (hInst)
700 int i;
701 for (i=nIconIndex; i < nIconIndex + n; i++)
702 RetPtr[i-nIconIndex] =
703 (HICON16)LoadIconA(hInst, (LPCSTR)(DWORD)i);
704 FreeLibrary(hInst);
705 return hRet;
707 return 0;
710 *RetPtr = (n == 0xFFFF)? 0 : 1; /* error return values */
712 sig = SHELL_GetResourceTable(hFile,&pData);
714 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
715 { HICON16 hIcon = 0;
716 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
717 NE_NAMEINFO* pIconStorage = NULL;
718 NE_NAMEINFO* pIconDir = NULL;
719 LPicoICONDIR lpiID = NULL;
721 if( pData == (BYTE*)-1 )
722 { hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID); /* check for .ICO file */
723 if( hIcon )
724 { iconDirCount = 1; iconCount = lpiID->idCount;
727 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
728 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
729 { iconDirCount = pTInfo->count;
730 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
731 TRACE("\tfound directory - %i icon families\n", iconDirCount);
733 if( pTInfo->type_id == NE_RSCTYPE_ICON )
734 { iconCount = pTInfo->count;
735 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
736 TRACE("\ttotal icons - %i\n", iconCount);
738 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
741 /* load resources and create icons */
743 if( (pIconStorage && pIconDir) || lpiID )
744 { if( nIconIndex == (UINT16)-1 )
745 { RetPtr[0] = iconDirCount;
747 else if( nIconIndex < iconDirCount )
748 { UINT16 i, icon;
749 if( n > iconDirCount - nIconIndex )
750 n = iconDirCount - nIconIndex;
752 for( i = nIconIndex; i < nIconIndex + n; i++ )
753 { /* .ICO files have only one icon directory */
755 if( lpiID == NULL )
756 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
757 RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
758 GlobalFree16(hIcon);
761 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
762 { hIcon = 0;
763 if( lpiID )
764 { hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
766 else
767 { for( i = 0; i < iconCount; i++ )
768 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
769 { hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,*(WORD*)pData );
773 if( hIcon )
774 { RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
775 FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
777 else
778 { RetPtr[icon-nIconIndex] = 0;
783 if( lpiID )
784 HeapFree( GetProcessHeap(), 0, lpiID);
785 else
786 HeapFree( GetProcessHeap(), 0, pData);
789 if( sig == IMAGE_NT_SIGNATURE)
790 { LPBYTE idata,igdata;
791 PIMAGE_DOS_HEADER dheader;
792 PIMAGE_NT_HEADERS pe_header;
793 PIMAGE_SECTION_HEADER pe_sections;
794 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
795 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
796 int i,j;
797 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
798 CURSORICONDIR **cids;
800 fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
801 if (fmapping == 0)
802 { /* FIXME, INVALID_HANDLE_VALUE? */
803 WARN("failed to create filemap.\n");
804 hRet = 0;
805 goto end_2; /* failure */
807 peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
808 if (!peimage)
809 { WARN("failed to mmap filemap.\n");
810 hRet = 0;
811 goto end_2; /* failure */
813 dheader = (PIMAGE_DOS_HEADER)peimage;
815 /* it is a pe header, SHELL_GetResourceTable checked that */
816 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);
818 /* probably makes problems with short PE headers... but I haven't seen
819 * one yet...
821 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
822 rootresdir = NULL;
824 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
825 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
826 continue;
827 /* FIXME: doesn't work when the resources are not in a seperate section */
828 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
829 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
830 break;
834 if (!rootresdir)
835 { WARN("haven't found section for resource directory.\n");
836 goto end_4; /* failure */
839 icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE);
841 if (!icongroupresdir)
842 { WARN("No Icongroupresourcedirectory!\n");
843 goto end_4; /* failure */
846 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
848 if( nIconIndex == (UINT16)-1 )
849 { RetPtr[0] = iconDirCount;
850 goto end_3; /* success */
853 if (nIconIndex >= iconDirCount)
854 { WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
855 GlobalFree16(hRet);
856 goto end_4; /* failure */
859 cids = (CURSORICONDIR**)HeapAlloc(GetProcessHeap(),0,n*sizeof(CURSORICONDIR*));
861 /* caller just wanted the number of entries */
862 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
864 /* assure we don't get too much ... */
865 if( n > iconDirCount - nIconIndex )
866 { n = iconDirCount - nIconIndex;
869 /* starting from specified index ... */
870 xresent = xresent+nIconIndex;
872 for (i=0;i<n;i++,xresent++)
873 { CURSORICONDIR *cid;
874 PIMAGE_RESOURCE_DIRECTORY resdir;
876 /* go down this resource entry, name */
877 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
879 /* default language (0) */
880 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
881 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
883 /* lookup address in mapped image for virtual address */
884 igdata = NULL;
886 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
887 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
888 continue;
889 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
890 continue;
891 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
894 if (!igdata)
895 { WARN("no matching real address for icongroup!\n");
896 goto end_4; /* failure */
898 /* found */
899 cid = (CURSORICONDIR*)igdata;
900 cids[i] = cid;
901 RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
904 iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE);
906 if (!iconresdir)
907 { WARN("No Iconresourcedirectory!\n");
908 goto end_4; /* failure */
911 for (i=0;i<n;i++)
912 { PIMAGE_RESOURCE_DIRECTORY xresdir;
913 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
914 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
915 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
916 idata = NULL;
918 /* map virtual to address in image */
919 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
920 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
921 continue;
922 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
923 continue;
924 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
926 if (!idata)
927 { WARN("no matching real address found for icondata!\n");
928 RetPtr[i]=0;
929 continue;
931 RetPtr[i] = CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
933 goto end_3; /* success */
935 goto end_1; /* return array with icon handles */
937 /* cleaning up (try & catch would be nicer) */
938 end_4: hRet = 0; /* failure */
939 end_3: UnmapViewOfFile(peimage); /* success */
940 end_2: CloseHandle(fmapping);
941 end_1: _lclose( hFile);
942 return hRet;
945 /*************************************************************************
946 * ExtractIcon16 (SHELL.34)
948 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
949 UINT16 nIconIndex )
950 { TRACE("\n");
951 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
954 /*************************************************************************
955 * ExtractIconEx16 (SHELL.40)
957 HICON16 WINAPI ExtractIconEx16(
958 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
959 HICON16 *phiconSmall, UINT16 nIcons
961 HICON *ilarge,*ismall;
962 UINT16 ret;
963 int i;
965 if (phiconLarge)
966 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
967 else
968 ilarge = NULL;
969 if (phiconSmall)
970 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
971 else
972 ismall = NULL;
973 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
974 if (ilarge) {
975 for (i=0;i<nIcons;i++)
976 phiconLarge[i]=ilarge[i];
977 HeapFree(GetProcessHeap(),0,ilarge);
979 if (ismall) {
980 for (i=0;i<nIcons;i++)
981 phiconSmall[i]=ismall[i];
982 HeapFree(GetProcessHeap(),0,ismall);
984 return ret;
987 /*************************************************************************
988 * ExtractAssociatedIcon [SHELL.36]
990 * Return icon for given file (either from file itself or from associated
991 * executable) and patch parameters if needed.
993 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
994 { HICON16 hIcon;
995 WORD wDummyIcon = 0;
997 TRACE("\n");
999 if(lpiIcon == NULL)
1000 lpiIcon = &wDummyIcon;
1002 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
1004 if( hIcon < 2 )
1005 { if( hIcon == 1 ) /* no icons found in given file */
1006 { char tempPath[0x80];
1007 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
1009 if( uRet > 32 && tempPath[0] )
1010 { strcpy(lpIconPath,tempPath);
1011 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
1012 if( hIcon > 2 )
1013 return hIcon;
1015 else hIcon = 0;
1018 if( hIcon == 1 )
1019 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
1020 else
1021 *lpiIcon = 6; /* generic icon - found nothing */
1023 GetModuleFileName16(hInst, lpIconPath, 0x80);
1024 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
1026 return hIcon;
1029 /*************************************************************************
1030 * ExtractAssociatedIconA
1032 * Return icon for given file (either from file itself or from associated
1033 * executable) and patch parameters if needed.
1035 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
1036 { TRACE("\n");
1037 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
1040 /*************************************************************************
1041 * FindEnvironmentString [SHELL.38]
1043 * Returns a pointer into the DOS environment... Ugh.
1045 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
1046 { UINT16 l;
1048 TRACE("\n");
1050 l = strlen(entry);
1051 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
1052 { if( strncasecmp(lpEnv, entry, l) )
1053 continue;
1054 if( !*(lpEnv+l) )
1055 return (lpEnv + l); /* empty entry */
1056 else if ( *(lpEnv+l)== '=' )
1057 return (lpEnv + l + 1);
1059 return NULL;
1062 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
1063 { SEGPTR spEnv;
1064 LPSTR lpEnv,lpString;
1065 TRACE("\n");
1067 spEnv = GetDOSEnvironment16();
1069 lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
1070 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
1072 if( lpString ) /* offset should be small enough */
1073 return spEnv + (lpString - lpEnv);
1074 return (SEGPTR)NULL;
1077 /*************************************************************************
1078 * DoEnvironmentSubst [SHELL.37]
1080 * Replace %KEYWORD% in the str with the value of variable KEYWORD
1081 * from "DOS" environment.
1083 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
1085 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment16());
1086 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
1087 LPSTR lpstr = str;
1088 LPSTR lpbstr = lpBuffer;
1090 CharToOemA(str,str);
1092 TRACE("accept %s\n", str);
1094 while( *lpstr && lpbstr - lpBuffer < length )
1096 LPSTR lpend = lpstr;
1098 if( *lpstr == '%' )
1100 do { lpend++; } while( *lpend && *lpend != '%' );
1101 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
1103 LPSTR lpKey;
1104 *lpend = '\0';
1105 lpKey = SHELL_FindString(lpEnv, lpstr+1);
1106 if( lpKey ) /* found key value */
1108 int l = strlen(lpKey);
1110 if( l > length - (lpbstr - lpBuffer) - 1 )
1112 WARN("-- Env subst aborted - string too short\n");
1113 *lpend = '%';
1114 break;
1116 strcpy(lpbstr, lpKey);
1117 lpbstr += l;
1119 else break;
1120 *lpend = '%';
1121 lpstr = lpend + 1;
1123 else break; /* back off and whine */
1125 continue;
1128 *lpbstr++ = *lpstr++;
1131 *lpbstr = '\0';
1132 if( lpstr - str == strlen(str) )
1134 strncpy(str, lpBuffer, length);
1135 length = 1;
1137 else
1138 length = 0;
1140 TRACE("-- return %s\n", str);
1142 OemToCharA(str,str);
1143 HeapFree( GetProcessHeap(), 0, lpBuffer);
1145 /* Return str length in the LOWORD
1146 * and 1 in HIWORD if subst was successful.
1148 return (DWORD)MAKELONG(strlen(str), length);
1151 /*************************************************************************
1152 * ShellHookProc [SHELL.103]
1153 * System-wide WH_SHELL hook.
1155 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
1157 TRACE("%i, %04x, %08x\n", code, wParam,
1158 (unsigned)lParam );
1159 if( SHELL_hHook && SHELL_hWnd )
1161 UINT16 uMsg = 0;
1162 switch( code )
1164 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
1165 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
1166 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
1168 PostMessage16( SHELL_hWnd, uMsg, wParam, 0 );
1170 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
1173 /*************************************************************************
1174 * RegisterShellHook [SHELL.102]
1176 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
1178 TRACE("%04x [%u]\n", hWnd, uAction );
1180 switch( uAction )
1182 case 2: /* register hWnd as a shell window */
1183 if( !SHELL_hHook )
1185 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
1186 HOOKPROC16 hookProc = (HOOKPROC16)NE_GetEntryPoint( hShell, 103 );
1187 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
1188 if ( SHELL_hHook )
1190 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
1191 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
1192 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
1194 else
1195 WARN("-- unable to install ShellHookProc()!\n");
1198 if ( SHELL_hHook )
1199 return ((SHELL_hWnd = hWnd) != 0);
1200 break;
1202 default:
1203 WARN("-- unknown code %i\n", uAction );
1204 SHELL_hWnd = 0; /* just in case */
1206 return FALSE;