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

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

Add: shell32/new - a direct port of WINE's Shell32 implmentation

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