source: trunk/src/shell32/new/shell.cpp@ 797

Last change on this file since 797 was 797, checked in by phaller, 26 years ago

.

File size: 35.3 KB
Line 
1/*
2 * Shell Library Functions
3 *
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
12#include <odin.h>
13
14#define ICOM_CINTERFACE 1
15#define CINTERFACE 1
16
17#include "wine/winuser16.h"
18#include "wine/winbase16.h"
19#include "wine/shell16.h"
20#include "winerror.h"
21#include "file.h"
22#include "heap.h"
23#include "ldt.h"
24#include "module.h"
25#include "neexe.h"
26#include "dlgs.h"
27#include "cursoricon.h"
28#include "shellapi.h"
29#include "shlobj.h"
30#include "debugtools.h"
31#include "winreg.h"
32#include "syslevel.h"
33#include "imagelist.h"
34
35#include <heapstring.h>
36#include <misc.h>
37
38DECLARE_DEBUG_CHANNEL(exec)
39DECLARE_DEBUG_CHANNEL(shell)
40
41/* .ICO file ICONDIR definitions */
42
43#include "pshpack1.h"
44
45typedef struct
46{
47 BYTE bWidth; /* Width, in pixels, of the image */
48 BYTE bHeight; /* Height, in pixels, of the image */
49 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
50 BYTE bReserved; /* Reserved ( must be 0) */
51 WORD wPlanes; /* Color Planes */
52 WORD wBitCount; /* Bits per pixel */
53 DWORD dwBytesInRes; /* How many bytes in this resource? */
54 DWORD dwImageOffset; /* Where in the file is this image? */
55} icoICONDIRENTRY, *LPicoICONDIRENTRY;
56
57typedef struct
58{
59 WORD idReserved; /* Reserved (must be 0) */
60 WORD idType; /* Resource Type (1 for icons) */
61 WORD idCount; /* How many images? */
62 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
63} icoICONDIR, *LPicoICONDIR;
64
65#include "poppack.h"
66
67static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
68static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
69static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
70
71static HWND16 SHELL_hWnd = 0;
72static HHOOK SHELL_hHook = 0;
73static UINT16 uMsgWndCreated = 0;
74static UINT16 uMsgWndDestroyed = 0;
75static UINT16 uMsgShellActivate = 0;
76HINSTANCE16 SHELL_hInstance = 0;
77HINSTANCE SHELL_hInstance32;
78static int SHELL_Attach = 0;
79
80/***********************************************************************
81 * SHELL_DllEntryPoint [SHELL.entry]
82 *
83 * Initialization code for shell.dll. Automatically loads the
84 * 32-bit shell32.dll to allow thunking up to 32-bit code.
85 *
86 * RETURNS:
87 */
88BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
89 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
90{
91 TRACE_(shell)("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
92 Reason, hInst, ds, HeapSize, res1, res2);
93
94 switch(Reason)
95 {
96 case DLL_PROCESS_ATTACH:
97 SHELL_Attach++;
98 if (SHELL_hInstance)
99 {
100 ERR_(shell)("shell.dll instantiated twice!\n");
101 /*
102 * We should return FALSE here, but that will break
103 * most apps that use CreateProcess because we do
104 * not yet support seperate address-spaces.
105 */
106 return TRUE;
107 }
108
109 SHELL_hInstance = hInst;
110 if(!SHELL_hInstance32)
111 {
112 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
113 {
114 ERR_(shell)("Could not load sibling shell32.dll\n");
115 return FALSE;
116 }
117 }
118 break;
119
120 case DLL_PROCESS_DETACH:
121 if(!--SHELL_Attach)
122 {
123 SHELL_hInstance = 0;
124 if(SHELL_hInstance32)
125 FreeLibrary(SHELL_hInstance32);
126 }
127 break;
128 }
129 return TRUE;
130}
131
132/*************************************************************************
133 * DragAcceptFiles32 [SHELL32.54]
134 */
135void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
136{
137 LONG exstyle;
138
139
140 if( !IsWindow(hWnd) )
141 return;
142 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
143 if (b)exstyle |= WS_EX_ACCEPTFILES;
144 else exstyle &= ~WS_EX_ACCEPTFILES;
145 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
146}
147
148/*************************************************************************
149 * DragAcceptFiles16 [SHELL.9]
150 */
151void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
152{
153 DragAcceptFiles(hWnd, b);
154}
155
156/*************************************************************************
157 * SHELL_DragQueryFile [internal]
158 *
159 */
160static UINT SHELL_DragQueryFile(LPSTR lpDrop, LPWSTR lpwDrop, UINT lFile,
161 LPSTR lpszFile, LPWSTR lpszwFile, UINT lLength)
162{
163 UINT i;
164
165 i = 0;
166 if (lpDrop) {
167 while (i++ < lFile) {
168 while (*lpDrop++); /* skip filename */
169 if (!*lpDrop)
170 return (lFile == 0xFFFFFFFF) ? i : 0;
171 }
172 }
173 if (lpwDrop) {
174 while (i++ < lFile) {
175 while (*lpwDrop++); /* skip filename */
176 if (!*lpwDrop)
177 return (lFile == 0xFFFFFFFF) ? i : 0;
178 }
179 }
180
181 if (lpDrop) i = lstrlenA(lpDrop);
182 if (lpwDrop) i = lstrlenW(lpwDrop);
183 i++;
184 if (!lpszFile && !lpszwFile) {
185 return i; /* needed buffer size */
186 }
187 i = (lLength > i) ? i : lLength;
188 if (lpszFile) {
189 if (lpDrop) lstrcpynA (lpszFile, lpDrop, i);
190 else lstrcpynWtoA(lpszFile, lpwDrop, i);
191 } else {
192 if (lpDrop) lstrcpynAtoW(lpszwFile, lpDrop, i);
193 else lstrcpynW (lpszwFile, lpwDrop, i);
194 }
195 return i;
196}
197
198/*************************************************************************
199 * DragQueryFile32A [SHELL32.81] [shell32.82]
200 */
201UINT WINAPI DragQueryFileA(HDROP hDrop, UINT lFile, LPSTR lpszFile,
202 UINT lLength)
203{ /* hDrop is a global memory block allocated with GMEM_SHARE
204 * with DROPFILESTRUCT as a header and filenames following
205 * it, zero length filename is in the end */
206
207 LPDROPFILESTRUCT lpDropFileStruct;
208 LPSTR lpCurrent;
209 UINT i;
210
211 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
212
213 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
214 if(!lpDropFileStruct)
215 return 0;
216
217 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->lSize;
218 i = SHELL_DragQueryFile(lpCurrent, NULL, lFile, lpszFile, NULL, lLength);
219 GlobalUnlock(hDrop);
220 return i;
221}
222
223/*************************************************************************
224 * DragQueryFile32W [shell32.133]
225 */
226UINT WINAPI DragQueryFileW(HDROP hDrop, UINT lFile, LPWSTR lpszwFile,
227 UINT lLength)
228{
229 LPDROPFILESTRUCT lpDropFileStruct;
230 LPWSTR lpwCurrent;
231 UINT i;
232
233 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
234
235 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
236 if(!lpDropFileStruct)
237 return 0;
238
239 lpwCurrent = (LPWSTR) lpDropFileStruct + lpDropFileStruct->lSize;
240 i = SHELL_DragQueryFile(NULL, lpwCurrent, lFile, NULL, lpszwFile,lLength);
241 GlobalUnlock(hDrop);
242 return i;
243}
244/*************************************************************************
245 * DragQueryFile16 [SHELL.11]
246 */
247UINT16 WINAPI DragQueryFile16(HDROP16 hDrop, WORD wFile, LPSTR lpszFile,
248 WORD wLength)
249{ /* hDrop is a global memory block allocated with GMEM_SHARE
250 * with DROPFILESTRUCT as a header and filenames following
251 * it, zero length filename is in the end */
252
253 LPDROPFILESTRUCT16 lpDropFileStruct;
254 LPSTR lpCurrent;
255 WORD i;
256
257 TRACE_(shell)("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
258
259 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
260 if(!lpDropFileStruct)
261 return 0;
262
263 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
264
265 i = (WORD)SHELL_DragQueryFile(lpCurrent, NULL, wFile==0xffff?0xffffffff:wFile,
266 lpszFile, NULL, wLength);
267 GlobalUnlock16(hDrop);
268 return i;
269}
270
271
272
273/*************************************************************************
274 * DragFinish32 [SHELL32.80]
275 */
276void WINAPI DragFinish(HDROP h)
277{ TRACE_(shell)("\n");
278 GlobalFree((HGLOBAL)h);
279}
280
281/*************************************************************************
282 * DragFinish16 [SHELL.12]
283 */
284void WINAPI DragFinish16(HDROP16 h)
285{ TRACE_(shell)("\n");
286 GlobalFree16((HGLOBAL16)h);
287}
288
289
290/*************************************************************************
291 * DragQueryPoint32 [SHELL32.135]
292 */
293BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
294{
295 LPDROPFILESTRUCT lpDropFileStruct;
296 BOOL bRet;
297 TRACE_(shell)("\n");
298 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
299
300 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT));
301 bRet = lpDropFileStruct->fInNonClientArea;
302
303 GlobalUnlock(hDrop);
304 return bRet;
305}
306
307/*************************************************************************
308 * DragQueryPoint16 [SHELL.13]
309 */
310BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
311{
312 LPDROPFILESTRUCT16 lpDropFileStruct;
313 BOOL16 bRet;
314 TRACE_(shell)("\n");
315 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
316
317 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
318 bRet = lpDropFileStruct->fInNonClientArea;
319
320 GlobalUnlock16(hDrop);
321 return bRet;
322}
323
324/*************************************************************************
325 * SHELL_FindExecutable [Internal]
326 *
327 * Utility for code sharing between FindExecutable and ShellExecute
328 */
329HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
330 LPCSTR lpOperation,
331 LPSTR lpResult)
332{ char *extension = NULL; /* pointer to file extension */
333 char tmpext[5]; /* local copy to mung as we please */
334 char filetype[256]; /* registry name for this filetype */
335 LONG filetypelen=256; /* length of above */
336 char command[256]; /* command from registry */
337 LONG commandlen=256; /* This is the most DOS can handle :) */
338 char buffer[256]; /* Used to GetProfileString */
339 HINSTANCE retval=31; /* default - 'No association was found' */
340 char *tok; /* token pointer */
341 int i; /* random counter */
342 char xlpFile[256]; /* result of SearchPath */
343
344 TRACE_(shell)("%s\n", (lpFile != NULL?lpFile:"-") );
345
346 lpResult[0]='\0'; /* Start off with an empty return string */
347
348 /* trap NULL parameters on entry */
349 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
350 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
351 lpFile, lpOperation, lpResult);
352 return 2; /* File not found. Close enough, I guess. */
353 }
354
355 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
356 { TRACE_(shell)("SearchPath32A returned non-zero\n");
357 lpFile = xlpFile;
358 }
359
360 /* First thing we need is the file's extension */
361 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
362 /* File->Run in progman uses */
363 /* .\FILE.EXE :( */
364 TRACE_(shell)("xlpFile=%s,extension=%s\n", xlpFile, extension);
365
366 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
367 { WARN_(shell)("Returning 31 - No association\n");
368 return 31; /* no association */
369 }
370
371 /* Make local copy & lowercase it for reg & 'programs=' lookup */
372 lstrcpynA( tmpext, extension, 5 );
373 CharLowerA( tmpext );
374 TRACE_(shell)("%s file\n", tmpext);
375
376 /* Three places to check: */
377 /* 1. win.ini, [windows], programs (NB no leading '.') */
378 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
379 /* 3. win.ini, [extensions], extension (NB no leading '.' */
380 /* All I know of the order is that registry is checked before */
381 /* extensions; however, it'd make sense to check the programs */
382 /* section first, so that's what happens here. */
383
384 /* See if it's a program - if GetProfileString fails, we skip this
385 * section. Actually, if GetProfileString fails, we've probably
386 * got a lot more to worry about than running a program... */
387 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
388 buffer, sizeof(buffer)) > 0 )
389 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
390
391 tok = strtok(buffer, " \t"); /* ? */
392 while( tok!= NULL)
393 {
394 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
395 {
396 strcpy(lpResult, xlpFile);
397 /* Need to perhaps check that the file has a path
398 * attached */
399 TRACE_(shell)("found %s\n", lpResult);
400 return 33;
401
402 /* Greater than 32 to indicate success FIXME According to the
403 * docs, I should be returning a handle for the
404 * executable. Does this mean I'm supposed to open the
405 * executable file or something? More RTFM, I guess... */
406 }
407 tok=strtok(NULL, " \t");
408 }
409 }
410
411 /* Check registry */
412 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
413 &filetypelen ) == ERROR_SUCCESS )
414 {
415 filetype[filetypelen]='\0';
416 TRACE_(shell)("File type: %s\n", filetype);
417
418 /* Looking for ...buffer\shell\lpOperation\command */
419 strcat( filetype, "\\shell\\" );
420 strcat( filetype, lpOperation );
421 strcat( filetype, "\\command" );
422
423 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
424 &commandlen ) == ERROR_SUCCESS )
425 {
426 /* Is there a replace() function anywhere? */
427 command[commandlen]='\0';
428 strcpy( lpResult, command );
429 tok=strstr( lpResult, "%1" );
430 if (tok != NULL)
431 {
432 tok[0]='\0'; /* truncate string at the percent */
433 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
434 tok=strstr( command, "%1" );
435 if ((tok!=NULL) && (strlen(tok)>2))
436 {
437 strcat( lpResult, &tok[2] );
438 }
439 }
440 retval=33; /* FIXME see above */
441 }
442 }
443 else /* Check win.ini */
444 {
445 /* Toss the leading dot */
446 extension++;
447 if ( GetProfileStringA( "extensions", extension, "", command,
448 sizeof(command)) > 0)
449 {
450 if (strlen(command)!=0)
451 {
452 strcpy( lpResult, command );
453 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
454 if (tok != NULL)
455 {
456 tok[0]='\0';
457 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
458 tok=strstr( command, "^" ); /* see above */
459 if ((tok != NULL) && (strlen(tok)>5))
460 {
461 strcat( lpResult, &tok[5]);
462 }
463 }
464 retval=33; /* FIXME - see above */
465 }
466 }
467 }
468
469 TRACE_(shell)("returning %s\n", lpResult);
470 return retval;
471}
472
473/*************************************************************************
474 * ShellExecute16 [SHELL.20]
475 */
476HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
477 LPCSTR lpFile, LPCSTR lpParameters,
478 LPCSTR lpDirectory, INT16 iShowCmd )
479{ HINSTANCE16 retval=31;
480 char old_dir[1024];
481 char cmd[256];
482
483 TRACE_(shell)("(%04x,'%s','%s','%s','%s',%x)\n",
484 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
485 lpParameters ? lpParameters : "<null>",
486 lpDirectory ? lpDirectory : "<null>", iShowCmd);
487
488 if (lpFile==NULL) return 0; /* should not happen */
489 if (lpOperation==NULL) /* default is open */
490 lpOperation="open";
491
492 if (lpDirectory)
493 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
494 SetCurrentDirectoryA( lpDirectory );
495 }
496
497 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
498
499 if (retval > 32) /* Found */
500 {
501 if (lpParameters)
502 {
503 strcat(cmd," ");
504 strcat(cmd,lpParameters);
505 }
506
507 TRACE_(shell)("starting %s\n",cmd);
508 SYSLEVEL_ReleaseWin16Lock();
509 retval = WinExec( cmd, iShowCmd );
510 SYSLEVEL_RestoreWin16Lock();
511 }
512 if (lpDirectory)
513 SetCurrentDirectoryA( old_dir );
514 return retval;
515}
516
517/*************************************************************************
518 * FindExecutable16 (SHELL.21)
519 */
520HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
521 LPSTR lpResult )
522{ return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
523}
524
525
526/*************************************************************************
527 * AboutDlgProc16 (SHELL.33)
528 */
529BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
530 LPARAM lParam )
531{ return AboutDlgProc( hWnd, msg, wParam, lParam );
532}
533
534
535/*************************************************************************
536 * ShellAbout16 (SHELL.22)
537 */
538BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
539 HICON16 hIcon )
540{ return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
541}
542
543/*************************************************************************
544 * SHELL_GetResourceTable
545 */
546static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
547{ IMAGE_DOS_HEADER mz_header;
548 char magic[4];
549 int size;
550
551 TRACE_(shell)("\n");
552
553 *retptr = NULL;
554 _llseek( hFile, 0, SEEK_SET );
555 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
556 { /* .ICO file ? */
557 if (mz_header.e_cblp == 1)
558 { /* ICONHEADER.idType, must be 1 */
559 *retptr = (LPBYTE)-1;
560 return 1;
561 }
562 else
563 return 0; /* failed */
564 }
565 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
566
567 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
568 return 0;
569
570 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
571
572 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
573 return IMAGE_NT_SIGNATURE;
574
575 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
576 { IMAGE_OS2_HEADER ne_header;
577 LPBYTE pTypeInfo = (LPBYTE)-1;
578
579 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
580 return 0;
581
582 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
583 return 0;
584
585 size = ne_header.rname_tab_offset - ne_header.resource_tab_offset;
586
587 if( size > sizeof(NE_TYPEINFO) )
588 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
589 if( pTypeInfo )
590 { _llseek(hFile, mz_header.e_lfanew+ne_header.resource_tab_offset, SEEK_SET);
591 if( _lread( hFile, (char*)pTypeInfo, size) != size )
592 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
593 pTypeInfo = NULL;
594 }
595 }
596 }
597 *retptr = pTypeInfo;
598 return IMAGE_OS2_SIGNATURE;
599 }
600 return 0; /* failed */
601}
602
603/*************************************************************************
604 * SHELL_LoadResource
605 */
606static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
607{ BYTE* ptr;
608 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
609
610 TRACE_(shell)("\n");
611
612 if( (ptr = (BYTE*)GlobalLock16( handle )) )
613 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
614 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
615 return handle;
616 }
617 return 0;
618}
619
620/*************************************************************************
621 * ICO_LoadIcon
622 */
623static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
624{ BYTE* ptr;
625 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
626 TRACE_(shell)("\n");
627 if( (ptr = (BYTE*)GlobalLock16( handle )) )
628 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
629 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
630 return handle;
631 }
632 return 0;
633}
634
635/*************************************************************************
636 * ICO_GetIconDirectory
637 *
638 * Read .ico file and build phony ICONDIR struct for GetIconID
639 */
640static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIR* lplpiID )
641{ WORD id[3]; /* idReserved, idType, idCount */
642 LPicoICONDIR lpiID;
643 int i;
644
645 TRACE_(shell)("\n");
646 _llseek( hFile, 0, SEEK_SET );
647 if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
648
649 /* check .ICO header
650 *
651 * - see http://www.microsoft.com/win32dev/ui/icons.htm
652 */
653
654 if( id[0] || id[1] != 1 || !id[2] ) return 0;
655
656 i = id[2]*sizeof(icoICONDIRENTRY) ;
657
658 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, i + sizeof(id));
659
660 if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
661 { HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10,
662 id[2]*sizeof(CURSORICONDIRENTRY) + sizeof(id) );
663 if( handle )
664 { CURSORICONDIR* lpID = (CURSORICONDIR*)GlobalLock16( handle );
665 lpID->idReserved = lpiID->idReserved = id[0];
666 lpID->idType = lpiID->idType = id[1];
667 lpID->idCount = lpiID->idCount = id[2];
668 for( i=0; i < lpiID->idCount; i++ )
669 { memcpy((void*)(lpID->idEntries + i),
670 (void*)(lpiID->idEntries + i), sizeof(CURSORICONDIRENTRY) - 2);
671 lpID->idEntries[i].wResId = i;
672 }
673 *lplpiID = lpiID;
674 return handle;
675 }
676 }
677 /* fail */
678
679 HeapFree( GetProcessHeap(), 0, lpiID);
680 return 0;
681}
682
683/*************************************************************************
684 * InternalExtractIcon [SHELL.39]
685 *
686 * This abortion is called directly by Progman
687 */
688HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
689 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
690{ HGLOBAL16 hRet = 0;
691 HGLOBAL16* RetPtr = NULL;
692 LPBYTE pData;
693 OFSTRUCT ofs;
694 DWORD sig;
695 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
696 UINT16 iconDirCount = 0,iconCount = 0;
697 LPBYTE peimage;
698 HANDLE fmapping;
699
700 TRACE_(shell)("(%04x,file %s,start %d,extract %d\n",
701 hInstance, lpszExeFileName, nIconIndex, n);
702
703 if( hFile == HFILE_ERROR || !n )
704 return 0;
705
706 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
707 RetPtr = (HICON16*)GlobalLock16(hRet);
708
709 *RetPtr = (n == 0xFFFF)? 0: 1; /* error return values */
710
711 sig = SHELL_GetResourceTable(hFile,&pData);
712
713 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
714 { HICON16 hIcon = 0;
715 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
716 NE_NAMEINFO* pIconStorage = NULL;
717 NE_NAMEINFO* pIconDir = NULL;
718 LPicoICONDIR lpiID = NULL;
719
720 if( pData == (BYTE*)-1 )
721 { hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID); /* check for .ICO file */
722 if( hIcon )
723 { iconDirCount = 1; iconCount = lpiID->idCount;
724 }
725 }
726 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
727 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
728 { iconDirCount = pTInfo->count;
729 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
730 TRACE_(shell)("\tfound directory - %i icon families\n", iconDirCount);
731 }
732 if( pTInfo->type_id == NE_RSCTYPE_ICON )
733 { iconCount = pTInfo->count;
734 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
735 TRACE_(shell)("\ttotal icons - %i\n", iconCount);
736 }
737 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
738 }
739
740 /* load resources and create icons */
741
742 if( (pIconStorage && pIconDir) || lpiID )
743 { if( nIconIndex == (UINT16)-1 )
744 { RetPtr[0] = iconDirCount;
745 }
746 else if( nIconIndex < iconDirCount )
747 { UINT16 i, icon;
748 if( n > iconDirCount - nIconIndex )
749 n = iconDirCount - nIconIndex;
750
751 for( i = nIconIndex; i < nIconIndex + n; i++ )
752 { /* .ICO files have only one icon directory */
753
754 if( lpiID == NULL )
755 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
756 RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
757 GlobalFree16(hIcon);
758 }
759
760 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
761 { hIcon = 0;
762 if( lpiID )
763 { hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
764 }
765 else
766 { for( i = 0; i < iconCount; i++ )
767 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
768 { hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,*(WORD*)pData );
769 }
770 }
771 }
772 if( hIcon )
773 { RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
774 FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
775 }
776 else
777 { RetPtr[icon-nIconIndex] = 0;
778 }
779 }
780 }
781 }
782 if( lpiID )
783 HeapFree( GetProcessHeap(), 0, lpiID);
784 else
785 HeapFree( GetProcessHeap(), 0, pData);
786 }
787
788 if( sig == IMAGE_NT_SIGNATURE)
789 { LPBYTE idata,igdata;
790 PIMAGE_DOS_HEADER dheader;
791 PIMAGE_NT_HEADERS pe_header;
792 PIMAGE_SECTION_HEADER pe_sections;
793 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
794 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
795 int i,j;
796 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
797 CURSORICONDIR **cids;
798
799 fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
800 if (fmapping == 0)
801 { /* FIXME, INVALID_HANDLE_VALUE? */
802 WARN_(shell)("failed to create filemap.\n");
803 hRet = 0;
804 goto end_2; /* failure */
805 }
806 peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
807 if (!peimage)
808 { WARN_(shell)("failed to mmap filemap.\n");
809 hRet = 0;
810 goto end_2; /* failure */
811 }
812 dheader = (PIMAGE_DOS_HEADER)peimage;
813
814 /* it is a pe header, SHELL_GetResourceTable checked that */
815 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);
816
817 /* probably makes problems with short PE headers... but I haven't seen
818 * one yet...
819 */
820 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
821 rootresdir = NULL;
822
823 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
824 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
825 continue;
826 /* FIXME: doesn't work when the resources are not in a seperate section */
827 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
828 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
829 break;
830 }
831 }
832
833 if (!rootresdir)
834 { WARN_(shell)("haven't found section for resource directory.\n");
835 goto end_4; /* failure */
836 }
837
838 icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE);
839
840 if (!icongroupresdir)
841 { WARN_(shell)("No Icongroupresourcedirectory!\n");
842 goto end_4; /* failure */
843 }
844
845 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
846
847 if( nIconIndex == (UINT16)-1 )
848 { RetPtr[0] = iconDirCount;
849 goto end_3; /* success */
850 }
851
852 if (nIconIndex >= iconDirCount)
853 { WARN_(shell)("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
854 GlobalFree16(hRet);
855 goto end_4; /* failure */
856 }
857
858 cids = (CURSORICONDIR**)HeapAlloc(GetProcessHeap(),0,n*sizeof(CURSORICONDIR*));
859
860 /* caller just wanted the number of entries */
861 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
862
863 /* assure we don't get too much ... */
864 if( n > iconDirCount - nIconIndex )
865 { n = iconDirCount - nIconIndex;
866 }
867
868 /* starting from specified index ... */
869 xresent = xresent+nIconIndex;
870
871 for (i=0;i<n;i++,xresent++)
872 { CURSORICONDIR *cid;
873 PIMAGE_RESOURCE_DIRECTORY resdir;
874
875 /* go down this resource entry, name */
876 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
877
878 /* default language (0) */
879 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
880 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
881
882 /* lookup address in mapped image for virtual address */
883 igdata = NULL;
884
885 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
886 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
887 continue;
888 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
889 continue;
890 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
891 }
892
893 if (!igdata)
894 { WARN_(shell)("no matching real address for icongroup!\n");
895 goto end_4; /* failure */
896 }
897 /* found */
898 cid = (CURSORICONDIR*)igdata;
899 cids[i] = cid;
900 RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
901 }
902
903 iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE);
904
905 if (!iconresdir)
906 { WARN_(shell)("No Iconresourcedirectory!\n");
907 goto end_4; /* failure */
908 }
909
910 for (i=0;i<n;i++)
911 { PIMAGE_RESOURCE_DIRECTORY xresdir;
912 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
913 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
914 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
915 idata = NULL;
916
917 /* map virtual to address in image */
918 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
919 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
920 continue;
921 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
922 continue;
923 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
924 }
925 if (!idata)
926 { WARN_(shell)("no matching real address found for icondata!\n");
927 RetPtr[i]=0;
928 continue;
929 }
930 RetPtr[i] = CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
931 }
932 goto end_3; /* sucess */
933 }
934 goto end_1; /* return array with icon handles */
935
936/* cleaning up (try & catch would be nicer) */
937end_4: hRet = 0; /* failure */
938end_3: UnmapViewOfFile(peimage); /* success */
939end_2: CloseHandle(fmapping);
940end_1: _lclose( hFile);
941 return hRet;
942}
943
944/*************************************************************************
945 * ExtractIcon16 (SHELL.34)
946 */
947HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
948 UINT16 nIconIndex )
949{ TRACE_(shell)("\n");
950 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
951}
952
953/*************************************************************************
954 * ExtractIconEx16 (SHELL.40)
955 */
956HICON16 WINAPI ExtractIconEx16(
957 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
958 HICON16 *phiconSmall, UINT16 nIcons
959) {
960 HICON *ilarge,*ismall;
961 UINT16 ret;
962 int i;
963
964 if (phiconLarge)
965 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
966 else
967 ilarge = NULL;
968 if (phiconSmall)
969 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
970 else
971 ismall = NULL;
972 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
973 if (ilarge) {
974 for (i=0;i<nIcons;i++)
975 phiconLarge[i]=ilarge[i];
976 HeapFree(GetProcessHeap(),0,ilarge);
977 }
978 if (ismall) {
979 for (i=0;i<nIcons;i++)
980 phiconSmall[i]=ismall[i];
981 HeapFree(GetProcessHeap(),0,ismall);
982 }
983 return ret;
984}
985
986/*************************************************************************
987 * ExtractAssociatedIcon [SHELL.36]
988 *
989 * Return icon for given file (either from file itself or from associated
990 * executable) and patch parameters if needed.
991 */
992HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
993{ TRACE_(shell)("\n");
994 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
995}
996
997HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
998{ HICON16 hIcon;
999
1000 TRACE_(shell)("\n");
1001
1002 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
1003
1004 if( hIcon < 2 )
1005 { if( hIcon == 1 ) /* no icons found in given file */
1006 { char tempPath[0x80];
1007 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
1008
1009 if( uRet > 32 && tempPath[0] )
1010 { strcpy(lpIconPath,tempPath);
1011 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
1012 if( hIcon > 2 )
1013 return hIcon;
1014 }
1015 else hIcon = 0;
1016 }
1017
1018 if( hIcon == 1 )
1019 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
1020 else
1021 *lpiIcon = 6; /* generic icon - found nothing */
1022
1023 GetModuleFileName16(hInst, lpIconPath, 0x80);
1024 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
1025 }
1026 return hIcon;
1027}
1028
1029/*************************************************************************
1030 * FindEnvironmentString [SHELL.38]
1031 *
1032 * Returns a pointer into the DOS environment... Ugh.
1033 */
1034LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
1035{ UINT16 l;
1036
1037 TRACE_(shell)("\n");
1038
1039 l = strlen(entry);
1040 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
1041 { if( lstrncmpiA(lpEnv, entry, l) )
1042 continue;
1043 if( !*(lpEnv+l) )
1044 return (lpEnv + l); /* empty entry */
1045 else if ( *(lpEnv+l)== '=' )
1046 return (lpEnv + l + 1);
1047 }
1048 return NULL;
1049}
1050
1051SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
1052{ SEGPTR spEnv;
1053 LPSTR lpEnv,lpString;
1054 TRACE_(shell)("\n");
1055
1056 spEnv = GetDOSEnvironment16();
1057
1058 lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
1059 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
1060
1061 if( lpString ) /* offset should be small enough */
1062 return spEnv + (lpString - lpEnv);
1063 return (SEGPTR)NULL;
1064}
1065
1066/*************************************************************************
1067 * DoEnvironmentSubst [SHELL.37]
1068 *
1069 * Replace %KEYWORD% in the str with the value of variable KEYWORD
1070 * from "DOS" environment.
1071 */
1072DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
1073{
1074 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment16());
1075 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
1076 LPSTR lpstr = str;
1077 LPSTR lpbstr = lpBuffer;
1078
1079 CharToOemA(str,str);
1080
1081 TRACE_(shell)("accept %s\n", str);
1082
1083 while( *lpstr && lpbstr - lpBuffer < length )
1084 {
1085 LPSTR lpend = lpstr;
1086
1087 if( *lpstr == '%' )
1088 {
1089 do { lpend++; } while( *lpend && *lpend != '%' );
1090 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
1091 {
1092 LPSTR lpKey;
1093 *lpend = '\0';
1094 lpKey = SHELL_FindString(lpEnv, lpstr+1);
1095 if( lpKey ) /* found key value */
1096 {
1097 int l = strlen(lpKey);
1098
1099 if( l > length - (lpbstr - lpBuffer) - 1 )
1100 {
1101 WARN_(shell)("-- Env subst aborted - string too short\n");
1102 *lpend = '%';
1103 break;
1104 }
1105 strcpy(lpbstr, lpKey);
1106 lpbstr += l;
1107 }
1108 else break;
1109 *lpend = '%';
1110 lpstr = lpend + 1;
1111 }
1112 else break; /* back off and whine */
1113
1114 continue;
1115 }
1116
1117 *lpbstr++ = *lpstr++;
1118 }
1119
1120 *lpbstr = '\0';
1121 if( lpstr - str == strlen(str) )
1122 {
1123 strncpy(str, lpBuffer, length);
1124 length = 1;
1125 }
1126 else
1127 length = 0;
1128
1129 TRACE_(shell)("-- return %s\n", str);
1130
1131 OemToCharA(str,str);
1132 HeapFree( GetProcessHeap(), 0, lpBuffer);
1133
1134 /* Return str length in the LOWORD
1135 * and 1 in HIWORD if subst was successful.
1136 */
1137 return (DWORD)MAKELONG(strlen(str), length);
1138}
1139
1140/*************************************************************************
1141 * ShellHookProc [SHELL.103]
1142 * System-wide WH_SHELL hook.
1143 */
1144LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
1145{
1146 TRACE_(shell)("%i, %04x, %08x\n", code, wParam,
1147 (unsigned)lParam );
1148 if( SHELL_hHook && SHELL_hWnd )
1149 {
1150 UINT16 uMsg = 0;
1151 switch( code )
1152 {
1153 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
1154 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
1155 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
1156 }
1157 PostMessage16( SHELL_hWnd, uMsg, wParam, 0 );
1158 }
1159 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
1160}
1161
1162/*************************************************************************
1163 * RegisterShellHook [SHELL.102]
1164 */
1165BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
1166{ TRACE_(shell)("%04x [%u]\n", hWnd, uAction );
1167
1168 switch( uAction )
1169 { case 2: /* register hWnd as a shell window */
1170 if( !SHELL_hHook )
1171 { HMODULE16 hShell = GetModuleHandle16( "SHELL" );
1172 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, ShellHookProc16, hShell, 0 );
1173 if( SHELL_hHook )
1174 { uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
1175 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
1176 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
1177 }
1178 else
1179 WARN_(shell)("-- unable to install ShellHookProc()!\n");
1180 }
1181
1182 if( SHELL_hHook )
1183 return ((SHELL_hWnd = hWnd) != 0);
1184 break;
1185
1186 default:
1187 WARN_(shell)("-- unknown code %i\n", uAction );
1188 /* just in case */
1189 SHELL_hWnd = 0;
1190 }
1191 return FALSE;
1192}
Note: See TracBrowser for help on using the repository browser.