2 * Shell Library Functions
11 #include "wine/winuser16.h"
12 #include "wine/winbase16.h"
13 #include "wine/shell16.h"
21 #include "cursoricon.h"
22 #include "sysmetrics.h"
27 #include "imagelist.h"
29 DECLARE_DEBUG_CHANNEL(exec
)
30 DECLARE_DEBUG_CHANNEL(shell
)
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 static const char* lpstrMsgWndCreated
= "OTHERWINDOWCREATED";
59 static const char* lpstrMsgWndDestroyed
= "OTHERWINDOWDESTROYED";
60 static const char* lpstrMsgShellActivate
= "ACTIVATESHELLWINDOW";
62 static HWND16 SHELL_hWnd
= 0;
63 static HHOOK SHELL_hHook
= 0;
64 static UINT16 uMsgWndCreated
= 0;
65 static UINT16 uMsgWndDestroyed
= 0;
66 static UINT16 uMsgShellActivate
= 0;
68 /*************************************************************************
69 * DragAcceptFiles32 [SHELL32.54]
71 void WINAPI
DragAcceptFiles(HWND hWnd
, BOOL b
)
78 exstyle
= GetWindowLongA(hWnd
,GWL_EXSTYLE
);
79 if (b
)exstyle
|= WS_EX_ACCEPTFILES
;
80 else exstyle
&= ~WS_EX_ACCEPTFILES
;
81 SetWindowLongA(hWnd
,GWL_EXSTYLE
,exstyle
);
84 /*************************************************************************
85 * DragAcceptFiles16 [SHELL.9]
87 void WINAPI
DragAcceptFiles16(HWND16 hWnd
, BOOL16 b
)
89 DragAcceptFiles(hWnd
, b
);
92 /*************************************************************************
93 * SHELL_DragQueryFile [internal]
96 static UINT
SHELL_DragQueryFile(LPSTR lpDrop
, LPWSTR lpwDrop
, UINT lFile
,
97 LPSTR lpszFile
, LPWSTR lpszwFile
, UINT lLength
)
103 while (i
++ < lFile
) {
104 while (*lpDrop
++); /* skip filename */
106 return (lFile
== 0xFFFFFFFF) ? i
: 0;
110 while (i
++ < lFile
) {
111 while (*lpwDrop
++); /* skip filename */
113 return (lFile
== 0xFFFFFFFF) ? i
: 0;
117 if (lpDrop
) i
= lstrlenA(lpDrop
);
118 if (lpwDrop
) i
= lstrlenW(lpwDrop
);
120 if (!lpszFile
&& !lpszwFile
) {
121 return i
; /* needed buffer size */
123 i
= (lLength
> i
) ? i
: lLength
;
125 if (lpDrop
) lstrcpynA (lpszFile
, lpDrop
, i
);
126 else lstrcpynWtoA(lpszFile
, lpwDrop
, i
);
128 if (lpDrop
) lstrcpynAtoW(lpszwFile
, lpDrop
, i
);
129 else lstrcpynW (lpszwFile
, lpwDrop
, i
);
134 /*************************************************************************
135 * DragQueryFile32A [SHELL32.81] [shell32.82]
137 UINT WINAPI
DragQueryFileA(HDROP hDrop
, UINT lFile
, LPSTR lpszFile
,
139 { /* hDrop is a global memory block allocated with GMEM_SHARE
140 * with DROPFILESTRUCT as a header and filenames following
141 * it, zero length filename is in the end */
143 LPDROPFILESTRUCT lpDropFileStruct
;
147 TRACE(shell
,"(%08x, %x, %p, %u)\n", hDrop
,lFile
,lpszFile
,lLength
);
149 lpDropFileStruct
= (LPDROPFILESTRUCT
) GlobalLock(hDrop
);
150 if(!lpDropFileStruct
)
153 lpCurrent
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->lSize
;
154 i
= SHELL_DragQueryFile(lpCurrent
, NULL
, lFile
, lpszFile
, NULL
, lLength
);
159 /*************************************************************************
160 * DragQueryFile32W [shell32.133]
162 UINT WINAPI
DragQueryFileW(HDROP hDrop
, UINT lFile
, LPWSTR lpszwFile
,
165 LPDROPFILESTRUCT lpDropFileStruct
;
169 TRACE(shell
,"(%08x, %x, %p, %u)\n", hDrop
,lFile
,lpszwFile
,lLength
);
171 lpDropFileStruct
= (LPDROPFILESTRUCT
) GlobalLock(hDrop
);
172 if(!lpDropFileStruct
)
175 lpwCurrent
= (LPWSTR
) lpDropFileStruct
+ lpDropFileStruct
->lSize
;
176 i
= SHELL_DragQueryFile(NULL
, lpwCurrent
, lFile
, NULL
, lpszwFile
,lLength
);
180 /*************************************************************************
181 * DragQueryFile16 [SHELL.11]
183 UINT16 WINAPI
DragQueryFile16(HDROP16 hDrop
, WORD wFile
, LPSTR lpszFile
,
185 { /* hDrop is a global memory block allocated with GMEM_SHARE
186 * with DROPFILESTRUCT as a header and filenames following
187 * it, zero length filename is in the end */
189 LPDROPFILESTRUCT16 lpDropFileStruct
;
193 TRACE(shell
,"(%04x, %x, %p, %u)\n", hDrop
,wFile
,lpszFile
,wLength
);
195 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
196 if(!lpDropFileStruct
)
199 lpCurrent
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->wSize
;
201 i
= (WORD
)SHELL_DragQueryFile(lpCurrent
, NULL
, wFile
==0xffff?0xffffffff:wFile
,
202 lpszFile
, NULL
, wLength
);
203 GlobalUnlock16(hDrop
);
209 /*************************************************************************
210 * DragFinish32 [SHELL32.80]
212 void WINAPI
DragFinish(HDROP h
)
214 GlobalFree((HGLOBAL
)h
);
217 /*************************************************************************
218 * DragFinish16 [SHELL.12]
220 void WINAPI
DragFinish16(HDROP16 h
)
222 GlobalFree16((HGLOBAL16
)h
);
226 /*************************************************************************
227 * DragQueryPoint32 [SHELL32.135]
229 BOOL WINAPI
DragQueryPoint(HDROP hDrop
, POINT
*p
)
231 LPDROPFILESTRUCT lpDropFileStruct
;
234 lpDropFileStruct
= (LPDROPFILESTRUCT
) GlobalLock(hDrop
);
236 memcpy(p
,&lpDropFileStruct
->ptMousePos
,sizeof(POINT
));
237 bRet
= lpDropFileStruct
->fInNonClientArea
;
243 /*************************************************************************
244 * DragQueryPoint16 [SHELL.13]
246 BOOL16 WINAPI
DragQueryPoint16(HDROP16 hDrop
, POINT16
*p
)
248 LPDROPFILESTRUCT16 lpDropFileStruct
;
251 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
253 memcpy(p
,&lpDropFileStruct
->ptMousePos
,sizeof(POINT16
));
254 bRet
= lpDropFileStruct
->fInNonClientArea
;
256 GlobalUnlock16(hDrop
);
260 /*************************************************************************
261 * SHELL_FindExecutable [Internal]
263 * Utility for code sharing between FindExecutable and ShellExecute
265 HINSTANCE
SHELL_FindExecutable( LPCSTR lpFile
,
268 { char *extension
= NULL
; /* pointer to file extension */
269 char tmpext
[5]; /* local copy to mung as we please */
270 char filetype
[256]; /* registry name for this filetype */
271 LONG filetypelen
=256; /* length of above */
272 char command
[256]; /* command from registry */
273 LONG commandlen
=256; /* This is the most DOS can handle :) */
274 char buffer
[256]; /* Used to GetProfileString */
275 HINSTANCE retval
=31; /* default - 'No association was found' */
276 char *tok
; /* token pointer */
277 int i
; /* random counter */
278 char xlpFile
[256]; /* result of SearchPath */
280 TRACE(shell
, "%s\n", (lpFile
!= NULL
?lpFile
:"-") );
282 lpResult
[0]='\0'; /* Start off with an empty return string */
284 /* trap NULL parameters on entry */
285 if (( lpFile
== NULL
) || ( lpResult
== NULL
) || ( lpOperation
== NULL
))
286 { WARN(exec
, "(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
287 lpFile
, lpOperation
, lpResult
);
288 return 2; /* File not found. Close enough, I guess. */
291 if (SearchPathA( NULL
, lpFile
,".exe",sizeof(xlpFile
),xlpFile
,NULL
))
292 { TRACE(shell
, "SearchPath32A returned non-zero\n");
296 /* First thing we need is the file's extension */
297 extension
= strrchr( xlpFile
, '.' ); /* Assume last "." is the one; */
298 /* File->Run in progman uses */
300 TRACE(shell
, "xlpFile=%s,extension=%s\n", xlpFile
, extension
);
302 if ((extension
== NULL
) || (extension
== &xlpFile
[strlen(xlpFile
)]))
303 { WARN(shell
, "Returning 31 - No association\n");
304 return 31; /* no association */
307 /* Make local copy & lowercase it for reg & 'programs=' lookup */
308 lstrcpynA( tmpext
, extension
, 5 );
309 CharLowerA( tmpext
);
310 TRACE(shell
, "%s file\n", tmpext
);
312 /* Three places to check: */
313 /* 1. win.ini, [windows], programs (NB no leading '.') */
314 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
315 /* 3. win.ini, [extensions], extension (NB no leading '.' */
316 /* All I know of the order is that registry is checked before */
317 /* extensions; however, it'd make sense to check the programs */
318 /* section first, so that's what happens here. */
320 /* See if it's a program - if GetProfileString fails, we skip this
321 * section. Actually, if GetProfileString fails, we've probably
322 * got a lot more to worry about than running a program... */
323 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
324 buffer
, sizeof(buffer
)) > 0 )
325 { for (i
=0;i
<strlen(buffer
); i
++) buffer
[i
]=tolower(buffer
[i
]);
327 tok
= strtok(buffer
, " \t"); /* ? */
330 if (strcmp(tok
, &tmpext
[1])==0) /* have to skip the leading "." */
332 strcpy(lpResult
, xlpFile
);
333 /* Need to perhaps check that the file has a path
335 TRACE(shell
, "found %s\n", lpResult
);
338 /* Greater than 32 to indicate success FIXME According to the
339 * docs, I should be returning a handle for the
340 * executable. Does this mean I'm supposed to open the
341 * executable file or something? More RTFM, I guess... */
343 tok
=strtok(NULL
, " \t");
348 if (RegQueryValue16( HKEY_CLASSES_ROOT
, tmpext
, filetype
,
349 &filetypelen
) == ERROR_SUCCESS
)
351 filetype
[filetypelen
]='\0';
352 TRACE(shell
, "File type: %s\n", filetype
);
354 /* Looking for ...buffer\shell\lpOperation\command */
355 strcat( filetype
, "\\shell\\" );
356 strcat( filetype
, lpOperation
);
357 strcat( filetype
, "\\command" );
359 if (RegQueryValue16( HKEY_CLASSES_ROOT
, filetype
, command
,
360 &commandlen
) == ERROR_SUCCESS
)
362 /* Is there a replace() function anywhere? */
363 command
[commandlen
]='\0';
364 strcpy( lpResult
, command
);
365 tok
=strstr( lpResult
, "%1" );
368 tok
[0]='\0'; /* truncate string at the percent */
369 strcat( lpResult
, xlpFile
); /* what if no dir in xlpFile? */
370 tok
=strstr( command
, "%1" );
371 if ((tok
!=NULL
) && (strlen(tok
)>2))
373 strcat( lpResult
, &tok
[2] );
376 retval
=33; /* FIXME see above */
379 else /* Check win.ini */
381 /* Toss the leading dot */
383 if ( GetProfileStringA( "extensions", extension
, "", command
,
384 sizeof(command
)) > 0)
386 if (strlen(command
)!=0)
388 strcpy( lpResult
, command
);
389 tok
=strstr( lpResult
, "^" ); /* should be ^.extension? */
393 strcat( lpResult
, xlpFile
); /* what if no dir in xlpFile? */
394 tok
=strstr( command
, "^" ); /* see above */
395 if ((tok
!= NULL
) && (strlen(tok
)>5))
397 strcat( lpResult
, &tok
[5]);
400 retval
=33; /* FIXME - see above */
405 TRACE(shell
, "returning %s\n", lpResult
);
409 /*************************************************************************
410 * ShellExecute16 [SHELL.20]
412 HINSTANCE16 WINAPI
ShellExecute16( HWND16 hWnd
, LPCSTR lpOperation
,
413 LPCSTR lpFile
, LPCSTR lpParameters
,
414 LPCSTR lpDirectory
, INT16 iShowCmd
)
415 { HINSTANCE16 retval
=31;
419 TRACE(shell
, "(%04x,'%s','%s','%s','%s',%x)\n",
420 hWnd
, lpOperation
? lpOperation
:"<null>", lpFile
? lpFile
:"<null>",
421 lpParameters
? lpParameters
: "<null>",
422 lpDirectory
? lpDirectory
: "<null>", iShowCmd
);
424 if (lpFile
==NULL
) return 0; /* should not happen */
425 if (lpOperation
==NULL
) /* default is open */
429 { GetCurrentDirectoryA( sizeof(old_dir
), old_dir
);
430 SetCurrentDirectoryA( lpDirectory
);
433 retval
= SHELL_FindExecutable( lpFile
, lpOperation
, cmd
);
435 if (retval
> 32) /* Found */
438 strcat(cmd
,lpParameters
);
441 TRACE(shell
,"starting %s\n",cmd
);
442 retval
= WinExec( cmd
, iShowCmd
);
445 SetCurrentDirectoryA( old_dir
);
449 /*************************************************************************
450 * FindExecutable16 (SHELL.21)
452 HINSTANCE16 WINAPI
FindExecutable16( LPCSTR lpFile
, LPCSTR lpDirectory
,
454 { return (HINSTANCE16
)FindExecutableA( lpFile
, lpDirectory
, lpResult
);
458 /*************************************************************************
459 * AboutDlgProc16 (SHELL.33)
461 BOOL16 WINAPI
AboutDlgProc16( HWND16 hWnd
, UINT16 msg
, WPARAM16 wParam
,
463 { return AboutDlgProc( hWnd
, msg
, wParam
, lParam
);
467 /*************************************************************************
468 * ShellAbout16 (SHELL.22)
470 BOOL16 WINAPI
ShellAbout16( HWND16 hWnd
, LPCSTR szApp
, LPCSTR szOtherStuff
,
472 { return ShellAboutA( hWnd
, szApp
, szOtherStuff
, hIcon
);
475 /*************************************************************************
476 * SHELL_GetResourceTable
478 static DWORD
SHELL_GetResourceTable(HFILE hFile
,LPBYTE
*retptr
)
479 { IMAGE_DOS_HEADER mz_header
;
486 _llseek( hFile
, 0, SEEK_SET
);
487 if ((_lread(hFile
,&mz_header
,sizeof(mz_header
)) != sizeof(mz_header
)) || (mz_header
.e_magic
!= IMAGE_DOS_SIGNATURE
))
489 if (mz_header
.e_cblp
== 1)
490 { /* ICONHEADER.idType, must be 1 */
491 *retptr
= (LPBYTE
)-1;
495 return 0; /* failed */
497 _llseek( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
499 if (_lread( hFile
, magic
, sizeof(magic
) ) != sizeof(magic
))
502 _llseek( hFile
, mz_header
.e_lfanew
, SEEK_SET
);
504 if (*(DWORD
*)magic
== IMAGE_NT_SIGNATURE
)
505 return IMAGE_NT_SIGNATURE
;
507 if (*(WORD
*)magic
== IMAGE_OS2_SIGNATURE
)
508 { IMAGE_OS2_HEADER ne_header
;
509 LPBYTE pTypeInfo
= (LPBYTE
)-1;
511 if (_lread(hFile
,&ne_header
,sizeof(ne_header
))!=sizeof(ne_header
))
514 if (ne_header
.ne_magic
!= IMAGE_OS2_SIGNATURE
)
517 size
= ne_header
.rname_tab_offset
- ne_header
.resource_tab_offset
;
519 if( size
> sizeof(NE_TYPEINFO
) )
520 { pTypeInfo
= (BYTE
*)HeapAlloc( GetProcessHeap(), 0, size
);
522 { _llseek(hFile
, mz_header
.e_lfanew
+ne_header
.resource_tab_offset
, SEEK_SET
);
523 if( _lread( hFile
, (char*)pTypeInfo
, size
) != size
)
524 { HeapFree( GetProcessHeap(), 0, pTypeInfo
);
530 return IMAGE_OS2_SIGNATURE
;
532 return 0; /* failed */
535 /*************************************************************************
538 static HGLOBAL16
SHELL_LoadResource(HINSTANCE16 hInst
, HFILE hFile
, NE_NAMEINFO
* pNInfo
, WORD sizeShift
)
540 HGLOBAL16 handle
= DirectResAlloc16( hInst
, 0x10, (DWORD
)pNInfo
->length
<< sizeShift
);
544 if( (ptr
= (BYTE
*)GlobalLock16( handle
)) )
545 { _llseek( hFile
, (DWORD
)pNInfo
->offset
<< sizeShift
, SEEK_SET
);
546 _lread( hFile
, (char*)ptr
, pNInfo
->length
<< sizeShift
);
552 /*************************************************************************
555 static HGLOBAL16
ICO_LoadIcon(HINSTANCE16 hInst
, HFILE hFile
, LPicoICONDIRENTRY lpiIDE
)
557 HGLOBAL16 handle
= DirectResAlloc16( hInst
, 0x10, lpiIDE
->dwBytesInRes
);
559 if( (ptr
= (BYTE
*)GlobalLock16( handle
)) )
560 { _llseek( hFile
, lpiIDE
->dwImageOffset
, SEEK_SET
);
561 _lread( hFile
, (char*)ptr
, lpiIDE
->dwBytesInRes
);
567 /*************************************************************************
568 * ICO_GetIconDirectory
570 * Read .ico file and build phony ICONDIR struct for GetIconID
572 static HGLOBAL16
ICO_GetIconDirectory(HINSTANCE16 hInst
, HFILE hFile
, LPicoICONDIR
* lplpiID
)
573 { WORD id
[3]; /* idReserved, idType, idCount */
578 _llseek( hFile
, 0, SEEK_SET
);
579 if( _lread(hFile
,(char*)id
,sizeof(id
)) != sizeof(id
) ) return 0;
583 * - see http://www.microsoft.com/win32dev/ui/icons.htm
586 if( id
[0] || id
[1] != 1 || !id
[2] ) return 0;
588 i
= id
[2]*sizeof(icoICONDIRENTRY
) ;
590 lpiID
= (LPicoICONDIR
)HeapAlloc( GetProcessHeap(), 0, i
+ sizeof(id
));
592 if( _lread(hFile
,(char*)lpiID
->idEntries
,i
) == i
)
593 { HGLOBAL16 handle
= DirectResAlloc16( hInst
, 0x10,
594 id
[2]*sizeof(CURSORICONDIRENTRY
) + sizeof(id
) );
596 { CURSORICONDIR
* lpID
= (CURSORICONDIR
*)GlobalLock16( handle
);
597 lpID
->idReserved
= lpiID
->idReserved
= id
[0];
598 lpID
->idType
= lpiID
->idType
= id
[1];
599 lpID
->idCount
= lpiID
->idCount
= id
[2];
600 for( i
=0; i
< lpiID
->idCount
; i
++ )
601 { memcpy((void*)(lpID
->idEntries
+ i
),
602 (void*)(lpiID
->idEntries
+ i
), sizeof(CURSORICONDIRENTRY
) - 2);
603 lpID
->idEntries
[i
].wResId
= i
;
611 HeapFree( GetProcessHeap(), 0, lpiID
);
615 /*************************************************************************
616 * InternalExtractIcon [SHELL.39]
618 * This abortion is called directly by Progman
620 HGLOBAL16 WINAPI
InternalExtractIcon16(HINSTANCE16 hInstance
,
621 LPCSTR lpszExeFileName
, UINT16 nIconIndex
, WORD n
)
622 { HGLOBAL16 hRet
= 0;
623 HGLOBAL16
* RetPtr
= NULL
;
627 HFILE hFile
= OpenFile( lpszExeFileName
, &ofs
, OF_READ
);
628 UINT16 iconDirCount
= 0,iconCount
= 0;
632 TRACE(shell
,"(%04x,file %s,start %d,extract %d\n",
633 hInstance
, lpszExeFileName
, nIconIndex
, n
);
635 if( hFile
== HFILE_ERROR
|| !n
)
638 hRet
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(HICON16
)*n
);
639 RetPtr
= (HICON16
*)GlobalLock16(hRet
);
641 *RetPtr
= (n
== 0xFFFF)? 0: 1; /* error return values */
643 sig
= SHELL_GetResourceTable(hFile
,&pData
);
645 if( sig
==IMAGE_OS2_SIGNATURE
|| sig
==1 ) /* .ICO file */
647 NE_TYPEINFO
* pTInfo
= (NE_TYPEINFO
*)(pData
+ 2);
648 NE_NAMEINFO
* pIconStorage
= NULL
;
649 NE_NAMEINFO
* pIconDir
= NULL
;
650 LPicoICONDIR lpiID
= NULL
;
652 if( pData
== (BYTE
*)-1 )
653 { hIcon
= ICO_GetIconDirectory(hInstance
, hFile
, &lpiID
); /* check for .ICO file */
655 { iconDirCount
= 1; iconCount
= lpiID
->idCount
;
658 else while( pTInfo
->type_id
&& !(pIconStorage
&& pIconDir
) )
659 { if( pTInfo
->type_id
== NE_RSCTYPE_GROUP_ICON
) /* find icon directory and icon repository */
660 { iconDirCount
= pTInfo
->count
;
661 pIconDir
= ((NE_NAMEINFO
*)(pTInfo
+ 1));
662 TRACE(shell
,"\tfound directory - %i icon families\n", iconDirCount
);
664 if( pTInfo
->type_id
== NE_RSCTYPE_ICON
)
665 { iconCount
= pTInfo
->count
;
666 pIconStorage
= ((NE_NAMEINFO
*)(pTInfo
+ 1));
667 TRACE(shell
,"\ttotal icons - %i\n", iconCount
);
669 pTInfo
= (NE_TYPEINFO
*)((char*)(pTInfo
+1)+pTInfo
->count
*sizeof(NE_NAMEINFO
));
672 /* load resources and create icons */
674 if( (pIconStorage
&& pIconDir
) || lpiID
)
675 { if( nIconIndex
== (UINT16
)-1 )
676 { RetPtr
[0] = iconDirCount
;
678 else if( nIconIndex
< iconDirCount
)
680 if( n
> iconDirCount
- nIconIndex
)
681 n
= iconDirCount
- nIconIndex
;
683 for( i
= nIconIndex
; i
< nIconIndex
+ n
; i
++ )
684 { /* .ICO files have only one icon directory */
687 hIcon
= SHELL_LoadResource( hInstance
, hFile
, pIconDir
+ i
, *(WORD
*)pData
);
688 RetPtr
[i
-nIconIndex
] = GetIconID16( hIcon
, 3 );
692 for( icon
= nIconIndex
; icon
< nIconIndex
+ n
; icon
++ )
695 { hIcon
= ICO_LoadIcon( hInstance
, hFile
, lpiID
->idEntries
+ RetPtr
[icon
-nIconIndex
]);
698 { for( i
= 0; i
< iconCount
; i
++ )
699 { if( pIconStorage
[i
].id
== (RetPtr
[icon
-nIconIndex
] | 0x8000) )
700 { hIcon
= SHELL_LoadResource( hInstance
, hFile
, pIconStorage
+ i
,*(WORD
*)pData
);
705 { RetPtr
[icon
-nIconIndex
] = LoadIconHandler16( hIcon
, TRUE
);
706 FarSetOwner16( RetPtr
[icon
-nIconIndex
], GetExePtr(hInstance
) );
709 { RetPtr
[icon
-nIconIndex
] = 0;
715 HeapFree( GetProcessHeap(), 0, lpiID
);
717 HeapFree( GetProcessHeap(), 0, pData
);
720 if( sig
== IMAGE_NT_SIGNATURE
)
721 { LPBYTE idata
,igdata
;
722 PIMAGE_DOS_HEADER dheader
;
723 PIMAGE_NT_HEADERS pe_header
;
724 PIMAGE_SECTION_HEADER pe_sections
;
725 PIMAGE_RESOURCE_DIRECTORY rootresdir
,iconresdir
,icongroupresdir
;
726 PIMAGE_RESOURCE_DATA_ENTRY idataent
,igdataent
;
728 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent
;
729 CURSORICONDIR
**cids
;
731 fmapping
= CreateFileMappingA(hFile
,NULL
,PAGE_READONLY
|SEC_COMMIT
,0,0,NULL
);
733 { /* FIXME, INVALID_HANDLE_VALUE? */
734 WARN(shell
,"failed to create filemap.\n");
736 goto end_2
; /* failure */
738 peimage
= MapViewOfFile(fmapping
,FILE_MAP_READ
,0,0,0);
740 { WARN(shell
,"failed to mmap filemap.\n");
742 goto end_2
; /* failure */
744 dheader
= (PIMAGE_DOS_HEADER
)peimage
;
746 /* it is a pe header, SHELL_GetResourceTable checked that */
747 pe_header
= (PIMAGE_NT_HEADERS
)(peimage
+dheader
->e_lfanew
);
749 /* probably makes problems with short PE headers... but I haven't seen
752 pe_sections
= (PIMAGE_SECTION_HEADER
)(((char*)pe_header
)+sizeof(*pe_header
));
755 for (i
=0;i
<pe_header
->FileHeader
.NumberOfSections
;i
++)
756 { if (pe_sections
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
758 /* FIXME: doesn't work when the resources are not in a seperate section */
759 if (pe_sections
[i
].VirtualAddress
== pe_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
)
760 { rootresdir
= (PIMAGE_RESOURCE_DIRECTORY
)((char*)peimage
+pe_sections
[i
].PointerToRawData
);
766 { WARN(shell
,"haven't found section for resource directory.\n");
767 goto end_4
; /* failure */
770 icongroupresdir
= GetResDirEntryW(rootresdir
,RT_GROUP_ICONW
, (DWORD
)rootresdir
,FALSE
);
772 if (!icongroupresdir
)
773 { WARN(shell
,"No Icongroupresourcedirectory!\n");
774 goto end_4
; /* failure */
777 iconDirCount
= icongroupresdir
->NumberOfNamedEntries
+icongroupresdir
->NumberOfIdEntries
;
779 if( nIconIndex
== (UINT16
)-1 )
780 { RetPtr
[0] = iconDirCount
;
781 goto end_3
; /* success */
784 if (nIconIndex
>= iconDirCount
)
785 { WARN(shell
,"nIconIndex %d is larger than iconDirCount %d\n",nIconIndex
,iconDirCount
);
787 goto end_4
; /* failure */
790 cids
= (CURSORICONDIR
**)HeapAlloc(GetProcessHeap(),0,n
*sizeof(CURSORICONDIR
*));
792 /* caller just wanted the number of entries */
793 xresent
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(icongroupresdir
+1);
795 /* assure we don't get too much ... */
796 if( n
> iconDirCount
- nIconIndex
)
797 { n
= iconDirCount
- nIconIndex
;
800 /* starting from specified index ... */
801 xresent
= xresent
+nIconIndex
;
803 for (i
=0;i
<n
;i
++,xresent
++)
804 { CURSORICONDIR
*cid
;
805 PIMAGE_RESOURCE_DIRECTORY resdir
;
807 /* go down this resource entry, name */
808 resdir
= (PIMAGE_RESOURCE_DIRECTORY
)((DWORD
)rootresdir
+(xresent
->u2
.s
.OffsetToDirectory
));
810 /* default language (0) */
811 resdir
= GetResDirEntryW(resdir
,(LPWSTR
)0,(DWORD
)rootresdir
,TRUE
);
812 igdataent
= (PIMAGE_RESOURCE_DATA_ENTRY
)resdir
;
814 /* lookup address in mapped image for virtual address */
817 for (j
=0;j
<pe_header
->FileHeader
.NumberOfSections
;j
++)
818 { if (igdataent
->OffsetToData
< pe_sections
[j
].VirtualAddress
)
820 if (igdataent
->OffsetToData
+igdataent
->Size
> pe_sections
[j
].VirtualAddress
+pe_sections
[j
].SizeOfRawData
)
822 igdata
= peimage
+(igdataent
->OffsetToData
-pe_sections
[j
].VirtualAddress
+pe_sections
[j
].PointerToRawData
);
826 { WARN(shell
,"no matching real address for icongroup!\n");
827 goto end_4
; /* failure */
830 cid
= (CURSORICONDIR
*)igdata
;
832 RetPtr
[i
] = LookupIconIdFromDirectoryEx(igdata
,TRUE
,SYSMETRICS_CXICON
,SYSMETRICS_CYICON
,0);
835 iconresdir
=GetResDirEntryW(rootresdir
,RT_ICONW
,(DWORD
)rootresdir
,FALSE
);
838 { WARN(shell
,"No Iconresourcedirectory!\n");
839 goto end_4
; /* failure */
843 { PIMAGE_RESOURCE_DIRECTORY xresdir
;
844 xresdir
= GetResDirEntryW(iconresdir
,(LPWSTR
)(DWORD
)RetPtr
[i
],(DWORD
)rootresdir
,FALSE
);
845 xresdir
= GetResDirEntryW(xresdir
,(LPWSTR
)0,(DWORD
)rootresdir
,TRUE
);
846 idataent
= (PIMAGE_RESOURCE_DATA_ENTRY
)xresdir
;
849 /* map virtual to address in image */
850 for (j
=0;j
<pe_header
->FileHeader
.NumberOfSections
;j
++)
851 { if (idataent
->OffsetToData
< pe_sections
[j
].VirtualAddress
)
853 if (idataent
->OffsetToData
+idataent
->Size
> pe_sections
[j
].VirtualAddress
+pe_sections
[j
].SizeOfRawData
)
855 idata
= peimage
+(idataent
->OffsetToData
-pe_sections
[j
].VirtualAddress
+pe_sections
[j
].PointerToRawData
);
858 { WARN(shell
,"no matching real address found for icondata!\n");
862 RetPtr
[i
] = CreateIconFromResourceEx(idata
,idataent
->Size
,TRUE
,0x00030000,SYSMETRICS_CXICON
,SYSMETRICS_CYICON
,0);
864 goto end_3
; /* sucess */
866 goto end_1
; /* return array with icon handles */
868 /* cleaning up (try & catch would be nicer) */
869 end_4
: hRet
= 0; /* failure */
870 end_3
: UnmapViewOfFile(peimage
); /* success */
871 end_2
: CloseHandle(fmapping
);
872 end_1
: _lclose( hFile
);
876 /*************************************************************************
877 * ExtractIcon16 (SHELL.34)
879 HICON16 WINAPI
ExtractIcon16( HINSTANCE16 hInstance
, LPCSTR lpszExeFileName
,
882 return ExtractIconA( hInstance
, lpszExeFileName
, nIconIndex
);
885 /*************************************************************************
886 * ExtractIconEx16 (SHELL.40)
888 HICON16 WINAPI
ExtractIconEx16(
889 LPCSTR lpszFile
, INT16 nIconIndex
, HICON16
*phiconLarge
,
890 HICON16
*phiconSmall
, UINT16 nIcons
892 HICON
*ilarge
,*ismall
;
897 ilarge
= (HICON
*)HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
901 ismall
= (HICON
*)HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
904 ret
= ExtractIconExA(lpszFile
,nIconIndex
,ilarge
,ismall
,nIcons
);
906 for (i
=0;i
<nIcons
;i
++)
907 phiconLarge
[i
]=ilarge
[i
];
908 HeapFree(GetProcessHeap(),0,ilarge
);
911 for (i
=0;i
<nIcons
;i
++)
912 phiconSmall
[i
]=ismall
[i
];
913 HeapFree(GetProcessHeap(),0,ismall
);
918 /*************************************************************************
919 * ExtractAssociatedIcon [SHELL.36]
921 * Return icon for given file (either from file itself or from associated
922 * executable) and patch parameters if needed.
924 HICON WINAPI
ExtractAssociatedIconA(HINSTANCE hInst
, LPSTR lpIconPath
, LPWORD lpiIcon
)
926 return ExtractAssociatedIcon16(hInst
,lpIconPath
,lpiIcon
);
929 HICON16 WINAPI
ExtractAssociatedIcon16(HINSTANCE16 hInst
, LPSTR lpIconPath
, LPWORD lpiIcon
)
934 hIcon
= ExtractIcon16(hInst
, lpIconPath
, *lpiIcon
);
937 { if( hIcon
== 1 ) /* no icons found in given file */
938 { char tempPath
[0x80];
939 UINT16 uRet
= FindExecutable16(lpIconPath
,NULL
,tempPath
);
941 if( uRet
> 32 && tempPath
[0] )
942 { strcpy(lpIconPath
,tempPath
);
943 hIcon
= ExtractIcon16(hInst
, lpIconPath
, *lpiIcon
);
951 *lpiIcon
= 2; /* MSDOS icon - we found .exe but no icons in it */
953 *lpiIcon
= 6; /* generic icon - found nothing */
955 GetModuleFileName16(hInst
, lpIconPath
, 0x80);
956 hIcon
= LoadIcon16( hInst
, MAKEINTRESOURCE16(*lpiIcon
));
961 /*************************************************************************
962 * FindEnvironmentString [SHELL.38]
964 * Returns a pointer into the DOS environment... Ugh.
966 LPSTR
SHELL_FindString(LPSTR lpEnv
, LPCSTR entry
)
972 for( ; *lpEnv
; lpEnv
+=strlen(lpEnv
)+1 )
973 { if( lstrncmpiA(lpEnv
, entry
, l
) )
976 return (lpEnv
+ l
); /* empty entry */
977 else if ( *(lpEnv
+l
)== '=' )
978 return (lpEnv
+ l
+ 1);
983 SEGPTR WINAPI
FindEnvironmentString16(LPSTR str
)
985 LPSTR lpEnv
,lpString
;
988 spEnv
= GetDOSEnvironment16();
990 lpEnv
= (LPSTR
)PTR_SEG_TO_LIN(spEnv
);
991 lpString
= (spEnv
)?SHELL_FindString(lpEnv
, str
):NULL
;
993 if( lpString
) /* offset should be small enough */
994 return spEnv
+ (lpString
- lpEnv
);
998 /*************************************************************************
999 * DoEnvironmentSubst [SHELL.37]
1001 * Replace %KEYWORD% in the str with the value of variable KEYWORD
1002 * from "DOS" environment.
1004 DWORD WINAPI
DoEnvironmentSubst16(LPSTR str
,WORD length
)
1006 LPSTR lpEnv
= (LPSTR
)PTR_SEG_TO_LIN(GetDOSEnvironment16());
1007 LPSTR lpBuffer
= (LPSTR
)HeapAlloc( GetProcessHeap(), 0, length
);
1009 LPSTR lpbstr
= lpBuffer
;
1011 CharToOemA(str
,str
);
1013 TRACE(shell
,"accept %s\n", str
);
1015 while( *lpstr
&& lpbstr
- lpBuffer
< length
)
1017 LPSTR lpend
= lpstr
;
1021 do { lpend
++; } while( *lpend
&& *lpend
!= '%' );
1022 if( *lpend
== '%' && lpend
- lpstr
> 1 ) /* found key */
1026 lpKey
= SHELL_FindString(lpEnv
, lpstr
+1);
1027 if( lpKey
) /* found key value */
1029 int l
= strlen(lpKey
);
1031 if( l
> length
- (lpbstr
- lpBuffer
) - 1 )
1033 WARN(shell
,"-- Env subst aborted - string too short\n");
1037 strcpy(lpbstr
, lpKey
);
1044 else break; /* back off and whine */
1049 *lpbstr
++ = *lpstr
++;
1053 if( lpstr
- str
== strlen(str
) )
1055 strncpy(str
, lpBuffer
, length
);
1061 TRACE(shell
,"-- return %s\n", str
);
1063 OemToCharA(str
,str
);
1064 HeapFree( GetProcessHeap(), 0, lpBuffer
);
1066 /* Return str length in the LOWORD
1067 * and 1 in HIWORD if subst was successful.
1069 return (DWORD
)MAKELONG(strlen(str
), length
);
1072 /*************************************************************************
1073 * ShellHookProc [SHELL.103]
1074 * System-wide WH_SHELL hook.
1076 LRESULT WINAPI
ShellHookProc16(INT16 code
, WPARAM16 wParam
, LPARAM lParam
)
1078 TRACE(shell
,"%i, %04x, %08x\n", code
, wParam
,
1080 if( SHELL_hHook
&& SHELL_hWnd
)
1085 case HSHELL_WINDOWCREATED
: uMsg
= uMsgWndCreated
; break;
1086 case HSHELL_WINDOWDESTROYED
: uMsg
= uMsgWndDestroyed
; break;
1087 case HSHELL_ACTIVATESHELLWINDOW
: uMsg
= uMsgShellActivate
;
1089 PostMessage16( SHELL_hWnd
, uMsg
, wParam
, 0 );
1091 return CallNextHookEx16( WH_SHELL
, code
, wParam
, lParam
);
1094 /*************************************************************************
1095 * RegisterShellHook [SHELL.102]
1097 BOOL WINAPI
RegisterShellHook16(HWND16 hWnd
, UINT16 uAction
)
1098 { TRACE(shell
,"%04x [%u]\n", hWnd
, uAction
);
1101 { case 2: /* register hWnd as a shell window */
1103 { HMODULE16 hShell
= GetModuleHandle16( "SHELL" );
1104 SHELL_hHook
= SetWindowsHookEx16( WH_SHELL
, ShellHookProc16
, hShell
, 0 );
1106 { uMsgWndCreated
= RegisterWindowMessageA( lpstrMsgWndCreated
);
1107 uMsgWndDestroyed
= RegisterWindowMessageA( lpstrMsgWndDestroyed
);
1108 uMsgShellActivate
= RegisterWindowMessageA( lpstrMsgShellActivate
);
1111 WARN(shell
,"-- unable to install ShellHookProc()!\n");
1115 return ((SHELL_hWnd
= hWnd
) != 0);
1119 WARN(shell
, "-- unknown code %i\n", uAction
);