2 * Shell Library Functions
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "wine/shell16.h"
22 #include "cursoricon.h"
25 #include "debugtools.h"
29 #include "imagelist.h"
31 DEFAULT_DEBUG_CHANNEL(shell)
32 DECLARE_DEBUG_CHANNEL(exec)
34 /* .ICO file ICONDIR definitions */
40 BYTE bWidth; /* Width, in pixels, of the image */
41 BYTE bHeight; /* Height, in pixels, of the image */
42 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
43 BYTE bReserved; /* Reserved ( must be 0) */
44 WORD wPlanes; /* Color Planes */
45 WORD wBitCount; /* Bits per pixel */
46 DWORD dwBytesInRes; /* How many bytes in this resource? */
47 DWORD dwImageOffset; /* Where in the file is this image? */
48 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
52 WORD idReserved; /* Reserved (must be 0) */
53 WORD idType; /* Resource Type (1 for icons) */
54 WORD idCount; /* How many images? */
55 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
56 } icoICONDIR, *LPicoICONDIR;
60 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
61 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
62 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
64 static HWND16 SHELL_hWnd = 0;
65 static HHOOK SHELL_hHook = 0;
66 static UINT16 uMsgWndCreated = 0;
67 static UINT16 uMsgWndDestroyed = 0;
68 static UINT16 uMsgShellActivate = 0;
69 HINSTANCE16 SHELL_hInstance = 0;
70 HINSTANCE SHELL_hInstance32;
71 static int SHELL_Attach = 0;
73 /***********************************************************************
74 * SHELL_DllEntryPoint [SHELL.entry]
76 * Initialization code for shell.dll. Automatically loads the
77 * 32-bit shell32.dll to allow thunking up to 32-bit code.
81 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
82 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
84 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
85 Reason, hInst, ds, HeapSize, res1, res2);
89 case DLL_PROCESS_ATTACH:
93 ERR("shell.dll instantiated twice!\n");
95 * We should return FALSE here, but that will break
96 * most apps that use CreateProcess because we do
97 * not yet support seperate address-spaces.
102 SHELL_hInstance = hInst;
103 if(!SHELL_hInstance32)
105 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
107 ERR("Could not load sibling shell32.dll\n");
113 case DLL_PROCESS_DETACH:
117 if(SHELL_hInstance32)
118 FreeLibrary(SHELL_hInstance32);
125 /*************************************************************************
126 * DragAcceptFiles16 [SHELL.9]
128 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
130 DragAcceptFiles(hWnd, b);
133 /*************************************************************************
134 * DragQueryFile16 [SHELL.11]
136 UINT16 WINAPI DragQueryFile16(
144 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
146 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
148 if(!lpDropFileStruct) goto end;
150 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
151 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
155 while (*lpDrop++); /* skip filename */
158 i = (wFile == 0xFFFFFFFF) ? i : 0;
163 i = lstrlenA(lpDrop);
165 if (!lpszFile ) goto end; /* needed buffer size */
166 i = (wLength > i) ? i : wLength;
167 lstrcpynA (lpszFile, lpDrop, i);
169 GlobalUnlock16(hDrop);
173 /*************************************************************************
174 * DragFinish16 [SHELL.12]
176 void WINAPI DragFinish16(HDROP16 h)
179 GlobalFree16((HGLOBAL16)h);
183 /*************************************************************************
184 * DragQueryPoint16 [SHELL.13]
186 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
188 LPDROPFILESTRUCT16 lpDropFileStruct;
191 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
193 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
194 bRet = lpDropFileStruct->fInNonClientArea;
196 GlobalUnlock16(hDrop);
200 /*************************************************************************
201 * SHELL_FindExecutable [Internal]
203 * Utility for code sharing between FindExecutable and ShellExecute
205 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
208 { char *extension = NULL; /* pointer to file extension */
209 char tmpext[5]; /* local copy to mung as we please */
210 char filetype[256]; /* registry name for this filetype */
211 LONG filetypelen=256; /* length of above */
212 char command[256]; /* command from registry */
213 LONG commandlen=256; /* This is the most DOS can handle :) */
214 char buffer[256]; /* Used to GetProfileString */
215 HINSTANCE retval=31; /* default - 'No association was found' */
216 char *tok; /* token pointer */
217 int i; /* random counter */
218 char xlpFile[256] = ""; /* result of SearchPath */
220 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
222 lpResult[0]='\0'; /* Start off with an empty return string */
224 /* trap NULL parameters on entry */
225 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
226 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
227 lpFile, lpOperation, lpResult);
228 return 2; /* File not found. Close enough, I guess. */
231 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
232 { TRACE("SearchPathA returned non-zero\n");
236 /* First thing we need is the file's extension */
237 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
238 /* File->Run in progman uses */
240 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
242 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
243 { WARN("Returning 31 - No association\n");
244 return 31; /* no association */
247 /* Make local copy & lowercase it for reg & 'programs=' lookup */
248 lstrcpynA( tmpext, extension, 5 );
249 CharLowerA( tmpext );
250 TRACE("%s file\n", tmpext);
252 /* Three places to check: */
253 /* 1. win.ini, [windows], programs (NB no leading '.') */
254 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
255 /* 3. win.ini, [extensions], extension (NB no leading '.' */
256 /* All I know of the order is that registry is checked before */
257 /* extensions; however, it'd make sense to check the programs */
258 /* section first, so that's what happens here. */
260 /* See if it's a program - if GetProfileString fails, we skip this
261 * section. Actually, if GetProfileString fails, we've probably
262 * got a lot more to worry about than running a program... */
263 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
264 buffer, sizeof(buffer)) > 0 )
265 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
267 tok = strtok(buffer, " \t"); /* ? */
270 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
272 strcpy(lpResult, xlpFile);
273 /* Need to perhaps check that the file has a path
275 TRACE("found %s\n", lpResult);
278 /* Greater than 32 to indicate success FIXME According to the
279 * docs, I should be returning a handle for the
280 * executable. Does this mean I'm supposed to open the
281 * executable file or something? More RTFM, I guess... */
283 tok=strtok(NULL, " \t");
288 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
289 &filetypelen ) == ERROR_SUCCESS )
291 filetype[filetypelen]='\0';
292 TRACE("File type: %s\n", filetype);
294 /* Looking for ...buffer\shell\lpOperation\command */
295 strcat( filetype, "\\shell\\" );
296 strcat( filetype, lpOperation );
297 strcat( filetype, "\\command" );
299 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
300 &commandlen ) == ERROR_SUCCESS )
307 /* Get the parameters needed by the application
308 from the associated ddeexec key */
309 tmp = strstr(filetype,"command");
311 strcat(filetype,"ddeexec");
313 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,¶mlen ) == ERROR_SUCCESS)
316 strcat(command,param);
317 commandlen += paramlen;
320 /* Is there a replace() function anywhere? */
321 command[commandlen]='\0';
322 strcpy( lpResult, command );
323 tok=strstr( lpResult, "%1" );
326 tok[0]='\0'; /* truncate string at the percent */
327 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
328 tok=strstr( command, "%1" );
329 if ((tok!=NULL) && (strlen(tok)>2))
331 strcat( lpResult, &tok[2] );
334 retval=33; /* FIXME see above */
337 else /* Check win.ini */
339 /* Toss the leading dot */
341 if ( GetProfileStringA( "extensions", extension, "", command,
342 sizeof(command)) > 0)
344 if (strlen(command)!=0)
346 strcpy( lpResult, command );
347 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
351 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
352 tok=strstr( command, "^" ); /* see above */
353 if ((tok != NULL) && (strlen(tok)>5))
355 strcat( lpResult, &tok[5]);
358 retval=33; /* FIXME - see above */
363 TRACE("returning %s\n", lpResult);
367 /*************************************************************************
368 * ShellExecute16 [SHELL.20]
370 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
371 LPCSTR lpFile, LPCSTR lpParameters,
372 LPCSTR lpDirectory, INT16 iShowCmd )
373 { HINSTANCE16 retval=31;
377 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
378 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
379 lpParameters ? lpParameters : "<null>",
380 lpDirectory ? lpDirectory : "<null>", iShowCmd);
382 if (lpFile==NULL) return 0; /* should not happen */
383 if (lpOperation==NULL) /* default is open */
387 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
388 SetCurrentDirectoryA( lpDirectory );
391 /* First try to execute lpFile with lpParameters directly */
393 strcat(cmd,lpParameters ? lpParameters : "");
395 SYSLEVEL_ReleaseWin16Lock();
396 retval = WinExec( cmd, iShowCmd );
397 SYSLEVEL_RestoreWin16Lock();
399 /* Unable to execute lpFile directly
400 Check if we can match an application to lpFile */
404 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
406 if (retval > 32) /* Found */
411 strcat(cmd,lpParameters);
413 SYSLEVEL_ReleaseWin16Lock();
414 retval = WinExec( cmd, iShowCmd );
415 SYSLEVEL_RestoreWin16Lock();
417 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
419 char lpstrProtocol[256];
424 lpstrRes = strchr(lpFile,':');
425 iSize = lpstrRes - lpFile;
427 /* Looking for ...protocol\shell\lpOperation\command */
428 strncpy(lpstrProtocol,lpFile,iSize);
429 lpstrProtocol[iSize]='\0';
430 strcat( lpstrProtocol, "\\shell\\" );
431 strcat( lpstrProtocol, lpOperation );
432 strcat( lpstrProtocol, "\\command" );
434 /* Remove File Protocol from lpFile */
435 /* In the case file://path/file */
436 if(!strncasecmp(lpFile,"file",iSize))
439 while(*lpFile == ':') lpFile++;
443 /* Get the application for the protocol and execute it */
444 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
445 &cmdlen ) == ERROR_SUCCESS )
449 char param[256] = "";
452 /* Get the parameters needed by the application
453 from the associated ddeexec key */
454 tmp = strstr(lpstrProtocol,"command");
456 strcat(lpstrProtocol,"ddeexec");
458 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,¶mlen ) == ERROR_SUCCESS)
465 /* Is there a replace() function anywhere? */
468 tok=strstr( cmd, "%1" );
471 tok[0]='\0'; /* truncate string at the percent */
472 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
473 tok=strstr( cmd, "%1" );
474 if ((tok!=NULL) && (strlen(tok)>2))
476 strcat( cmd, &tok[2] );
480 SYSLEVEL_ReleaseWin16Lock();
481 retval = WinExec( cmd, iShowCmd );
482 SYSLEVEL_RestoreWin16Lock();
485 /* Check if file specified is in the form www.??????.*** */
486 else if(!strncasecmp(lpFile,"www",3))
488 /* if so, append lpFile http:// and call ShellExecute */
489 char lpstrTmpFile[256] = "http://" ;
490 strcat(lpstrTmpFile,lpFile);
491 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
495 SetCurrentDirectoryA( old_dir );
499 /*************************************************************************
500 * FindExecutable16 (SHELL.21)
502 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
504 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
508 /*************************************************************************
509 * AboutDlgProc16 (SHELL.33)
511 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
513 { return AboutDlgProc( hWnd, msg, wParam, lParam );
517 /*************************************************************************
518 * ShellAbout16 (SHELL.22)
520 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
522 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
525 /*************************************************************************
526 * SHELL_GetResourceTable
528 static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
529 { IMAGE_DOS_HEADER mz_header;
536 _llseek( hFile, 0, SEEK_SET );
537 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
539 if (mz_header.e_cblp == 1)
540 { /* ICONHEADER.idType, must be 1 */
541 *retptr = (LPBYTE)-1;
545 return 0; /* failed */
547 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
549 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
552 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
554 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
555 return IMAGE_NT_SIGNATURE;
557 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
558 { IMAGE_OS2_HEADER ne_header;
559 LPBYTE pTypeInfo = (LPBYTE)-1;
561 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
564 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
567 size = ne_header.ne_restab - ne_header.ne_rsrctab;
569 if( size > sizeof(NE_TYPEINFO) )
570 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
572 { _llseek(hFile, mz_header.e_lfanew+ne_header.ne_rsrctab, SEEK_SET);
573 if( _lread( hFile, (char*)pTypeInfo, size) != size )
574 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
580 return IMAGE_OS2_SIGNATURE;
582 return 0; /* failed */
585 /*************************************************************************
588 static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
590 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
594 if( (ptr = (BYTE*)GlobalLock16( handle )) )
595 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
596 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
602 /*************************************************************************
605 static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
607 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
609 if( (ptr = (BYTE*)GlobalLock16( handle )) )
610 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
611 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
617 /*************************************************************************
618 * ICO_GetIconDirectory
620 * Read .ico file and build phony ICONDIR struct for GetIconID
622 static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIR* lplpiID )
623 { WORD id[3]; /* idReserved, idType, idCount */
628 _llseek( hFile, 0, SEEK_SET );
629 if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
633 * - see http://www.microsoft.com/win32dev/ui/icons.htm
636 if( id[0] || id[1] != 1 || !id[2] ) return 0;
638 i = id[2]*sizeof(icoICONDIRENTRY) ;
640 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, i + sizeof(id));
642 if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
643 { HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10,
644 id[2]*sizeof(CURSORICONDIRENTRY) + sizeof(id) );
646 { CURSORICONDIR* lpID = (CURSORICONDIR*)GlobalLock16( handle );
647 lpID->idReserved = lpiID->idReserved = id[0];
648 lpID->idType = lpiID->idType = id[1];
649 lpID->idCount = lpiID->idCount = id[2];
650 for( i=0; i < lpiID->idCount; i++ )
651 { memcpy((void*)(lpID->idEntries + i),
652 (void*)(lpiID->idEntries + i), sizeof(CURSORICONDIRENTRY) - 2);
653 lpID->idEntries[i].wResId = i;
661 HeapFree( GetProcessHeap(), 0, lpiID);
665 /*************************************************************************
666 * InternalExtractIcon [SHELL.39]
668 * This abortion is called directly by Progman
670 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
671 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
672 { HGLOBAL16 hRet = 0;
673 HGLOBAL16* RetPtr = NULL;
677 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
678 UINT16 iconDirCount = 0,iconCount = 0;
682 TRACE("(%04x,file %s,start %d,extract %d\n",
683 hInstance, lpszExeFileName, nIconIndex, n);
685 if( hFile == HFILE_ERROR || !n )
688 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
689 RetPtr = (HICON16*)GlobalLock16(hRet);
691 *RetPtr = (n == 0xFFFF)? 0: 1; /* error return values */
693 sig = SHELL_GetResourceTable(hFile,&pData);
695 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
697 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
698 NE_NAMEINFO* pIconStorage = NULL;
699 NE_NAMEINFO* pIconDir = NULL;
700 LPicoICONDIR lpiID = NULL;
702 if( pData == (BYTE*)-1 )
703 { hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID); /* check for .ICO file */
705 { iconDirCount = 1; iconCount = lpiID->idCount;
708 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
709 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
710 { iconDirCount = pTInfo->count;
711 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
712 TRACE("\tfound directory - %i icon families\n", iconDirCount);
714 if( pTInfo->type_id == NE_RSCTYPE_ICON )
715 { iconCount = pTInfo->count;
716 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
717 TRACE("\ttotal icons - %i\n", iconCount);
719 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
722 /* load resources and create icons */
724 if( (pIconStorage && pIconDir) || lpiID )
725 { if( nIconIndex == (UINT16)-1 )
726 { RetPtr[0] = iconDirCount;
728 else if( nIconIndex < iconDirCount )
730 if( n > iconDirCount - nIconIndex )
731 n = iconDirCount - nIconIndex;
733 for( i = nIconIndex; i < nIconIndex + n; i++ )
734 { /* .ICO files have only one icon directory */
737 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
738 RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
742 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
745 { hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
748 { for( i = 0; i < iconCount; i++ )
749 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
750 { hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,*(WORD*)pData );
755 { RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
756 FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
759 { RetPtr[icon-nIconIndex] = 0;
765 HeapFree( GetProcessHeap(), 0, lpiID);
767 HeapFree( GetProcessHeap(), 0, pData);
770 if( sig == IMAGE_NT_SIGNATURE)
771 { LPBYTE idata,igdata;
772 PIMAGE_DOS_HEADER dheader;
773 PIMAGE_NT_HEADERS pe_header;
774 PIMAGE_SECTION_HEADER pe_sections;
775 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
776 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
778 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
779 CURSORICONDIR **cids;
781 fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
783 { /* FIXME, INVALID_HANDLE_VALUE? */
784 WARN("failed to create filemap.\n");
786 goto end_2; /* failure */
788 peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
790 { WARN("failed to mmap filemap.\n");
792 goto end_2; /* failure */
794 dheader = (PIMAGE_DOS_HEADER)peimage;
796 /* it is a pe header, SHELL_GetResourceTable checked that */
797 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);
799 /* probably makes problems with short PE headers... but I haven't seen
802 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
805 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
806 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
808 /* FIXME: doesn't work when the resources are not in a seperate section */
809 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
810 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
816 { WARN("haven't found section for resource directory.\n");
817 goto end_4; /* failure */
820 icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE);
822 if (!icongroupresdir)
823 { WARN("No Icongroupresourcedirectory!\n");
824 goto end_4; /* failure */
827 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
829 if( nIconIndex == (UINT16)-1 )
830 { RetPtr[0] = iconDirCount;
831 goto end_3; /* success */
834 if (nIconIndex >= iconDirCount)
835 { WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
837 goto end_4; /* failure */
840 cids = (CURSORICONDIR**)HeapAlloc(GetProcessHeap(),0,n*sizeof(CURSORICONDIR*));
842 /* caller just wanted the number of entries */
843 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
845 /* assure we don't get too much ... */
846 if( n > iconDirCount - nIconIndex )
847 { n = iconDirCount - nIconIndex;
850 /* starting from specified index ... */
851 xresent = xresent+nIconIndex;
853 for (i=0;i<n;i++,xresent++)
854 { CURSORICONDIR *cid;
855 PIMAGE_RESOURCE_DIRECTORY resdir;
857 /* go down this resource entry, name */
858 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
860 /* default language (0) */
861 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
862 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
864 /* lookup address in mapped image for virtual address */
867 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
868 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
870 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
872 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
876 { WARN("no matching real address for icongroup!\n");
877 goto end_4; /* failure */
880 cid = (CURSORICONDIR*)igdata;
882 RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
885 iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE);
888 { WARN("No Iconresourcedirectory!\n");
889 goto end_4; /* failure */
893 { PIMAGE_RESOURCE_DIRECTORY xresdir;
894 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
895 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
896 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
899 /* map virtual to address in image */
900 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
901 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
903 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
905 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
908 { WARN("no matching real address found for icondata!\n");
912 RetPtr[i] = CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
914 goto end_3; /* sucess */
916 goto end_1; /* return array with icon handles */
918 /* cleaning up (try & catch would be nicer) */
919 end_4: hRet = 0; /* failure */
920 end_3: UnmapViewOfFile(peimage); /* success */
921 end_2: CloseHandle(fmapping);
922 end_1: _lclose( hFile);
926 /*************************************************************************
927 * ExtractIcon16 (SHELL.34)
929 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
932 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
935 /*************************************************************************
936 * ExtractIconEx16 (SHELL.40)
938 HICON16 WINAPI ExtractIconEx16(
939 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
940 HICON16 *phiconSmall, UINT16 nIcons
942 HICON *ilarge,*ismall;
947 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
951 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
954 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
956 for (i=0;i<nIcons;i++)
957 phiconLarge[i]=ilarge[i];
958 HeapFree(GetProcessHeap(),0,ilarge);
961 for (i=0;i<nIcons;i++)
962 phiconSmall[i]=ismall[i];
963 HeapFree(GetProcessHeap(),0,ismall);
968 /*************************************************************************
969 * ExtractAssociatedIconA
971 * Return icon for given file (either from file itself or from associated
972 * executable) and patch parameters if needed.
974 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
976 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
979 /*************************************************************************
980 * ExtractAssociatedIcon [SHELL.36]
982 * Return icon for given file (either from file itself or from associated
983 * executable) and patch parameters if needed.
985 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
990 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
993 { if( hIcon == 1 ) /* no icons found in given file */
994 { char tempPath[0x80];
995 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
997 if( uRet > 32 && tempPath[0] )
998 { strcpy(lpIconPath,tempPath);
999 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
1007 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
1009 *lpiIcon = 6; /* generic icon - found nothing */
1011 GetModuleFileName16(hInst, lpIconPath, 0x80);
1012 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
1017 /*************************************************************************
1018 * FindEnvironmentString [SHELL.38]
1020 * Returns a pointer into the DOS environment... Ugh.
1022 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
1028 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
1029 { if( lstrncmpiA(lpEnv, entry, l) )
1032 return (lpEnv + l); /* empty entry */
1033 else if ( *(lpEnv+l)== '=' )
1034 return (lpEnv + l + 1);
1039 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
1041 LPSTR lpEnv,lpString;
1044 spEnv = GetDOSEnvironment16();
1046 lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
1047 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
1049 if( lpString ) /* offset should be small enough */
1050 return spEnv + (lpString - lpEnv);
1051 return (SEGPTR)NULL;
1054 /*************************************************************************
1055 * DoEnvironmentSubst [SHELL.37]
1057 * Replace %KEYWORD% in the str with the value of variable KEYWORD
1058 * from "DOS" environment.
1060 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
1062 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment16());
1063 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
1065 LPSTR lpbstr = lpBuffer;
1067 CharToOemA(str,str);
1069 TRACE("accept %s\n", str);
1071 while( *lpstr && lpbstr - lpBuffer < length )
1073 LPSTR lpend = lpstr;
1077 do { lpend++; } while( *lpend && *lpend != '%' );
1078 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
1082 lpKey = SHELL_FindString(lpEnv, lpstr+1);
1083 if( lpKey ) /* found key value */
1085 int l = strlen(lpKey);
1087 if( l > length - (lpbstr - lpBuffer) - 1 )
1089 WARN("-- Env subst aborted - string too short\n");
1093 strcpy(lpbstr, lpKey);
1100 else break; /* back off and whine */
1105 *lpbstr++ = *lpstr++;
1109 if( lpstr - str == strlen(str) )
1111 strncpy(str, lpBuffer, length);
1117 TRACE("-- return %s\n", str);
1119 OemToCharA(str,str);
1120 HeapFree( GetProcessHeap(), 0, lpBuffer);
1122 /* Return str length in the LOWORD
1123 * and 1 in HIWORD if subst was successful.
1125 return (DWORD)MAKELONG(strlen(str), length);
1128 /*************************************************************************
1129 * ShellHookProc [SHELL.103]
1130 * System-wide WH_SHELL hook.
1132 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
1134 TRACE("%i, %04x, %08x\n", code, wParam,
1136 if( SHELL_hHook && SHELL_hWnd )
1141 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
1142 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
1143 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
1145 PostMessage16( SHELL_hWnd, uMsg, wParam, 0 );
1147 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
1150 /*************************************************************************
1151 * RegisterShellHook [SHELL.102]
1153 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
1155 TRACE("%04x [%u]\n", hWnd, uAction );
1159 case 2: /* register hWnd as a shell window */
1162 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
1163 HOOKPROC16 hookProc = (HOOKPROC16)NE_GetEntryPoint( hShell, 103 );
1164 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
1167 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
1168 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
1169 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
1172 WARN("-- unable to install ShellHookProc()!\n");
1176 return ((SHELL_hWnd = hWnd) != 0);
1180 WARN("-- unknown code %i\n", uAction );
1181 SHELL_hWnd = 0; /* just in case */