2 * Shell Library Functions
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "wine/shell16.h"
21 #include "cursoricon.h"
24 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(shell);
30 DECLARE_DEBUG_CHANNEL(exec);
32 /* .ICO file ICONDIR definitions */
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;
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;
58 typedef struct { /* structure for dropped files */
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.
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);
94 case DLL_PROCESS_ATTACH:
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.
107 SHELL_hInstance = hInst;
108 if(!SHELL_hInstance32)
110 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
112 ERR("Could not load sibling shell32.dll\n");
118 case DLL_PROCESS_DETACH:
122 if(SHELL_hInstance32)
123 FreeLibrary(SHELL_hInstance32);
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(
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;
160 while (*lpDrop++); /* skip filename */
163 i = (wFile == 0xFFFFFFFF) ? i : 0;
170 if (!lpszFile ) goto end; /* needed buffer size */
171 i = (wLength > i) ? i : wLength;
172 lstrcpynA (lpszFile, lpDrop, i);
174 GlobalUnlock16(hDrop);
178 /*************************************************************************
179 * DragFinish16 [SHELL.12]
181 void WINAPI DragFinish16(HDROP16 h)
184 GlobalFree16((HGLOBAL16)h);
188 /*************************************************************************
189 * DragQueryPoint16 [SHELL.13]
191 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
193 LPDROPFILESTRUCT16 lpDropFileStruct;
196 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
198 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
199 bRet = lpDropFileStruct->fInNonClientArea;
201 GlobalUnlock16(hDrop);
205 /*************************************************************************
206 * SHELL_FindExecutable [Internal]
208 * Utility for code sharing between FindExecutable and ShellExecute
210 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
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");
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 */
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"); /* ? */
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
280 TRACE("found %s\n", lpResult);
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");
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 )
312 /* Get the parameters needed by the application
313 from the associated ddeexec key */
314 tmp = strstr(filetype,"command");
316 strcat(filetype,"ddeexec");
318 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,¶mlen ) == ERROR_SUCCESS)
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" );
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 */
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? */
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);
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;
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 */
392 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
393 SetCurrentDirectoryA( lpDirectory );
396 /* First try to execute lpFile with lpParameters directly */
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 */
407 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
409 if (retval > 32) /* Found */
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];
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))
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 )
450 char param[256] = "";
453 /* Get the parameters needed by the application
454 from the associated ddeexec key */
455 tmp = strstr(lpstrProtocol,"command");
457 strcat(lpstrProtocol,"ddeexec");
459 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,¶mlen ) == ERROR_SUCCESS)
466 /* Is there a replace() function anywhere? */
469 tok=strstr( cmd, "%1" );
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);
494 SetCurrentDirectoryA( old_dir );
498 /*************************************************************************
499 * FindExecutable16 (SHELL.21)
501 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
503 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
507 /*************************************************************************
508 * AboutDlgProc16 (SHELL.33)
510 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
512 { return AboutDlgProc( hWnd, msg, wParam, lParam );
516 /*************************************************************************
517 * ShellAbout16 (SHELL.22)
519 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
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;
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))
538 if (mz_header.e_cblp == 1)
539 { /* ICONHEADER.idType, must be 1 */
540 *retptr = (LPBYTE)-1;
544 return 0; /* failed */
546 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
548 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
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))
563 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
566 size = ne_header.ne_restab - ne_header.ne_rsrctab;
568 if( size > sizeof(NE_TYPEINFO) )
569 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
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);
579 return IMAGE_OS2_SIGNATURE;
581 return 0; /* failed */
584 /*************************************************************************
587 static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
589 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
593 if( (ptr = (BYTE*)GlobalLock16( handle )) )
594 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
595 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
601 /*************************************************************************
604 static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
606 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
608 if( (ptr = (BYTE*)GlobalLock16( handle )) )
609 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
610 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
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 */
627 _llseek( hFile, 0, SEEK_SET );
628 if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
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) );
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;
660 HeapFree( GetProcessHeap(), 0, lpiID);
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;
676 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
677 UINT16 iconDirCount = 0,iconCount = 0;
681 TRACE("(%04x,file %s,start %d,extract %d\n",
682 hInstance, lpszExeFileName, nIconIndex, n);
684 if( hFile == HFILE_ERROR || !n )
687 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
688 RetPtr = (HICON16*)GlobalLock16(hRet);
690 *RetPtr = (n == 0xFFFF)? 0: 1; /* error return values */
692 sig = SHELL_GetResourceTable(hFile,&pData);
694 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
696 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
697 NE_NAMEINFO* pIconStorage = NULL;
698 NE_NAMEINFO* pIconDir = NULL;
699 LPicoICONDIR lpiID = NULL;
701 if( pData == (BYTE*)-1 )
702 { hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID); /* check for .ICO file */
704 { iconDirCount = 1; iconCount = lpiID->idCount;
707 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
708 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
709 { iconDirCount = pTInfo->count;
710 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
711 TRACE("\tfound directory - %i icon families\n", iconDirCount);
713 if( pTInfo->type_id == NE_RSCTYPE_ICON )
714 { iconCount = pTInfo->count;
715 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
716 TRACE("\ttotal icons - %i\n", iconCount);
718 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
721 /* load resources and create icons */
723 if( (pIconStorage && pIconDir) || lpiID )
724 { if( nIconIndex == (UINT16)-1 )
725 { RetPtr[0] = iconDirCount;
727 else if( nIconIndex < iconDirCount )
729 if( n > iconDirCount - nIconIndex )
730 n = iconDirCount - nIconIndex;
732 for( i = nIconIndex; i < nIconIndex + n; i++ )
733 { /* .ICO files have only one icon directory */
736 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
737 RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
741 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
744 { hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
747 { for( i = 0; i < iconCount; i++ )
748 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
749 { hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,*(WORD*)pData );
754 { RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
755 FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
758 { RetPtr[icon-nIconIndex] = 0;
764 HeapFree( GetProcessHeap(), 0, lpiID);
766 HeapFree( GetProcessHeap(), 0, pData);
769 if( sig == IMAGE_NT_SIGNATURE)
770 { LPBYTE idata,igdata;
771 PIMAGE_DOS_HEADER dheader;
772 PIMAGE_NT_HEADERS pe_header;
773 PIMAGE_SECTION_HEADER pe_sections;
774 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
775 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
777 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
778 CURSORICONDIR **cids;
780 fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
782 { /* FIXME, INVALID_HANDLE_VALUE? */
783 WARN("failed to create filemap.\n");
785 goto end_2; /* failure */
787 peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
789 { WARN("failed to mmap filemap.\n");
791 goto end_2; /* failure */
793 dheader = (PIMAGE_DOS_HEADER)peimage;
795 /* it is a pe header, SHELL_GetResourceTable checked that */
796 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);
798 /* probably makes problems with short PE headers... but I haven't seen
801 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
804 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
805 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
807 /* FIXME: doesn't work when the resources are not in a seperate section */
808 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
809 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
815 { WARN("haven't found section for resource directory.\n");
816 goto end_4; /* failure */
819 icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE);
821 if (!icongroupresdir)
822 { WARN("No Icongroupresourcedirectory!\n");
823 goto end_4; /* failure */
826 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
828 if( nIconIndex == (UINT16)-1 )
829 { RetPtr[0] = iconDirCount;
830 goto end_3; /* success */
833 if (nIconIndex >= iconDirCount)
834 { WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
836 goto end_4; /* failure */
839 cids = (CURSORICONDIR**)HeapAlloc(GetProcessHeap(),0,n*sizeof(CURSORICONDIR*));
841 /* caller just wanted the number of entries */
842 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
844 /* assure we don't get too much ... */
845 if( n > iconDirCount - nIconIndex )
846 { n = iconDirCount - nIconIndex;
849 /* starting from specified index ... */
850 xresent = xresent+nIconIndex;
852 for (i=0;i<n;i++,xresent++)
853 { CURSORICONDIR *cid;
854 PIMAGE_RESOURCE_DIRECTORY resdir;
856 /* go down this resource entry, name */
857 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
859 /* default language (0) */
860 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
861 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
863 /* lookup address in mapped image for virtual address */
866 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
867 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
869 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
871 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
875 { WARN("no matching real address for icongroup!\n");
876 goto end_4; /* failure */
879 cid = (CURSORICONDIR*)igdata;
881 RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
884 iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE);
887 { WARN("No Iconresourcedirectory!\n");
888 goto end_4; /* failure */
892 { PIMAGE_RESOURCE_DIRECTORY xresdir;
893 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
894 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
895 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
898 /* map virtual to address in image */
899 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
900 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
902 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
904 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
907 { WARN("no matching real address found for icondata!\n");
911 RetPtr[i] = CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
913 goto end_3; /* sucess */
915 goto end_1; /* return array with icon handles */
917 /* cleaning up (try & catch would be nicer) */
918 end_4: hRet = 0; /* failure */
919 end_3: UnmapViewOfFile(peimage); /* success */
920 end_2: CloseHandle(fmapping);
921 end_1: _lclose( hFile);
925 /*************************************************************************
926 * ExtractIcon16 (SHELL.34)
928 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
931 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
934 /*************************************************************************
935 * ExtractIconEx16 (SHELL.40)
937 HICON16 WINAPI ExtractIconEx16(
938 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
939 HICON16 *phiconSmall, UINT16 nIcons
941 HICON *ilarge,*ismall;
946 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
950 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
953 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
955 for (i=0;i<nIcons;i++)
956 phiconLarge[i]=ilarge[i];
957 HeapFree(GetProcessHeap(),0,ilarge);
960 for (i=0;i<nIcons;i++)
961 phiconSmall[i]=ismall[i];
962 HeapFree(GetProcessHeap(),0,ismall);
967 /*************************************************************************
968 * ExtractAssociatedIconA
970 * Return icon for given file (either from file itself or from associated
971 * executable) and patch parameters if needed.
973 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
975 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
978 /*************************************************************************
979 * ExtractAssociatedIcon [SHELL.36]
981 * Return icon for given file (either from file itself or from associated
982 * executable) and patch parameters if needed.
984 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
991 lpiIcon = &wDummyIcon;
993 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
996 { if( hIcon == 1 ) /* no icons found in given file */
997 { char tempPath[0x80];
998 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
1000 if( uRet > 32 && tempPath[0] )
1001 { strcpy(lpIconPath,tempPath);
1002 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
1010 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
1012 *lpiIcon = 6; /* generic icon - found nothing */
1014 GetModuleFileName16(hInst, lpIconPath, 0x80);
1015 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
1020 /*************************************************************************
1021 * FindEnvironmentString [SHELL.38]
1023 * Returns a pointer into the DOS environment... Ugh.
1025 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
1031 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
1032 { if( strncasecmp(lpEnv, entry, l) )
1035 return (lpEnv + l); /* empty entry */
1036 else if ( *(lpEnv+l)== '=' )
1037 return (lpEnv + l + 1);
1042 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
1044 LPSTR lpEnv,lpString;
1047 spEnv = GetDOSEnvironment16();
1049 lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
1050 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
1052 if( lpString ) /* offset should be small enough */
1053 return spEnv + (lpString - lpEnv);
1054 return (SEGPTR)NULL;
1057 /*************************************************************************
1058 * DoEnvironmentSubst [SHELL.37]
1060 * Replace %KEYWORD% in the str with the value of variable KEYWORD
1061 * from "DOS" environment.
1063 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
1065 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment16());
1066 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
1068 LPSTR lpbstr = lpBuffer;
1070 CharToOemA(str,str);
1072 TRACE("accept %s\n", str);
1074 while( *lpstr && lpbstr - lpBuffer < length )
1076 LPSTR lpend = lpstr;
1080 do { lpend++; } while( *lpend && *lpend != '%' );
1081 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
1085 lpKey = SHELL_FindString(lpEnv, lpstr+1);
1086 if( lpKey ) /* found key value */
1088 int l = strlen(lpKey);
1090 if( l > length - (lpbstr - lpBuffer) - 1 )
1092 WARN("-- Env subst aborted - string too short\n");
1096 strcpy(lpbstr, lpKey);
1103 else break; /* back off and whine */
1108 *lpbstr++ = *lpstr++;
1112 if( lpstr - str == strlen(str) )
1114 strncpy(str, lpBuffer, length);
1120 TRACE("-- return %s\n", str);
1122 OemToCharA(str,str);
1123 HeapFree( GetProcessHeap(), 0, lpBuffer);
1125 /* Return str length in the LOWORD
1126 * and 1 in HIWORD if subst was successful.
1128 return (DWORD)MAKELONG(strlen(str), length);
1131 /*************************************************************************
1132 * ShellHookProc [SHELL.103]
1133 * System-wide WH_SHELL hook.
1135 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
1137 TRACE("%i, %04x, %08x\n", code, wParam,
1139 if( SHELL_hHook && SHELL_hWnd )
1144 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
1145 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
1146 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
1148 PostMessage16( SHELL_hWnd, uMsg, wParam, 0 );
1150 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
1153 /*************************************************************************
1154 * RegisterShellHook [SHELL.102]
1156 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
1158 TRACE("%04x [%u]\n", hWnd, uAction );
1162 case 2: /* register hWnd as a shell window */
1165 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
1166 HOOKPROC16 hookProc = (HOOKPROC16)NE_GetEntryPoint( hShell, 103 );
1167 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
1170 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
1171 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
1172 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
1175 WARN("-- unable to install ShellHookProc()!\n");
1179 return ((SHELL_hWnd = hWnd) != 0);
1183 WARN("-- unknown code %i\n", uAction );
1184 SHELL_hWnd = 0; /* just in case */