2 * Shell Library Functions
22 #include "cursoricon.h"
23 #include "interfaces.h"
24 #include "sysmetrics.h"
28 #include "imagelist.h"
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 * DragAcceptFiles [SHELL.9]
70 void WINAPI
DragAcceptFiles(HWND16 hWnd
, BOOL16 b
)
71 { WND
* wnd
= WIN_FindWndPtr(hWnd
);
74 wnd
->dwExStyle
= b
? wnd
->dwExStyle
| WS_EX_ACCEPTFILES
75 : wnd
->dwExStyle
& ~WS_EX_ACCEPTFILES
;
79 /*************************************************************************
80 * DragQueryFile [SHELL.11]
82 UINT16 WINAPI
DragQueryFile(HDROP16 hDrop
, WORD wFile
, LPSTR lpszFile
,
84 { /* hDrop is a global memory block allocated with GMEM_SHARE
85 * with DROPFILESTRUCT as a header and filenames following
86 * it, zero length filename is in the end */
88 LPDROPFILESTRUCT lpDropFileStruct
;
92 TRACE(shell
,"(%04x, %i, %p, %u)\n",
93 hDrop
,wFile
,lpszFile
,wLength
);
95 lpDropFileStruct
= (LPDROPFILESTRUCT
) GlobalLock16(hDrop
);
99 lpCurrent
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->wSize
;
103 { while (*lpCurrent
++); /* skip filename */
105 return (wFile
== 0xFFFF) ? i
: 0;
108 i
= strlen(lpCurrent
);
110 return i
+1; /* needed buffer size */
112 i
= (wLength
> i
) ? i
: wLength
-1;
113 strncpy(lpszFile
, lpCurrent
, i
);
116 GlobalUnlock16(hDrop
);
121 /*************************************************************************
122 * DragFinish [SHELL.12]
124 void WINAPI
DragFinish(HDROP16 h
)
126 GlobalFree16((HGLOBAL16
)h
);
130 /*************************************************************************
131 * DragQueryPoint [SHELL.13]
133 BOOL16 WINAPI
DragQueryPoint(HDROP16 hDrop
, POINT16
*p
)
134 { LPDROPFILESTRUCT lpDropFileStruct
;
137 lpDropFileStruct
= (LPDROPFILESTRUCT
) GlobalLock16(hDrop
);
139 memcpy(p
,&lpDropFileStruct
->ptMousePos
,sizeof(POINT16
));
140 bRet
= lpDropFileStruct
->fInNonClientArea
;
142 GlobalUnlock16(hDrop
);
146 /*************************************************************************
147 * SHELL_FindExecutable [Internal]
149 * Utility for code sharing between FindExecutable and ShellExecute
151 HINSTANCE32
SHELL_FindExecutable( LPCSTR lpFile
,
154 { char *extension
= NULL
; /* pointer to file extension */
155 char tmpext
[5]; /* local copy to mung as we please */
156 char filetype
[256]; /* registry name for this filetype */
157 LONG filetypelen
=256; /* length of above */
158 char command
[256]; /* command from registry */
159 LONG commandlen
=256; /* This is the most DOS can handle :) */
160 char buffer
[256]; /* Used to GetProfileString */
161 HINSTANCE32 retval
=31; /* default - 'No association was found' */
162 char *tok
; /* token pointer */
163 int i
; /* random counter */
164 char xlpFile
[256]; /* result of SearchPath */
166 TRACE(shell
, "%s\n", (lpFile
!= NULL
?lpFile
:"-") );
168 lpResult
[0]='\0'; /* Start off with an empty return string */
170 /* trap NULL parameters on entry */
171 if (( lpFile
== NULL
) || ( lpResult
== NULL
) || ( lpOperation
== NULL
))
172 { WARN(exec
, "(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
173 lpFile
, lpOperation
, lpResult
);
174 return 2; /* File not found. Close enough, I guess. */
177 if (SearchPath32A( NULL
, lpFile
,".exe",sizeof(xlpFile
),xlpFile
,NULL
))
178 { TRACE(shell
, "SearchPath32A returned non-zero\n");
182 /* First thing we need is the file's extension */
183 extension
= strrchr( xlpFile
, '.' ); /* Assume last "." is the one; */
184 /* File->Run in progman uses */
186 TRACE(shell
, "xlpFile=%s,extension=%s\n", xlpFile
, extension
);
188 if ((extension
== NULL
) || (extension
== &xlpFile
[strlen(xlpFile
)]))
189 { WARN(shell
, "Returning 31 - No association\n");
190 return 31; /* no association */
193 /* Make local copy & lowercase it for reg & 'programs=' lookup */
194 lstrcpyn32A( tmpext
, extension
, 5 );
195 CharLower32A( tmpext
);
196 TRACE(shell
, "%s file\n", tmpext
);
198 /* Three places to check: */
199 /* 1. win.ini, [windows], programs (NB no leading '.') */
200 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
201 /* 3. win.ini, [extensions], extension (NB no leading '.' */
202 /* All I know of the order is that registry is checked before */
203 /* extensions; however, it'd make sense to check the programs */
204 /* section first, so that's what happens here. */
206 /* See if it's a program - if GetProfileString fails, we skip this
207 * section. Actually, if GetProfileString fails, we've probably
208 * got a lot more to worry about than running a program... */
209 if ( GetProfileString32A("windows", "programs", "exe pif bat com",
210 buffer
, sizeof(buffer
)) > 0 )
211 { for (i
=0;i
<strlen(buffer
); i
++) buffer
[i
]=tolower(buffer
[i
]);
213 tok
= strtok(buffer
, " \t"); /* ? */
216 if (strcmp(tok
, &tmpext
[1])==0) /* have to skip the leading "." */
218 strcpy(lpResult
, xlpFile
);
219 /* Need to perhaps check that the file has a path
221 TRACE(shell
, "found %s\n", lpResult
);
224 /* Greater than 32 to indicate success FIXME According to the
225 * docs, I should be returning a handle for the
226 * executable. Does this mean I'm supposed to open the
227 * executable file or something? More RTFM, I guess... */
229 tok
=strtok(NULL
, " \t");
234 if (RegQueryValue16( HKEY_CLASSES_ROOT
, tmpext
, filetype
,
235 &filetypelen
) == ERROR_SUCCESS
)
237 filetype
[filetypelen
]='\0';
238 TRACE(shell
, "File type: %s\n", filetype
);
240 /* Looking for ...buffer\shell\lpOperation\command */
241 strcat( filetype
, "\\shell\\" );
242 strcat( filetype
, lpOperation
);
243 strcat( filetype
, "\\command" );
245 if (RegQueryValue16( HKEY_CLASSES_ROOT
, filetype
, command
,
246 &commandlen
) == ERROR_SUCCESS
)
248 /* Is there a replace() function anywhere? */
249 command
[commandlen
]='\0';
250 strcpy( lpResult
, command
);
251 tok
=strstr( lpResult
, "%1" );
254 tok
[0]='\0'; /* truncate string at the percent */
255 strcat( lpResult
, xlpFile
); /* what if no dir in xlpFile? */
256 tok
=strstr( command
, "%1" );
257 if ((tok
!=NULL
) && (strlen(tok
)>2))
259 strcat( lpResult
, &tok
[2] );
262 retval
=33; /* FIXME see above */
265 else /* Check win.ini */
267 /* Toss the leading dot */
269 if ( GetProfileString32A( "extensions", extension
, "", command
,
270 sizeof(command
)) > 0)
272 if (strlen(command
)!=0)
274 strcpy( lpResult
, command
);
275 tok
=strstr( lpResult
, "^" ); /* should be ^.extension? */
279 strcat( lpResult
, xlpFile
); /* what if no dir in xlpFile? */
280 tok
=strstr( command
, "^" ); /* see above */
281 if ((tok
!= NULL
) && (strlen(tok
)>5))
283 strcat( lpResult
, &tok
[5]);
286 retval
=33; /* FIXME - see above */
291 TRACE(shell
, "returning %s\n", lpResult
);
295 /*************************************************************************
296 * ShellExecute16 [SHELL.20]
298 HINSTANCE16 WINAPI
ShellExecute16( HWND16 hWnd
, LPCSTR lpOperation
,
299 LPCSTR lpFile
, LPCSTR lpParameters
,
300 LPCSTR lpDirectory
, INT16 iShowCmd
)
301 { HINSTANCE16 retval
=31;
305 TRACE(shell
, "(%04x,'%s','%s','%s','%s',%x)\n",
306 hWnd
, lpOperation
? lpOperation
:"<null>", lpFile
? lpFile
:"<null>",
307 lpParameters
? lpParameters
: "<null>",
308 lpDirectory
? lpDirectory
: "<null>", iShowCmd
);
310 if (lpFile
==NULL
) return 0; /* should not happen */
311 if (lpOperation
==NULL
) /* default is open */
315 { GetCurrentDirectory32A( sizeof(old_dir
), old_dir
);
316 SetCurrentDirectory32A( lpDirectory
);
319 retval
= SHELL_FindExecutable( lpFile
, lpOperation
, cmd
);
321 if (retval
> 32) /* Found */
324 strcat(cmd
,lpParameters
);
327 TRACE(shell
,"starting %s\n",cmd
);
328 retval
= WinExec32( cmd
, iShowCmd
);
331 SetCurrentDirectory32A( old_dir
);
335 /*************************************************************************
336 * FindExecutable16 (SHELL.21)
338 HINSTANCE16 WINAPI
FindExecutable16( LPCSTR lpFile
, LPCSTR lpDirectory
,
340 { return (HINSTANCE16
)FindExecutable32A( lpFile
, lpDirectory
, lpResult
);
344 /*************************************************************************
345 * AboutDlgProc16 (SHELL.33)
347 LRESULT WINAPI
AboutDlgProc16( HWND16 hWnd
, UINT16 msg
, WPARAM16 wParam
,
349 { return AboutDlgProc32( hWnd
, msg
, wParam
, lParam
);
353 /*************************************************************************
354 * ShellAbout16 (SHELL.22)
356 BOOL16 WINAPI
ShellAbout16( HWND16 hWnd
, LPCSTR szApp
, LPCSTR szOtherStuff
,
358 { return ShellAbout32A( hWnd
, szApp
, szOtherStuff
, hIcon
);
361 /*************************************************************************
362 * SHELL_GetResourceTable
364 static DWORD
SHELL_GetResourceTable(HFILE32 hFile
,LPBYTE
*retptr
)
366 IMAGE_DOS_HEADER mz_header
;
371 _llseek32( hFile
, 0, SEEK_SET
);
372 if ( (_lread32(hFile
,&mz_header
,sizeof(mz_header
)) != sizeof(mz_header
)) ||
373 (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
)
374 ) { /* .ICO file ? */
375 if (mz_header
.e_cblp
== 1) { /* ICONHEADER.idType, must be 1 */
376 *retptr
= (LPBYTE
)-1;
380 return 0; /* failed */
382 _llseek32( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
383 if (_lread32( hFile
, magic
, sizeof(magic
) ) != sizeof(magic
))
385 _llseek32( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
387 if (*(DWORD
*)magic
== IMAGE_NT_SIGNATURE
)
388 return IMAGE_NT_SIGNATURE
;
389 if (*(WORD
*)magic
== IMAGE_OS2_SIGNATURE
) {
390 IMAGE_OS2_HEADER ne_header
;
391 LPBYTE pTypeInfo
= (LPBYTE
)-1;
393 if (_lread32(hFile
,&ne_header
,sizeof(ne_header
))!=sizeof(ne_header
))
396 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
) return 0;
397 size
= ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
;
398 if( size
> sizeof(NE_TYPEINFO
) )
400 pTypeInfo
= (BYTE
*)HeapAlloc( GetProcessHeap(), 0, size
);
402 _llseek32(hFile
, mz_header
.e_lfanew
+ne_header
.resource_tab_offset
, SEEK_SET
);
403 if( _lread32( hFile
, (char*)pTypeInfo
, size
) != size
) {
404 HeapFree( GetProcessHeap(), 0, pTypeInfo
);
410 return IMAGE_OS2_SIGNATURE
;
412 return 0; /* failed */
415 /*************************************************************************
418 static HGLOBAL16
SHELL_LoadResource(HINSTANCE16 hInst
, HFILE32 hFile
, NE_NAMEINFO
* pNInfo
, WORD sizeShift
)
420 HGLOBAL16 handle
= DirectResAlloc( hInst
, 0x10, (DWORD
)pNInfo
->length
<< sizeShift
);
422 if( (ptr
= (BYTE
*)GlobalLock16( handle
)) )
423 { _llseek32( hFile
, (DWORD
)pNInfo
->offset
<< sizeShift
, SEEK_SET
);
424 _lread32( hFile
, (char*)ptr
, pNInfo
->length
<< sizeShift
);
430 /*************************************************************************
433 static HGLOBAL16
ICO_LoadIcon(HINSTANCE16 hInst
, HFILE32 hFile
, LPicoICONDIRENTRY lpiIDE
)
435 HGLOBAL16 handle
= DirectResAlloc( hInst
, 0x10, lpiIDE
->dwBytesInRes
);
437 if( (ptr
= (BYTE
*)GlobalLock16( handle
)) )
438 { _llseek32( hFile
, lpiIDE
->dwImageOffset
, SEEK_SET
);
439 _lread32( hFile
, (char*)ptr
, lpiIDE
->dwBytesInRes
);
445 /*************************************************************************
446 * ICO_GetIconDirectory
448 * Read .ico file and build phony ICONDIR struct for GetIconID
450 static HGLOBAL16
ICO_GetIconDirectory(HINSTANCE16 hInst
, HFILE32 hFile
, LPicoICONDIR
* lplpiID
)
451 { WORD id
[3]; /* idReserved, idType, idCount */
456 _llseek32( hFile
, 0, SEEK_SET
);
457 if( _lread32(hFile
,(char*)id
,sizeof(id
)) != sizeof(id
) ) return 0;
461 * - see http://www.microsoft.com/win32dev/ui/icons.htm
464 if( id
[0] || id
[1] != 1 || !id
[2] ) return 0;
466 i
= id
[2]*sizeof(icoICONDIRENTRY
) + sizeof(id
);
468 lpiID
= (LPicoICONDIR
)HeapAlloc( GetProcessHeap(), 0, i
);
470 if( _lread32(hFile
,(char*)lpiID
->idEntries
,i
) == i
)
471 { HGLOBAL16 handle
= DirectResAlloc( hInst
, 0x10,
472 id
[2]*sizeof(ICONDIRENTRY
) + sizeof(id
) );
474 { CURSORICONDIR
* lpID
= (CURSORICONDIR
*)GlobalLock16( handle
);
475 lpID
->idReserved
= lpiID
->idReserved
= id
[0];
476 lpID
->idType
= lpiID
->idType
= id
[1];
477 lpID
->idCount
= lpiID
->idCount
= id
[2];
478 for( i
=0; i
< lpiID
->idCount
; i
++ )
479 { memcpy((void*)(lpID
->idEntries
+ i
),
480 (void*)(lpiID
->idEntries
+ i
), sizeof(ICONDIRENTRY
) - 2);
481 lpID
->idEntries
[i
].icon
.wResId
= i
;
489 HeapFree( GetProcessHeap(), 0, lpiID
);
493 /*************************************************************************
494 * InternalExtractIcon [SHELL.39]
496 * This abortion is called directly by Progman
498 HGLOBAL16 WINAPI
InternalExtractIcon(HINSTANCE16 hInstance
,
499 LPCSTR lpszExeFileName
, UINT16 nIconIndex
,
503 HGLOBAL16
* RetPtr
= NULL
;
507 HFILE32 hFile
= OpenFile32( lpszExeFileName
, &ofs
, OF_READ
);
508 UINT16 iconDirCount
= 0,iconCount
= 0;
510 TRACE(shell
,"(%04x,file %s,start %d,extract %d\n",
511 hInstance
, lpszExeFileName
, nIconIndex
, n
);
513 if( hFile
== HFILE_ERROR32
|| !n
)
516 hRet
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(HICON16
)*n
);
517 RetPtr
= (HICON16
*)GlobalLock16(hRet
);
519 *RetPtr
= (n
== 0xFFFF)? 0: 1; /* error return values */
521 sig
= SHELL_GetResourceTable(hFile
,&pData
);
523 if((sig
== IMAGE_OS2_SIGNATURE
)
524 || (sig
== 1)) /* .ICO file */
527 NE_TYPEINFO
* pTInfo
= (NE_TYPEINFO
*)(pData
+ 2);
528 NE_NAMEINFO
* pIconStorage
= NULL
;
529 NE_NAMEINFO
* pIconDir
= NULL
;
530 LPicoICONDIR lpiID
= NULL
;
532 if( pData
== (BYTE
*)-1 )
534 /* check for .ICO file */
536 hIcon
= ICO_GetIconDirectory(hInstance
, hFile
, &lpiID
);
537 if( hIcon
) { iconDirCount
= 1; iconCount
= lpiID
->idCount
; }
539 else while( pTInfo
->type_id
&& !(pIconStorage
&& pIconDir
) )
541 /* find icon directory and icon repository */
543 if( pTInfo
->type_id
== NE_RSCTYPE_GROUP_ICON
)
545 iconDirCount
= pTInfo
->count
;
546 pIconDir
= ((NE_NAMEINFO
*)(pTInfo
+ 1));
547 TRACE(shell
,"\tfound directory - %i icon families\n", iconDirCount
);
549 if( pTInfo
->type_id
== NE_RSCTYPE_ICON
)
551 iconCount
= pTInfo
->count
;
552 pIconStorage
= ((NE_NAMEINFO
*)(pTInfo
+ 1));
553 TRACE(shell
,"\ttotal icons - %i\n", iconCount
);
555 pTInfo
= (NE_TYPEINFO
*)((char*)(pTInfo
+1)+pTInfo
->count
*sizeof(NE_NAMEINFO
));
558 /* load resources and create icons */
560 if( (pIconStorage
&& pIconDir
) || lpiID
)
561 if( nIconIndex
== (UINT16
)-1 ) RetPtr
[0] = iconDirCount
;
562 else if( nIconIndex
< iconDirCount
)
566 if( n
> iconDirCount
- nIconIndex
) n
= iconDirCount
- nIconIndex
;
568 for( i
= nIconIndex
; i
< nIconIndex
+ n
; i
++ )
570 /* .ICO files have only one icon directory */
573 hIcon
= SHELL_LoadResource( hInstance
, hFile
, pIconDir
+ i
,
575 RetPtr
[i
-nIconIndex
] = GetIconID( hIcon
, 3 );
579 for( icon
= nIconIndex
; icon
< nIconIndex
+ n
; icon
++ )
583 hIcon
= ICO_LoadIcon( hInstance
, hFile
,
584 lpiID
->idEntries
+ RetPtr
[icon
-nIconIndex
]);
586 for( i
= 0; i
< iconCount
; i
++ )
587 if( pIconStorage
[i
].id
== (RetPtr
[icon
-nIconIndex
] | 0x8000) )
588 hIcon
= SHELL_LoadResource( hInstance
, hFile
, pIconStorage
+ i
,
592 RetPtr
[icon
-nIconIndex
] = LoadIconHandler( hIcon
, TRUE
);
593 FarSetOwner( RetPtr
[icon
-nIconIndex
], GetExePtr(hInstance
) );
596 RetPtr
[icon
-nIconIndex
] = 0;
599 if( lpiID
) HeapFree( GetProcessHeap(), 0, lpiID
);
600 else HeapFree( GetProcessHeap(), 0, pData
);
602 if( sig
== IMAGE_NT_SIGNATURE
)
604 LPBYTE peimage
,idata
,igdata
;
605 PIMAGE_DOS_HEADER dheader
;
606 PIMAGE_NT_HEADERS pe_header
;
607 PIMAGE_SECTION_HEADER pe_sections
;
608 PIMAGE_RESOURCE_DIRECTORY rootresdir
,iconresdir
,icongroupresdir
;
609 PIMAGE_RESOURCE_DATA_ENTRY idataent
,igdataent
;
612 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent
;
613 CURSORICONDIR
**cids
;
615 fmapping
= CreateFileMapping32A(hFile
,NULL
,PAGE_READONLY
|SEC_COMMIT
,0,0,NULL
);
616 if (fmapping
== 0) { /* FIXME, INVALID_HANDLE_VALUE? */
617 WARN(shell
,"failed to create filemap.\n");
621 peimage
= MapViewOfFile(fmapping
,FILE_MAP_READ
,0,0,0);
623 WARN(shell
,"failed to mmap filemap.\n");
624 CloseHandle(fmapping
);
628 dheader
= (PIMAGE_DOS_HEADER
)peimage
;
629 /* it is a pe header, SHELL_GetResourceTable checked that */
630 pe_header
= (PIMAGE_NT_HEADERS
)(peimage
+dheader
->e_lfanew
);
631 /* probably makes problems with short PE headers... but I haven't seen
634 pe_sections
= (PIMAGE_SECTION_HEADER
)(((char*)pe_header
)+sizeof(*pe_header
));
636 for (i
=0;i
<pe_header
->FileHeader
.NumberOfSections
;i
++) {
637 if (pe_sections
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
639 /* FIXME: doesn't work when the resources are not in a seperate section */
640 if (pe_sections
[i
].VirtualAddress
== pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
) {
641 rootresdir
= (PIMAGE_RESOURCE_DIRECTORY
)((char*)peimage
+pe_sections
[i
].PointerToRawData
);
647 WARN(shell
,"haven't found section for resource directory.\n");
648 UnmapViewOfFile(peimage
);
649 CloseHandle(fmapping
);
653 icongroupresdir
= GetResDirEntryW(rootresdir
,RT_GROUP_ICON32W
,
654 (DWORD
)rootresdir
,FALSE
);
655 if (!icongroupresdir
) {
656 WARN(shell
,"No Icongroupresourcedirectory!\n");
657 UnmapViewOfFile(peimage
);
658 CloseHandle(fmapping
);
663 iconDirCount
= icongroupresdir
->NumberOfNamedEntries
+icongroupresdir
->NumberOfIdEntries
;
664 if( nIconIndex
== (UINT16
)-1 ) {
665 RetPtr
[0] = iconDirCount
;
666 UnmapViewOfFile(peimage
);
667 CloseHandle(fmapping
);
672 if (nIconIndex
>= iconDirCount
) {
673 WARN(shell
,"nIconIndex %d is larger than iconDirCount %d\n",
674 nIconIndex
,iconDirCount
);
675 UnmapViewOfFile(peimage
);
676 CloseHandle(fmapping
);
681 cids
= (CURSORICONDIR
**)HeapAlloc(GetProcessHeap(),0,n
*sizeof(CURSORICONDIR
*));
683 /* caller just wanted the number of entries */
685 xresent
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(icongroupresdir
+1);
686 /* assure we don't get too much ... */
687 if( n
> iconDirCount
- nIconIndex
) n
= iconDirCount
- nIconIndex
;
689 /* starting from specified index ... */
690 xresent
= xresent
+nIconIndex
;
692 for (i
=0;i
<n
;i
++,xresent
++) {
694 PIMAGE_RESOURCE_DIRECTORY resdir
;
696 /* go down this resource entry, name */
697 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)((DWORD
)rootresdir
+(xresent
->u2
.s
.OffsetToDirectory
));
698 /* default language (0) */
699 resdir
= GetResDirEntryW(resdir
,(LPWSTR
)0,(DWORD
)rootresdir
,TRUE
);
700 igdataent
= (PIMAGE_RESOURCE_DATA_ENTRY
)resdir
;
702 /* lookup address in mapped image for virtual address */
704 for (j
=0;j
<pe_header
->FileHeader
.NumberOfSections
;j
++) {
705 if (igdataent
->OffsetToData
< pe_sections
[j
].VirtualAddress
)
707 if (igdataent
->OffsetToData
+igdataent
->Size
> pe_sections
[j
].VirtualAddress
+pe_sections
[j
].SizeOfRawData
)
709 igdata
= peimage
+(igdataent
->OffsetToData
-pe_sections
[j
].VirtualAddress
+pe_sections
[j
].PointerToRawData
);
712 WARN(shell
,"no matching real address for icongroup!\n");
713 UnmapViewOfFile(peimage
);
714 CloseHandle(fmapping
);
719 cid
= (CURSORICONDIR
*)igdata
;
721 RetPtr
[i
] = LookupIconIdFromDirectoryEx32(igdata
,TRUE
,SYSMETRICS_CXICON
,SYSMETRICS_CYICON
,0);
723 iconresdir
=GetResDirEntryW(rootresdir
,RT_ICON32W
,
724 (DWORD
)rootresdir
,FALSE
);
726 WARN(shell
,"No Iconresourcedirectory!\n");
727 UnmapViewOfFile(peimage
);
728 CloseHandle(fmapping
);
733 PIMAGE_RESOURCE_DIRECTORY xresdir
;
735 xresdir
= GetResDirEntryW(iconresdir
,(LPWSTR
)RetPtr
[i
],(DWORD
)rootresdir
,FALSE
);
736 xresdir
= GetResDirEntryW(xresdir
,(LPWSTR
)0,(DWORD
)rootresdir
,TRUE
);
738 idataent
= (PIMAGE_RESOURCE_DATA_ENTRY
)xresdir
;
741 /* map virtual to address in image */
742 for (j
=0;j
<pe_header
->FileHeader
.NumberOfSections
;j
++) {
743 if (idataent
->OffsetToData
< pe_sections
[j
].VirtualAddress
)
745 if (idataent
->OffsetToData
+idataent
->Size
> pe_sections
[j
].VirtualAddress
+pe_sections
[j
].SizeOfRawData
)
747 idata
= peimage
+(idataent
->OffsetToData
-pe_sections
[j
].VirtualAddress
+pe_sections
[j
].PointerToRawData
);
750 WARN(shell
,"no matching real address found for icondata!\n");
754 RetPtr
[i
] = CreateIconFromResourceEx32(idata
,idataent
->Size
,TRUE
,0x00030000,SYSMETRICS_CXICON
,SYSMETRICS_CYICON
,0);
756 UnmapViewOfFile(peimage
);
757 CloseHandle(fmapping
);
762 /* return array with icon handles */
767 /*************************************************************************
768 * ExtractIcon16 (SHELL.34)
770 HICON16 WINAPI
ExtractIcon16( HINSTANCE16 hInstance
, LPCSTR lpszExeFileName
,
773 return ExtractIcon32A( hInstance
, lpszExeFileName
, nIconIndex
);
778 /*************************************************************************
779 * ExtractAssociatedIcon [SHELL.36]
781 * Return icon for given file (either from file itself or from associated
782 * executable) and patch parameters if needed.
784 HICON32 WINAPI
ExtractAssociatedIcon32A(HINSTANCE32 hInst
,LPSTR lpIconPath
,
787 return ExtractAssociatedIcon16(hInst
,lpIconPath
,lpiIcon
);
790 HICON16 WINAPI
ExtractAssociatedIcon16(HINSTANCE16 hInst
,LPSTR lpIconPath
,
796 hIcon
= ExtractIcon16(hInst
, lpIconPath
, *lpiIcon
);
799 { if( hIcon
== 1 ) /* no icons found in given file */
800 { char tempPath
[0x80];
801 UINT16 uRet
= FindExecutable16(lpIconPath
,NULL
,tempPath
);
803 if( uRet
> 32 && tempPath
[0] )
804 { strcpy(lpIconPath
,tempPath
);
805 hIcon
= ExtractIcon16(hInst
, lpIconPath
, *lpiIcon
);
813 *lpiIcon
= 2; /* MSDOS icon - we found .exe but no icons in it */
815 *lpiIcon
= 6; /* generic icon - found nothing */
817 GetModuleFileName16(hInst
, lpIconPath
, 0x80);
818 hIcon
= LoadIcon16( hInst
, MAKEINTRESOURCE16(*lpiIcon
));
823 /*************************************************************************
824 * FindEnvironmentString [SHELL.38]
826 * Returns a pointer into the DOS environment... Ugh.
828 LPSTR
SHELL_FindString(LPSTR lpEnv
, LPCSTR entry
)
834 for( ; *lpEnv
; lpEnv
+=strlen(lpEnv
)+1 )
835 { if( lstrncmpi32A(lpEnv
, entry
, l
) )
838 return (lpEnv
+ l
); /* empty entry */
839 else if ( *(lpEnv
+l
)== '=' )
840 return (lpEnv
+ l
+ 1);
845 SEGPTR WINAPI
FindEnvironmentString(LPSTR str
)
847 LPSTR lpEnv
,lpString
;
850 spEnv
= GetDOSEnvironment();
852 lpEnv
= (LPSTR
)PTR_SEG_TO_LIN(spEnv
);
853 lpString
= (spEnv
)?SHELL_FindString(lpEnv
, str
):NULL
;
855 if( lpString
) /* offset should be small enough */
856 return spEnv
+ (lpString
- lpEnv
);
860 /*************************************************************************
861 * DoEnvironmentSubst [SHELL.37]
863 * Replace %KEYWORD% in the str with the value of variable KEYWORD
864 * from "DOS" environment.
866 DWORD WINAPI
DoEnvironmentSubst(LPSTR str
,WORD length
)
868 LPSTR lpEnv
= (LPSTR
)PTR_SEG_TO_LIN(GetDOSEnvironment());
869 LPSTR lpBuffer
= (LPSTR
)HeapAlloc( GetProcessHeap(), 0, length
);
871 LPSTR lpbstr
= lpBuffer
;
873 CharToOem32A(str
,str
);
875 TRACE(shell
,"accept %s\n", str
);
877 while( *lpstr
&& lpbstr
- lpBuffer
< length
)
883 do { lpend
++; } while( *lpend
&& *lpend
!= '%' );
884 if( *lpend
== '%' && lpend
- lpstr
> 1 ) /* found key */
888 lpKey
= SHELL_FindString(lpEnv
, lpstr
+1);
889 if( lpKey
) /* found key value */
891 int l
= strlen(lpKey
);
893 if( l
> length
- (lpbstr
- lpBuffer
) - 1 )
895 WARN(shell
,"-- Env subst aborted - string too short\n");
899 strcpy(lpbstr
, lpKey
);
906 else break; /* back off and whine */
911 *lpbstr
++ = *lpstr
++;
915 if( lpstr
- str
== strlen(str
) )
917 strncpy(str
, lpBuffer
, length
);
923 TRACE(shell
,"-- return %s\n", str
);
925 OemToChar32A(str
,str
);
926 HeapFree( GetProcessHeap(), 0, lpBuffer
);
928 /* Return str length in the LOWORD
929 * and 1 in HIWORD if subst was successful.
931 return (DWORD
)MAKELONG(strlen(str
), length
);
934 /*************************************************************************
935 * ShellHookProc [SHELL.103]
936 * System-wide WH_SHELL hook.
938 LRESULT WINAPI
ShellHookProc(INT16 code
, WPARAM16 wParam
, LPARAM lParam
)
940 TRACE(shell
,"%i, %04x, %08x\n", code
, wParam
,
942 if( SHELL_hHook
&& SHELL_hWnd
)
947 case HSHELL_WINDOWCREATED
: uMsg
= uMsgWndCreated
; break;
948 case HSHELL_WINDOWDESTROYED
: uMsg
= uMsgWndDestroyed
; break;
949 case HSHELL_ACTIVATESHELLWINDOW
: uMsg
= uMsgShellActivate
;
951 PostMessage16( SHELL_hWnd
, uMsg
, wParam
, 0 );
953 return CallNextHookEx16( WH_SHELL
, code
, wParam
, lParam
);
956 /*************************************************************************
957 * RegisterShellHook [SHELL.102]
959 BOOL32 WINAPI
RegisterShellHook(HWND16 hWnd
, UINT16 uAction
)
960 { TRACE(shell
,"%04x [%u]\n", hWnd
, uAction
);
963 { case 2: /* register hWnd as a shell window */
965 { HMODULE16 hShell
= GetModuleHandle16( "SHELL" );
966 SHELL_hHook
= SetWindowsHookEx16( WH_SHELL
, ShellHookProc
, hShell
, 0 );
968 { uMsgWndCreated
= RegisterWindowMessage32A( lpstrMsgWndCreated
);
969 uMsgWndDestroyed
= RegisterWindowMessage32A( lpstrMsgWndDestroyed
);
970 uMsgShellActivate
= RegisterWindowMessage32A( lpstrMsgShellActivate
);
973 WARN(shell
,"-- unable to install ShellHookProc()!\n");
977 return ((SHELL_hWnd
= hWnd
) != 0);
981 WARN(shell
, "-- unknown code %i\n", uAction
);