Added UnlockFileEx stub.
[wine/testsucceed.git] / misc / shell.c
blobe22f21bd41f01382ddccbb582be7dde90fe9c532
1 /*
2 * Shell Library Functions
4 * 1998 Marcus Meissner
5 */
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include "wine/winuser16.h"
12 #include "wine/winbase16.h"
13 #include "wine/shell16.h"
14 #include "winerror.h"
15 #include "file.h"
16 #include "heap.h"
17 #include "ldt.h"
18 #include "module.h"
19 #include "neexe.h"
20 #include "dlgs.h"
21 #include "cursoricon.h"
22 #include "shellapi.h"
23 #include "shlobj.h"
24 #include "debugtools.h"
25 #include "winreg.h"
26 #include "imagelist.h"
28 DECLARE_DEBUG_CHANNEL(exec)
29 DECLARE_DEBUG_CHANNEL(shell)
31 /* .ICO file ICONDIR definitions */
33 #include "pshpack1.h"
35 typedef struct
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;
47 typedef struct
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;
55 #include "poppack.h"
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)
72 LONG exstyle;
75 if( !IsWindow(hWnd) )
76 return;
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)
98 UINT i;
100 i = 0;
101 if (lpDrop) {
102 while (i++ < lFile) {
103 while (*lpDrop++); /* skip filename */
104 if (!*lpDrop)
105 return (lFile == 0xFFFFFFFF) ? i : 0;
108 if (lpwDrop) {
109 while (i++ < lFile) {
110 while (*lpwDrop++); /* skip filename */
111 if (!*lpwDrop)
112 return (lFile == 0xFFFFFFFF) ? i : 0;
116 if (lpDrop) i = lstrlenA(lpDrop);
117 if (lpwDrop) i = lstrlenW(lpwDrop);
118 i++;
119 if (!lpszFile && !lpszwFile) {
120 return i; /* needed buffer size */
122 i = (lLength > i) ? i : lLength;
123 if (lpszFile) {
124 if (lpDrop) lstrcpynA (lpszFile, lpDrop, i);
125 else lstrcpynWtoA(lpszFile, lpwDrop, i);
126 } else {
127 if (lpDrop) lstrcpynAtoW(lpszwFile, lpDrop, i);
128 else lstrcpynW (lpszwFile, lpwDrop, i);
130 return i;
133 /*************************************************************************
134 * DragQueryFile32A [SHELL32.81] [shell32.82]
136 UINT WINAPI DragQueryFileA(HDROP hDrop, UINT lFile, LPSTR lpszFile,
137 UINT lLength)
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;
143 LPSTR lpCurrent;
144 UINT i;
146 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
148 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
149 if(!lpDropFileStruct)
150 return 0;
152 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->lSize;
153 i = SHELL_DragQueryFile(lpCurrent, NULL, lFile, lpszFile, NULL, lLength);
154 GlobalUnlock(hDrop);
155 return i;
158 /*************************************************************************
159 * DragQueryFile32W [shell32.133]
161 UINT WINAPI DragQueryFileW(HDROP hDrop, UINT lFile, LPWSTR lpszwFile,
162 UINT lLength)
164 LPDROPFILESTRUCT lpDropFileStruct;
165 LPWSTR lpwCurrent;
166 UINT i;
168 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
170 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
171 if(!lpDropFileStruct)
172 return 0;
174 lpwCurrent = (LPWSTR) lpDropFileStruct + lpDropFileStruct->lSize;
175 i = SHELL_DragQueryFile(NULL, lpwCurrent, lFile, NULL, lpszwFile,lLength);
176 GlobalUnlock(hDrop);
177 return i;
179 /*************************************************************************
180 * DragQueryFile16 [SHELL.11]
182 UINT16 WINAPI DragQueryFile16(HDROP16 hDrop, WORD wFile, LPSTR lpszFile,
183 WORD wLength)
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;
189 LPSTR lpCurrent;
190 WORD i;
192 TRACE_(shell)("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
194 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
195 if(!lpDropFileStruct)
196 return 0;
198 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
200 i = (WORD)SHELL_DragQueryFile(lpCurrent, NULL, wFile==0xffff?0xffffffff:wFile,
201 lpszFile, NULL, wLength);
202 GlobalUnlock16(hDrop);
203 return i;
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;
231 BOOL bRet;
232 TRACE_(shell)("\n");
233 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
235 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT));
236 bRet = lpDropFileStruct->fInNonClientArea;
238 GlobalUnlock(hDrop);
239 return bRet;
242 /*************************************************************************
243 * DragQueryPoint16 [SHELL.13]
245 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
247 LPDROPFILESTRUCT16 lpDropFileStruct;
248 BOOL16 bRet;
249 TRACE_(shell)("\n");
250 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
252 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
253 bRet = lpDropFileStruct->fInNonClientArea;
255 GlobalUnlock16(hDrop);
256 return bRet;
259 /*************************************************************************
260 * SHELL_FindExecutable [Internal]
262 * Utility for code sharing between FindExecutable and ShellExecute
264 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
265 LPCSTR lpOperation,
266 LPSTR lpResult)
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");
292 lpFile = xlpFile;
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 */
298 /* .\FILE.EXE :( */
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"); /* ? */
327 while( tok!= NULL)
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
333 * attached */
334 TRACE_(shell)("found %s\n", lpResult);
335 return 33;
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");
346 /* Check registry */
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" );
365 if (tok != NULL)
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 */
381 extension++;
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? */
389 if (tok != NULL)
391 tok[0]='\0';
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);
405 return retval;
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;
415 char old_dir[1024];
416 char cmd[256];
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 */
425 lpOperation="open";
427 if (lpDirectory)
428 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
429 SetCurrentDirectoryA( lpDirectory );
432 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
434 if (retval > 32) /* Found */
436 if (lpParameters)
438 strcat(cmd," ");
439 strcat(cmd,lpParameters);
442 TRACE_(shell)("starting %s\n",cmd);
443 SYSLEVEL_ReleaseWin16Lock();
444 retval = WinExec( cmd, iShowCmd );
445 SYSLEVEL_RestoreWin16Lock();
447 if (lpDirectory)
448 SetCurrentDirectoryA( old_dir );
449 return retval;
452 /*************************************************************************
453 * FindExecutable16 (SHELL.21)
455 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
456 LPSTR lpResult )
457 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
461 /*************************************************************************
462 * AboutDlgProc16 (SHELL.33)
464 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
465 LPARAM lParam )
466 { return AboutDlgProc( hWnd, msg, wParam, lParam );
470 /*************************************************************************
471 * ShellAbout16 (SHELL.22)
473 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
474 HICON16 hIcon )
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;
483 char magic[4];
484 int size;
486 TRACE_(shell)("\n");
488 *retptr = NULL;
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))
491 { /* .ICO file ? */
492 if (mz_header.e_cblp == 1)
493 { /* ICONHEADER.idType, must be 1 */
494 *retptr = (LPBYTE)-1;
495 return 1;
497 else
498 return 0; /* failed */
500 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
502 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
503 return 0;
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))
515 return 0;
517 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
518 return 0;
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);
524 if( pTypeInfo )
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);
528 pTypeInfo = NULL;
532 *retptr = pTypeInfo;
533 return IMAGE_OS2_SIGNATURE;
535 return 0; /* failed */
538 /*************************************************************************
539 * SHELL_LoadResource
541 static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
542 { BYTE* ptr;
543 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
545 TRACE_(shell)("\n");
547 if( (ptr = (BYTE*)GlobalLock16( handle )) )
548 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
549 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
550 return handle;
552 return 0;
555 /*************************************************************************
556 * ICO_LoadIcon
558 static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
559 { BYTE* ptr;
560 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
561 TRACE_(shell)("\n");
562 if( (ptr = (BYTE*)GlobalLock16( handle )) )
563 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
564 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
565 return handle;
567 return 0;
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 */
577 LPicoICONDIR lpiID;
578 int i;
580 TRACE_(shell)("\n");
581 _llseek( hFile, 0, SEEK_SET );
582 if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
584 /* check .ICO header
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) );
598 if( handle )
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;
608 *lplpiID = lpiID;
609 return handle;
612 /* fail */
614 HeapFree( GetProcessHeap(), 0, lpiID);
615 return 0;
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;
627 LPBYTE pData;
628 OFSTRUCT ofs;
629 DWORD sig;
630 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
631 UINT16 iconDirCount = 0,iconCount = 0;
632 LPBYTE peimage;
633 HANDLE fmapping;
635 TRACE_(shell)("(%04x,file %s,start %d,extract %d\n",
636 hInstance, lpszExeFileName, nIconIndex, n);
638 if( hFile == HFILE_ERROR || !n )
639 return 0;
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 */
649 { HICON16 hIcon = 0;
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 */
657 if( hIcon )
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 )
682 { UINT16 i, icon;
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 */
689 if( lpiID == NULL )
690 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
691 RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
692 GlobalFree16(hIcon);
695 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
696 { hIcon = 0;
697 if( lpiID )
698 { hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
700 else
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 );
707 if( hIcon )
708 { RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
709 FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
711 else
712 { RetPtr[icon-nIconIndex] = 0;
717 if( lpiID )
718 HeapFree( GetProcessHeap(), 0, lpiID);
719 else
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;
730 int i,j;
731 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
732 CURSORICONDIR **cids;
734 fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
735 if (fmapping == 0)
736 { /* FIXME, INVALID_HANDLE_VALUE? */
737 WARN_(shell)("failed to create filemap.\n");
738 hRet = 0;
739 goto end_2; /* failure */
741 peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
742 if (!peimage)
743 { WARN_(shell)("failed to mmap filemap.\n");
744 hRet = 0;
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
753 * one yet...
755 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
756 rootresdir = NULL;
758 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
759 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
760 continue;
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);
764 break;
768 if (!rootresdir)
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);
789 GlobalFree16(hRet);
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 */
818 igdata = NULL;
820 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
821 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
822 continue;
823 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
824 continue;
825 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
828 if (!igdata)
829 { WARN_(shell)("no matching real address for icongroup!\n");
830 goto end_4; /* failure */
832 /* found */
833 cid = (CURSORICONDIR*)igdata;
834 cids[i] = cid;
835 RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
838 iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE);
840 if (!iconresdir)
841 { WARN_(shell)("No Iconresourcedirectory!\n");
842 goto end_4; /* failure */
845 for (i=0;i<n;i++)
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;
850 idata = NULL;
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)
855 continue;
856 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
857 continue;
858 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
860 if (!idata)
861 { WARN_(shell)("no matching real address found for icondata!\n");
862 RetPtr[i]=0;
863 continue;
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);
876 return hRet;
879 /*************************************************************************
880 * ExtractIcon16 (SHELL.34)
882 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
883 UINT16 nIconIndex )
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;
896 UINT16 ret;
897 int i;
899 if (phiconLarge)
900 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
901 else
902 ilarge = NULL;
903 if (phiconSmall)
904 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
905 else
906 ismall = NULL;
907 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
908 if (ilarge) {
909 for (i=0;i<nIcons;i++)
910 phiconLarge[i]=ilarge[i];
911 HeapFree(GetProcessHeap(),0,ilarge);
913 if (ismall) {
914 for (i=0;i<nIcons;i++)
915 phiconSmall[i]=ismall[i];
916 HeapFree(GetProcessHeap(),0,ismall);
918 return ret;
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)
933 { HICON16 hIcon;
935 TRACE_(shell)("\n");
937 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
939 if( hIcon < 2 )
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);
947 if( hIcon > 2 )
948 return hIcon;
950 else hIcon = 0;
953 if( hIcon == 1 )
954 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
955 else
956 *lpiIcon = 6; /* generic icon - found nothing */
958 GetModuleFileName16(hInst, lpIconPath, 0x80);
959 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
961 return hIcon;
964 /*************************************************************************
965 * FindEnvironmentString [SHELL.38]
967 * Returns a pointer into the DOS environment... Ugh.
969 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
970 { UINT16 l;
972 TRACE_(shell)("\n");
974 l = strlen(entry);
975 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
976 { if( lstrncmpiA(lpEnv, entry, l) )
977 continue;
978 if( !*(lpEnv+l) )
979 return (lpEnv + l); /* empty entry */
980 else if ( *(lpEnv+l)== '=' )
981 return (lpEnv + l + 1);
983 return NULL;
986 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
987 { SEGPTR spEnv;
988 LPSTR lpEnv,lpString;
989 TRACE_(shell)("\n");
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);
998 return (SEGPTR)NULL;
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);
1011 LPSTR lpstr = str;
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;
1022 if( *lpstr == '%' )
1024 do { lpend++; } while( *lpend && *lpend != '%' );
1025 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
1027 LPSTR lpKey;
1028 *lpend = '\0';
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");
1037 *lpend = '%';
1038 break;
1040 strcpy(lpbstr, lpKey);
1041 lpbstr += l;
1043 else break;
1044 *lpend = '%';
1045 lpstr = lpend + 1;
1047 else break; /* back off and whine */
1049 continue;
1052 *lpbstr++ = *lpstr++;
1055 *lpbstr = '\0';
1056 if( lpstr - str == strlen(str) )
1058 strncpy(str, lpBuffer, length);
1059 length = 1;
1061 else
1062 length = 0;
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,
1082 (unsigned)lParam );
1083 if( SHELL_hHook && SHELL_hWnd )
1085 UINT16 uMsg = 0;
1086 switch( code )
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 );
1103 switch( uAction )
1104 { case 2: /* register hWnd as a shell window */
1105 if( !SHELL_hHook )
1106 { HMODULE16 hShell = GetModuleHandle16( "SHELL" );
1107 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, ShellHookProc16, hShell, 0 );
1108 if( SHELL_hHook )
1109 { uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
1110 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
1111 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
1113 else
1114 WARN_(shell)("-- unable to install ShellHookProc()!\n");
1117 if( SHELL_hHook )
1118 return ((SHELL_hWnd = hWnd) != 0);
1119 break;
1121 default:
1122 WARN_(shell)("-- unknown code %i\n", uAction );
1123 /* just in case */
1124 SHELL_hWnd = 0;
1126 return FALSE;