2 * Shell Library Functions
11 #include "wine/winuser16.h"
12 #include "wine/winbase16.h"
13 #include "wine/shell16.h"
21 #include "cursoricon.h"
24 #include "debugtools.h"
26 #include "imagelist.h"
28 DECLARE_DEBUG_CHANNEL(exec)
29 DECLARE_DEBUG_CHANNEL(shell)
31 /* .ICO file ICONDIR definitions */
37 BYTE bWidth; /* Width, in pixels, of the image */
38 BYTE bHeight; /* Height, in pixels, of the image */
39 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
40 BYTE bReserved; /* Reserved ( must be 0) */
41 WORD wPlanes; /* Color Planes */
42 WORD wBitCount; /* Bits per pixel */
43 DWORD dwBytesInRes; /* How many bytes in this resource? */
44 DWORD dwImageOffset; /* Where in the file is this image? */
45 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
49 WORD idReserved; /* Reserved (must be 0) */
50 WORD idType; /* Resource Type (1 for icons) */
51 WORD idCount; /* How many images? */
52 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
53 } icoICONDIR, *LPicoICONDIR;
57 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
58 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
59 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
61 static HWND16 SHELL_hWnd = 0;
62 static HHOOK SHELL_hHook = 0;
63 static UINT16 uMsgWndCreated = 0;
64 static UINT16 uMsgWndDestroyed = 0;
65 static UINT16 uMsgShellActivate = 0;
67 /*************************************************************************
68 * DragAcceptFiles32 [SHELL32.54]
70 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
77 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
78 if (b)exstyle |= WS_EX_ACCEPTFILES;
79 else exstyle &= ~WS_EX_ACCEPTFILES;
80 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
83 /*************************************************************************
84 * DragAcceptFiles16 [SHELL.9]
86 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
88 DragAcceptFiles(hWnd, b);
91 /*************************************************************************
92 * SHELL_DragQueryFile [internal]
95 static UINT SHELL_DragQueryFile(LPSTR lpDrop, LPWSTR lpwDrop, UINT lFile,
96 LPSTR lpszFile, LPWSTR lpszwFile, UINT lLength)
102 while (i++ < lFile) {
103 while (*lpDrop++); /* skip filename */
105 return (lFile == 0xFFFFFFFF) ? i : 0;
109 while (i++ < lFile) {
110 while (*lpwDrop++); /* skip filename */
112 return (lFile == 0xFFFFFFFF) ? i : 0;
116 if (lpDrop) i = lstrlenA(lpDrop);
117 if (lpwDrop) i = lstrlenW(lpwDrop);
119 if (!lpszFile && !lpszwFile) {
120 return i; /* needed buffer size */
122 i = (lLength > i) ? i : lLength;
124 if (lpDrop) lstrcpynA (lpszFile, lpDrop, i);
125 else lstrcpynWtoA(lpszFile, lpwDrop, i);
127 if (lpDrop) lstrcpynAtoW(lpszwFile, lpDrop, i);
128 else lstrcpynW (lpszwFile, lpwDrop, i);
133 /*************************************************************************
134 * DragQueryFile32A [SHELL32.81] [shell32.82]
136 UINT WINAPI DragQueryFileA(HDROP hDrop, UINT lFile, LPSTR lpszFile,
138 { /* hDrop is a global memory block allocated with GMEM_SHARE
139 * with DROPFILESTRUCT as a header and filenames following
140 * it, zero length filename is in the end */
142 LPDROPFILESTRUCT lpDropFileStruct;
146 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
148 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
149 if(!lpDropFileStruct)
152 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->lSize;
153 i = SHELL_DragQueryFile(lpCurrent, NULL, lFile, lpszFile, NULL, lLength);
158 /*************************************************************************
159 * DragQueryFile32W [shell32.133]
161 UINT WINAPI DragQueryFileW(HDROP hDrop, UINT lFile, LPWSTR lpszwFile,
164 LPDROPFILESTRUCT lpDropFileStruct;
168 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
170 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
171 if(!lpDropFileStruct)
174 lpwCurrent = (LPWSTR) lpDropFileStruct + lpDropFileStruct->lSize;
175 i = SHELL_DragQueryFile(NULL, lpwCurrent, lFile, NULL, lpszwFile,lLength);
179 /*************************************************************************
180 * DragQueryFile16 [SHELL.11]
182 UINT16 WINAPI DragQueryFile16(HDROP16 hDrop, WORD wFile, LPSTR lpszFile,
184 { /* hDrop is a global memory block allocated with GMEM_SHARE
185 * with DROPFILESTRUCT as a header and filenames following
186 * it, zero length filename is in the end */
188 LPDROPFILESTRUCT16 lpDropFileStruct;
192 TRACE_(shell)("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
194 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
195 if(!lpDropFileStruct)
198 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
200 i = (WORD)SHELL_DragQueryFile(lpCurrent, NULL, wFile==0xffff?0xffffffff:wFile,
201 lpszFile, NULL, wLength);
202 GlobalUnlock16(hDrop);
208 /*************************************************************************
209 * DragFinish32 [SHELL32.80]
211 void WINAPI DragFinish(HDROP h)
212 { TRACE_(shell)("\n");
213 GlobalFree((HGLOBAL)h);
216 /*************************************************************************
217 * DragFinish16 [SHELL.12]
219 void WINAPI DragFinish16(HDROP16 h)
220 { TRACE_(shell)("\n");
221 GlobalFree16((HGLOBAL16)h);
225 /*************************************************************************
226 * DragQueryPoint32 [SHELL32.135]
228 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
230 LPDROPFILESTRUCT lpDropFileStruct;
233 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
235 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT));
236 bRet = lpDropFileStruct->fInNonClientArea;
242 /*************************************************************************
243 * DragQueryPoint16 [SHELL.13]
245 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
247 LPDROPFILESTRUCT16 lpDropFileStruct;
250 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
252 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
253 bRet = lpDropFileStruct->fInNonClientArea;
255 GlobalUnlock16(hDrop);
259 /*************************************************************************
260 * SHELL_FindExecutable [Internal]
262 * Utility for code sharing between FindExecutable and ShellExecute
264 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
267 { char *extension = NULL; /* pointer to file extension */
268 char tmpext[5]; /* local copy to mung as we please */
269 char filetype[256]; /* registry name for this filetype */
270 LONG filetypelen=256; /* length of above */
271 char command[256]; /* command from registry */
272 LONG commandlen=256; /* This is the most DOS can handle :) */
273 char buffer[256]; /* Used to GetProfileString */
274 HINSTANCE retval=31; /* default - 'No association was found' */
275 char *tok; /* token pointer */
276 int i; /* random counter */
277 char xlpFile[256]; /* result of SearchPath */
279 TRACE_(shell)("%s\n", (lpFile != NULL?lpFile:"-") );
281 lpResult[0]='\0'; /* Start off with an empty return string */
283 /* trap NULL parameters on entry */
284 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
285 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
286 lpFile, lpOperation, lpResult);
287 return 2; /* File not found. Close enough, I guess. */
290 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
291 { TRACE_(shell)("SearchPath32A returned non-zero\n");
295 /* First thing we need is the file's extension */
296 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
297 /* File->Run in progman uses */
299 TRACE_(shell)("xlpFile=%s,extension=%s\n", xlpFile, extension);
301 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
302 { WARN_(shell)("Returning 31 - No association\n");
303 return 31; /* no association */
306 /* Make local copy & lowercase it for reg & 'programs=' lookup */
307 lstrcpynA( tmpext, extension, 5 );
308 CharLowerA( tmpext );
309 TRACE_(shell)("%s file\n", tmpext);
311 /* Three places to check: */
312 /* 1. win.ini, [windows], programs (NB no leading '.') */
313 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
314 /* 3. win.ini, [extensions], extension (NB no leading '.' */
315 /* All I know of the order is that registry is checked before */
316 /* extensions; however, it'd make sense to check the programs */
317 /* section first, so that's what happens here. */
319 /* See if it's a program - if GetProfileString fails, we skip this
320 * section. Actually, if GetProfileString fails, we've probably
321 * got a lot more to worry about than running a program... */
322 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
323 buffer, sizeof(buffer)) > 0 )
324 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
326 tok = strtok(buffer, " \t"); /* ? */
329 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
331 strcpy(lpResult, xlpFile);
332 /* Need to perhaps check that the file has a path
334 TRACE_(shell)("found %s\n", lpResult);
337 /* Greater than 32 to indicate success FIXME According to the
338 * docs, I should be returning a handle for the
339 * executable. Does this mean I'm supposed to open the
340 * executable file or something? More RTFM, I guess... */
342 tok=strtok(NULL, " \t");
347 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
348 &filetypelen ) == ERROR_SUCCESS )
350 filetype[filetypelen]='\0';
351 TRACE_(shell)("File type: %s\n", filetype);
353 /* Looking for ...buffer\shell\lpOperation\command */
354 strcat( filetype, "\\shell\\" );
355 strcat( filetype, lpOperation );
356 strcat( filetype, "\\command" );
358 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
359 &commandlen ) == ERROR_SUCCESS )
361 /* Is there a replace() function anywhere? */
362 command[commandlen]='\0';
363 strcpy( lpResult, command );
364 tok=strstr( lpResult, "%1" );
367 tok[0]='\0'; /* truncate string at the percent */
368 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
369 tok=strstr( command, "%1" );
370 if ((tok!=NULL) && (strlen(tok)>2))
372 strcat( lpResult, &tok[2] );
375 retval=33; /* FIXME see above */
378 else /* Check win.ini */
380 /* Toss the leading dot */
382 if ( GetProfileStringA( "extensions", extension, "", command,
383 sizeof(command)) > 0)
385 if (strlen(command)!=0)
387 strcpy( lpResult, command );
388 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
392 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
393 tok=strstr( command, "^" ); /* see above */
394 if ((tok != NULL) && (strlen(tok)>5))
396 strcat( lpResult, &tok[5]);
399 retval=33; /* FIXME - see above */
404 TRACE_(shell)("returning %s\n", lpResult);
408 /*************************************************************************
409 * ShellExecute16 [SHELL.20]
411 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
412 LPCSTR lpFile, LPCSTR lpParameters,
413 LPCSTR lpDirectory, INT16 iShowCmd )
414 { HINSTANCE16 retval=31;
418 TRACE_(shell)("(%04x,'%s','%s','%s','%s',%x)\n",
419 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
420 lpParameters ? lpParameters : "<null>",
421 lpDirectory ? lpDirectory : "<null>", iShowCmd);
423 if (lpFile==NULL) return 0; /* should not happen */
424 if (lpOperation==NULL) /* default is open */
428 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
429 SetCurrentDirectoryA( lpDirectory );
432 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
434 if (retval > 32) /* Found */
439 strcat(cmd,lpParameters);
442 TRACE_(shell)("starting %s\n",cmd);
443 SYSLEVEL_ReleaseWin16Lock();
444 retval = WinExec( cmd, iShowCmd );
445 SYSLEVEL_RestoreWin16Lock();
448 SetCurrentDirectoryA( old_dir );
452 /*************************************************************************
453 * FindExecutable16 (SHELL.21)
455 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
457 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
461 /*************************************************************************
462 * AboutDlgProc16 (SHELL.33)
464 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
466 { return AboutDlgProc( hWnd, msg, wParam, lParam );
470 /*************************************************************************
471 * ShellAbout16 (SHELL.22)
473 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
475 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
478 /*************************************************************************
479 * SHELL_GetResourceTable
481 static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
482 { IMAGE_DOS_HEADER mz_header;
489 _llseek( hFile, 0, SEEK_SET );
490 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
492 if (mz_header.e_cblp == 1)
493 { /* ICONHEADER.idType, must be 1 */
494 *retptr = (LPBYTE)-1;
498 return 0; /* failed */
500 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
502 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
505 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
507 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
508 return IMAGE_NT_SIGNATURE;
510 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
511 { IMAGE_OS2_HEADER ne_header;
512 LPBYTE pTypeInfo = (LPBYTE)-1;
514 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
517 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
520 size = ne_header.rname_tab_offset - ne_header.resource_tab_offset;
522 if( size > sizeof(NE_TYPEINFO) )
523 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
525 { _llseek(hFile, mz_header.e_lfanew+ne_header.resource_tab_offset, SEEK_SET);
526 if( _lread( hFile, (char*)pTypeInfo, size) != size )
527 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
533 return IMAGE_OS2_SIGNATURE;
535 return 0; /* failed */
538 /*************************************************************************
541 static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
543 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
547 if( (ptr = (BYTE*)GlobalLock16( handle )) )
548 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
549 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
555 /*************************************************************************
558 static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
560 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
562 if( (ptr = (BYTE*)GlobalLock16( handle )) )
563 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
564 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
570 /*************************************************************************
571 * ICO_GetIconDirectory
573 * Read .ico file and build phony ICONDIR struct for GetIconID
575 static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIR* lplpiID )
576 { WORD id[3]; /* idReserved, idType, idCount */
581 _llseek( hFile, 0, SEEK_SET );
582 if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
586 * - see http://www.microsoft.com/win32dev/ui/icons.htm
589 if( id[0] || id[1] != 1 || !id[2] ) return 0;
591 i = id[2]*sizeof(icoICONDIRENTRY) ;
593 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, i + sizeof(id));
595 if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
596 { HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10,
597 id[2]*sizeof(CURSORICONDIRENTRY) + sizeof(id) );
599 { CURSORICONDIR* lpID = (CURSORICONDIR*)GlobalLock16( handle );
600 lpID->idReserved = lpiID->idReserved = id[0];
601 lpID->idType = lpiID->idType = id[1];
602 lpID->idCount = lpiID->idCount = id[2];
603 for( i=0; i < lpiID->idCount; i++ )
604 { memcpy((void*)(lpID->idEntries + i),
605 (void*)(lpiID->idEntries + i), sizeof(CURSORICONDIRENTRY) - 2);
606 lpID->idEntries[i].wResId = i;
614 HeapFree( GetProcessHeap(), 0, lpiID);
618 /*************************************************************************
619 * InternalExtractIcon [SHELL.39]
621 * This abortion is called directly by Progman
623 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
624 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
625 { HGLOBAL16 hRet = 0;
626 HGLOBAL16* RetPtr = NULL;
630 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
631 UINT16 iconDirCount = 0,iconCount = 0;
635 TRACE_(shell)("(%04x,file %s,start %d,extract %d\n",
636 hInstance, lpszExeFileName, nIconIndex, n);
638 if( hFile == HFILE_ERROR || !n )
641 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
642 RetPtr = (HICON16*)GlobalLock16(hRet);
644 *RetPtr = (n == 0xFFFF)? 0: 1; /* error return values */
646 sig = SHELL_GetResourceTable(hFile,&pData);
648 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
650 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
651 NE_NAMEINFO* pIconStorage = NULL;
652 NE_NAMEINFO* pIconDir = NULL;
653 LPicoICONDIR lpiID = NULL;
655 if( pData == (BYTE*)-1 )
656 { hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID); /* check for .ICO file */
658 { iconDirCount = 1; iconCount = lpiID->idCount;
661 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
662 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
663 { iconDirCount = pTInfo->count;
664 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
665 TRACE_(shell)("\tfound directory - %i icon families\n", iconDirCount);
667 if( pTInfo->type_id == NE_RSCTYPE_ICON )
668 { iconCount = pTInfo->count;
669 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
670 TRACE_(shell)("\ttotal icons - %i\n", iconCount);
672 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
675 /* load resources and create icons */
677 if( (pIconStorage && pIconDir) || lpiID )
678 { if( nIconIndex == (UINT16)-1 )
679 { RetPtr[0] = iconDirCount;
681 else if( nIconIndex < iconDirCount )
683 if( n > iconDirCount - nIconIndex )
684 n = iconDirCount - nIconIndex;
686 for( i = nIconIndex; i < nIconIndex + n; i++ )
687 { /* .ICO files have only one icon directory */
690 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
691 RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
695 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
698 { hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
701 { for( i = 0; i < iconCount; i++ )
702 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
703 { hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,*(WORD*)pData );
708 { RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
709 FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
712 { RetPtr[icon-nIconIndex] = 0;
718 HeapFree( GetProcessHeap(), 0, lpiID);
720 HeapFree( GetProcessHeap(), 0, pData);
723 if( sig == IMAGE_NT_SIGNATURE)
724 { LPBYTE idata,igdata;
725 PIMAGE_DOS_HEADER dheader;
726 PIMAGE_NT_HEADERS pe_header;
727 PIMAGE_SECTION_HEADER pe_sections;
728 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
729 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
731 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
732 CURSORICONDIR **cids;
734 fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
736 { /* FIXME, INVALID_HANDLE_VALUE? */
737 WARN_(shell)("failed to create filemap.\n");
739 goto end_2; /* failure */
741 peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
743 { WARN_(shell)("failed to mmap filemap.\n");
745 goto end_2; /* failure */
747 dheader = (PIMAGE_DOS_HEADER)peimage;
749 /* it is a pe header, SHELL_GetResourceTable checked that */
750 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);
752 /* probably makes problems with short PE headers... but I haven't seen
755 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
758 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
759 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
761 /* FIXME: doesn't work when the resources are not in a seperate section */
762 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
763 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
769 { WARN_(shell)("haven't found section for resource directory.\n");
770 goto end_4; /* failure */
773 icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE);
775 if (!icongroupresdir)
776 { WARN_(shell)("No Icongroupresourcedirectory!\n");
777 goto end_4; /* failure */
780 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
782 if( nIconIndex == (UINT16)-1 )
783 { RetPtr[0] = iconDirCount;
784 goto end_3; /* success */
787 if (nIconIndex >= iconDirCount)
788 { WARN_(shell)("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
790 goto end_4; /* failure */
793 cids = (CURSORICONDIR**)HeapAlloc(GetProcessHeap(),0,n*sizeof(CURSORICONDIR*));
795 /* caller just wanted the number of entries */
796 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
798 /* assure we don't get too much ... */
799 if( n > iconDirCount - nIconIndex )
800 { n = iconDirCount - nIconIndex;
803 /* starting from specified index ... */
804 xresent = xresent+nIconIndex;
806 for (i=0;i<n;i++,xresent++)
807 { CURSORICONDIR *cid;
808 PIMAGE_RESOURCE_DIRECTORY resdir;
810 /* go down this resource entry, name */
811 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
813 /* default language (0) */
814 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
815 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
817 /* lookup address in mapped image for virtual address */
820 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
821 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
823 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
825 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
829 { WARN_(shell)("no matching real address for icongroup!\n");
830 goto end_4; /* failure */
833 cid = (CURSORICONDIR*)igdata;
835 RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
838 iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE);
841 { WARN_(shell)("No Iconresourcedirectory!\n");
842 goto end_4; /* failure */
846 { PIMAGE_RESOURCE_DIRECTORY xresdir;
847 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
848 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
849 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
852 /* map virtual to address in image */
853 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
854 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
856 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
858 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
861 { WARN_(shell)("no matching real address found for icondata!\n");
865 RetPtr[i] = CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
867 goto end_3; /* sucess */
869 goto end_1; /* return array with icon handles */
871 /* cleaning up (try & catch would be nicer) */
872 end_4: hRet = 0; /* failure */
873 end_3: UnmapViewOfFile(peimage); /* success */
874 end_2: CloseHandle(fmapping);
875 end_1: _lclose( hFile);
879 /*************************************************************************
880 * ExtractIcon16 (SHELL.34)
882 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
884 { TRACE_(shell)("\n");
885 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
888 /*************************************************************************
889 * ExtractIconEx16 (SHELL.40)
891 HICON16 WINAPI ExtractIconEx16(
892 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
893 HICON16 *phiconSmall, UINT16 nIcons
895 HICON *ilarge,*ismall;
900 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
904 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
907 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
909 for (i=0;i<nIcons;i++)
910 phiconLarge[i]=ilarge[i];
911 HeapFree(GetProcessHeap(),0,ilarge);
914 for (i=0;i<nIcons;i++)
915 phiconSmall[i]=ismall[i];
916 HeapFree(GetProcessHeap(),0,ismall);
921 /*************************************************************************
922 * ExtractAssociatedIcon [SHELL.36]
924 * Return icon for given file (either from file itself or from associated
925 * executable) and patch parameters if needed.
927 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
928 { TRACE_(shell)("\n");
929 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
932 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
937 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
940 { if( hIcon == 1 ) /* no icons found in given file */
941 { char tempPath[0x80];
942 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
944 if( uRet > 32 && tempPath[0] )
945 { strcpy(lpIconPath,tempPath);
946 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
954 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
956 *lpiIcon = 6; /* generic icon - found nothing */
958 GetModuleFileName16(hInst, lpIconPath, 0x80);
959 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
964 /*************************************************************************
965 * FindEnvironmentString [SHELL.38]
967 * Returns a pointer into the DOS environment... Ugh.
969 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
975 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
976 { if( lstrncmpiA(lpEnv, entry, l) )
979 return (lpEnv + l); /* empty entry */
980 else if ( *(lpEnv+l)== '=' )
981 return (lpEnv + l + 1);
986 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
988 LPSTR lpEnv,lpString;
991 spEnv = GetDOSEnvironment16();
993 lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
994 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
996 if( lpString ) /* offset should be small enough */
997 return spEnv + (lpString - lpEnv);
1001 /*************************************************************************
1002 * DoEnvironmentSubst [SHELL.37]
1004 * Replace %KEYWORD% in the str with the value of variable KEYWORD
1005 * from "DOS" environment.
1007 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
1009 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment16());
1010 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
1012 LPSTR lpbstr = lpBuffer;
1014 CharToOemA(str,str);
1016 TRACE_(shell)("accept %s\n", str);
1018 while( *lpstr && lpbstr - lpBuffer < length )
1020 LPSTR lpend = lpstr;
1024 do { lpend++; } while( *lpend && *lpend != '%' );
1025 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
1029 lpKey = SHELL_FindString(lpEnv, lpstr+1);
1030 if( lpKey ) /* found key value */
1032 int l = strlen(lpKey);
1034 if( l > length - (lpbstr - lpBuffer) - 1 )
1036 WARN_(shell)("-- Env subst aborted - string too short\n");
1040 strcpy(lpbstr, lpKey);
1047 else break; /* back off and whine */
1052 *lpbstr++ = *lpstr++;
1056 if( lpstr - str == strlen(str) )
1058 strncpy(str, lpBuffer, length);
1064 TRACE_(shell)("-- return %s\n", str);
1066 OemToCharA(str,str);
1067 HeapFree( GetProcessHeap(), 0, lpBuffer);
1069 /* Return str length in the LOWORD
1070 * and 1 in HIWORD if subst was successful.
1072 return (DWORD)MAKELONG(strlen(str), length);
1075 /*************************************************************************
1076 * ShellHookProc [SHELL.103]
1077 * System-wide WH_SHELL hook.
1079 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
1081 TRACE_(shell)("%i, %04x, %08x\n", code, wParam,
1083 if( SHELL_hHook && SHELL_hWnd )
1088 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
1089 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
1090 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
1092 PostMessage16( SHELL_hWnd, uMsg, wParam, 0 );
1094 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
1097 /*************************************************************************
1098 * RegisterShellHook [SHELL.102]
1100 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
1101 { TRACE_(shell)("%04x [%u]\n", hWnd, uAction );
1104 { case 2: /* register hWnd as a shell window */
1106 { HMODULE16 hShell = GetModuleHandle16( "SHELL" );
1107 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, ShellHookProc16, hShell, 0 );
1109 { uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
1110 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
1111 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
1114 WARN_(shell)("-- unable to install ShellHookProc()!\n");
1118 return ((SHELL_hWnd = hWnd) != 0);
1122 WARN_(shell)("-- unknown code %i\n", uAction );