source: trunk/src/shell32/shell.c@ 8048

Last change on this file since 8048 was 8048, checked in by sandervl, 23 years ago

PH: Wine resync + OS2 integration

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