Removed all non-standard common control headers from the include
[wine/hacks.git] / dlls / shell32 / shell.c
blob0e477b41701c760b0ad5e763653488aae51eeb27
1 /*
2 * Shell Library Functions
4 * 1998 Marcus Meissner
5 */
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <ctype.h>
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "wine/winuser16.h"
13 #include "wine/winbase16.h"
14 #include "wine/shell16.h"
15 #include "winerror.h"
16 #include "file.h"
17 #include "heap.h"
18 #include "ldt.h"
19 #include "module.h"
20 #include "neexe.h"
21 #include "dlgs.h"
22 #include "cursoricon.h"
23 #include "shellapi.h"
24 #include "shlobj.h"
25 #include "debugtools.h"
26 #include "winreg.h"
27 #include "syslevel.h"
28 #include "shlwapi.h"
30 DEFAULT_DEBUG_CHANNEL(shell);
31 DECLARE_DEBUG_CHANNEL(exec);
33 /* .ICO file ICONDIR definitions */
35 #include "pshpack1.h"
37 typedef struct
39 BYTE bWidth; /* Width, in pixels, of the image */
40 BYTE bHeight; /* Height, in pixels, of the image */
41 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
42 BYTE bReserved; /* Reserved ( must be 0) */
43 WORD wPlanes; /* Color Planes */
44 WORD wBitCount; /* Bits per pixel */
45 DWORD dwBytesInRes; /* How many bytes in this resource? */
46 DWORD dwImageOffset; /* Where in the file is this image? */
47 } icoICONDIRENTRY, *LPicoICONDIRENTRY;
49 typedef struct
51 WORD idReserved; /* Reserved (must be 0) */
52 WORD idType; /* Resource Type (1 for icons) */
53 WORD idCount; /* How many images? */
54 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
55 } icoICONDIR, *LPicoICONDIR;
57 #include "poppack.h"
59 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
60 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
61 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
63 static HWND16 SHELL_hWnd = 0;
64 static HHOOK SHELL_hHook = 0;
65 static UINT16 uMsgWndCreated = 0;
66 static UINT16 uMsgWndDestroyed = 0;
67 static UINT16 uMsgShellActivate = 0;
68 HINSTANCE16 SHELL_hInstance = 0;
69 HINSTANCE SHELL_hInstance32;
70 static int SHELL_Attach = 0;
72 /***********************************************************************
73 * SHELL_DllEntryPoint [SHELL.entry]
75 * Initialization code for shell.dll. Automatically loads the
76 * 32-bit shell32.dll to allow thunking up to 32-bit code.
78 * RETURNS:
80 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
81 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
83 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
84 Reason, hInst, ds, HeapSize, res1, res2);
86 switch(Reason)
88 case DLL_PROCESS_ATTACH:
89 SHELL_Attach++;
90 if (SHELL_hInstance)
92 ERR("shell.dll instantiated twice!\n");
94 * We should return FALSE here, but that will break
95 * most apps that use CreateProcess because we do
96 * not yet support seperate address-spaces.
98 return TRUE;
101 SHELL_hInstance = hInst;
102 if(!SHELL_hInstance32)
104 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
106 ERR("Could not load sibling shell32.dll\n");
107 return FALSE;
110 break;
112 case DLL_PROCESS_DETACH:
113 if(!--SHELL_Attach)
115 SHELL_hInstance = 0;
116 if(SHELL_hInstance32)
117 FreeLibrary(SHELL_hInstance32);
119 break;
121 return TRUE;
124 /*************************************************************************
125 * DragAcceptFiles16 [SHELL.9]
127 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
129 DragAcceptFiles(hWnd, b);
132 /*************************************************************************
133 * DragQueryFile16 [SHELL.11]
135 UINT16 WINAPI DragQueryFile16(
136 HDROP16 hDrop,
137 WORD wFile,
138 LPSTR lpszFile,
139 WORD wLength)
141 LPSTR lpDrop;
142 UINT i = 0;
143 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
145 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
147 if(!lpDropFileStruct) goto end;
149 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
150 wFile = (wFile==0xffff) ? 0xffffffff : wFile;
152 while (i++ < wFile)
154 while (*lpDrop++); /* skip filename */
155 if (!*lpDrop)
157 i = (wFile == 0xFFFFFFFF) ? i : 0;
158 goto end;
162 i = lstrlenA(lpDrop);
163 i++;
164 if (!lpszFile ) goto end; /* needed buffer size */
165 i = (wLength > i) ? i : wLength;
166 lstrcpynA (lpszFile, lpDrop, i);
167 end:
168 GlobalUnlock16(hDrop);
169 return i;
172 /*************************************************************************
173 * DragFinish16 [SHELL.12]
175 void WINAPI DragFinish16(HDROP16 h)
177 TRACE("\n");
178 GlobalFree16((HGLOBAL16)h);
182 /*************************************************************************
183 * DragQueryPoint16 [SHELL.13]
185 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
187 LPDROPFILESTRUCT16 lpDropFileStruct;
188 BOOL16 bRet;
189 TRACE("\n");
190 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
192 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
193 bRet = lpDropFileStruct->fInNonClientArea;
195 GlobalUnlock16(hDrop);
196 return bRet;
199 /*************************************************************************
200 * SHELL_FindExecutable [Internal]
202 * Utility for code sharing between FindExecutable and ShellExecute
204 HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
205 LPCSTR lpOperation,
206 LPSTR lpResult)
207 { char *extension = NULL; /* pointer to file extension */
208 char tmpext[5]; /* local copy to mung as we please */
209 char filetype[256]; /* registry name for this filetype */
210 LONG filetypelen=256; /* length of above */
211 char command[256]; /* command from registry */
212 LONG commandlen=256; /* This is the most DOS can handle :) */
213 char buffer[256]; /* Used to GetProfileString */
214 HINSTANCE retval=31; /* default - 'No association was found' */
215 char *tok; /* token pointer */
216 int i; /* random counter */
217 char xlpFile[256] = ""; /* result of SearchPath */
219 TRACE("%s\n", (lpFile != NULL?lpFile:"-") );
221 lpResult[0]='\0'; /* Start off with an empty return string */
223 /* trap NULL parameters on entry */
224 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
225 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
226 lpFile, lpOperation, lpResult);
227 return 2; /* File not found. Close enough, I guess. */
230 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
231 { TRACE("SearchPathA returned non-zero\n");
232 lpFile = xlpFile;
235 /* First thing we need is the file's extension */
236 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
237 /* File->Run in progman uses */
238 /* .\FILE.EXE :( */
239 TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
241 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
242 { WARN("Returning 31 - No association\n");
243 return 31; /* no association */
246 /* Make local copy & lowercase it for reg & 'programs=' lookup */
247 lstrcpynA( tmpext, extension, 5 );
248 CharLowerA( tmpext );
249 TRACE("%s file\n", tmpext);
251 /* Three places to check: */
252 /* 1. win.ini, [windows], programs (NB no leading '.') */
253 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
254 /* 3. win.ini, [extensions], extension (NB no leading '.' */
255 /* All I know of the order is that registry is checked before */
256 /* extensions; however, it'd make sense to check the programs */
257 /* section first, so that's what happens here. */
259 /* See if it's a program - if GetProfileString fails, we skip this
260 * section. Actually, if GetProfileString fails, we've probably
261 * got a lot more to worry about than running a program... */
262 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
263 buffer, sizeof(buffer)) > 0 )
264 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
266 tok = strtok(buffer, " \t"); /* ? */
267 while( tok!= NULL)
269 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
271 strcpy(lpResult, xlpFile);
272 /* Need to perhaps check that the file has a path
273 * attached */
274 TRACE("found %s\n", lpResult);
275 return 33;
277 /* Greater than 32 to indicate success FIXME According to the
278 * docs, I should be returning a handle for the
279 * executable. Does this mean I'm supposed to open the
280 * executable file or something? More RTFM, I guess... */
282 tok=strtok(NULL, " \t");
286 /* Check registry */
287 if (RegQueryValue16( HKEY_CLASSES_ROOT, tmpext, filetype,
288 &filetypelen ) == ERROR_SUCCESS )
290 filetype[filetypelen]='\0';
291 TRACE("File type: %s\n", filetype);
293 /* Looking for ...buffer\shell\lpOperation\command */
294 strcat( filetype, "\\shell\\" );
295 strcat( filetype, lpOperation );
296 strcat( filetype, "\\command" );
298 if (RegQueryValue16( HKEY_CLASSES_ROOT, filetype, command,
299 &commandlen ) == ERROR_SUCCESS )
301 LPSTR tmp;
302 char param[256];
303 LONG paramlen = 256;
306 /* Get the parameters needed by the application
307 from the associated ddeexec key */
308 tmp = strstr(filetype,"command");
309 tmp[0] = '\0';
310 strcat(filetype,"ddeexec");
312 if(RegQueryValue16( HKEY_CLASSES_ROOT, filetype, param,&paramlen ) == ERROR_SUCCESS)
314 strcat(command," ");
315 strcat(command,param);
316 commandlen += paramlen;
319 /* Is there a replace() function anywhere? */
320 command[commandlen]='\0';
321 strcpy( lpResult, command );
322 tok=strstr( lpResult, "%1" );
323 if (tok != NULL)
325 tok[0]='\0'; /* truncate string at the percent */
326 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
327 tok=strstr( command, "%1" );
328 if ((tok!=NULL) && (strlen(tok)>2))
330 strcat( lpResult, &tok[2] );
333 retval=33; /* FIXME see above */
336 else /* Check win.ini */
338 /* Toss the leading dot */
339 extension++;
340 if ( GetProfileStringA( "extensions", extension, "", command,
341 sizeof(command)) > 0)
343 if (strlen(command)!=0)
345 strcpy( lpResult, command );
346 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
347 if (tok != NULL)
349 tok[0]='\0';
350 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
351 tok=strstr( command, "^" ); /* see above */
352 if ((tok != NULL) && (strlen(tok)>5))
354 strcat( lpResult, &tok[5]);
357 retval=33; /* FIXME - see above */
362 TRACE("returning %s\n", lpResult);
363 return retval;
366 /*************************************************************************
367 * ShellExecute16 [SHELL.20]
369 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
370 LPCSTR lpFile, LPCSTR lpParameters,
371 LPCSTR lpDirectory, INT16 iShowCmd )
372 { HINSTANCE16 retval=31;
373 char old_dir[1024];
374 char cmd[1024] = "";
376 TRACE("(%04x,'%s','%s','%s','%s',%x)\n",
377 hWnd, lpOperation ? lpOperation:"<null>", lpFile ? lpFile:"<null>",
378 lpParameters ? lpParameters : "<null>",
379 lpDirectory ? lpDirectory : "<null>", iShowCmd);
381 if (lpFile==NULL) return 0; /* should not happen */
382 if (lpOperation==NULL) /* default is open */
383 lpOperation="open";
385 if (lpDirectory)
386 { GetCurrentDirectoryA( sizeof(old_dir), old_dir );
387 SetCurrentDirectoryA( lpDirectory );
390 /* First try to execute lpFile with lpParameters directly */
391 strcpy(cmd,lpFile);
392 strcat(cmd,lpParameters ? lpParameters : "");
394 retval = WinExec16( cmd, iShowCmd );
396 /* Unable to execute lpFile directly
397 Check if we can match an application to lpFile */
398 if(retval < 32)
400 cmd[0] = '\0';
401 retval = SHELL_FindExecutable( lpFile, lpOperation, cmd );
403 if (retval > 32) /* Found */
405 if (lpParameters)
407 strcat(cmd," ");
408 strcat(cmd,lpParameters);
410 retval = WinExec16( cmd, iShowCmd );
412 else if(PathIsURLA((LPSTR)lpFile)) /* File not found, check for URL */
414 char lpstrProtocol[256];
415 LONG cmdlen = 512;
416 LPSTR lpstrRes;
417 INT iSize;
419 lpstrRes = strchr(lpFile,':');
420 iSize = lpstrRes - lpFile;
422 /* Looking for ...protocol\shell\lpOperation\command */
423 strncpy(lpstrProtocol,lpFile,iSize);
424 lpstrProtocol[iSize]='\0';
425 strcat( lpstrProtocol, "\\shell\\" );
426 strcat( lpstrProtocol, lpOperation );
427 strcat( lpstrProtocol, "\\command" );
429 /* Remove File Protocol from lpFile */
430 /* In the case file://path/file */
431 if(!strncasecmp(lpFile,"file",iSize))
433 lpFile += iSize;
434 while(*lpFile == ':') lpFile++;
438 /* Get the application for the protocol and execute it */
439 if (RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, cmd,
440 &cmdlen ) == ERROR_SUCCESS )
442 LPSTR tok;
443 LPSTR tmp;
444 char param[256] = "";
445 LONG paramlen = 256;
447 /* Get the parameters needed by the application
448 from the associated ddeexec key */
449 tmp = strstr(lpstrProtocol,"command");
450 tmp[0] = '\0';
451 strcat(lpstrProtocol,"ddeexec");
453 if(RegQueryValue16( HKEY_CLASSES_ROOT, lpstrProtocol, param,&paramlen ) == ERROR_SUCCESS)
455 strcat(cmd," ");
456 strcat(cmd,param);
457 cmdlen += paramlen;
460 /* Is there a replace() function anywhere? */
461 cmd[cmdlen]='\0';
463 tok=strstr( cmd, "%1" );
464 if (tok != NULL)
466 tok[0]='\0'; /* truncate string at the percent */
467 strcat( cmd, lpFile ); /* what if no dir in xlpFile? */
468 tok=strstr( cmd, "%1" );
469 if ((tok!=NULL) && (strlen(tok)>2))
471 strcat( cmd, &tok[2] );
475 retval = WinExec16( cmd, iShowCmd );
478 /* Check if file specified is in the form www.??????.*** */
479 else if(!strncasecmp(lpFile,"www",3))
481 /* if so, append lpFile http:// and call ShellExecute */
482 char lpstrTmpFile[256] = "http://" ;
483 strcat(lpstrTmpFile,lpFile);
484 retval = ShellExecuteA(hWnd,lpOperation,lpstrTmpFile,NULL,NULL,0);
487 if (lpDirectory)
488 SetCurrentDirectoryA( old_dir );
489 return retval;
492 /*************************************************************************
493 * FindExecutable16 (SHELL.21)
495 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
496 LPSTR lpResult )
497 { return (HINSTANCE16)FindExecutableA( lpFile, lpDirectory, lpResult );
501 /*************************************************************************
502 * AboutDlgProc16 (SHELL.33)
504 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
505 LPARAM lParam )
506 { return AboutDlgProc( hWnd, msg, wParam, lParam );
510 /*************************************************************************
511 * ShellAbout16 (SHELL.22)
513 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
514 HICON16 hIcon )
515 { return ShellAboutA( hWnd, szApp, szOtherStuff, hIcon );
518 /*************************************************************************
519 * SHELL_GetResourceTable
521 static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
522 { IMAGE_DOS_HEADER mz_header;
523 char magic[4];
524 int size;
526 TRACE("\n");
528 *retptr = NULL;
529 _llseek( hFile, 0, SEEK_SET );
530 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
531 { /* .ICO file ? */
532 if (mz_header.e_cblp == 1)
533 { /* ICONHEADER.idType, must be 1 */
534 *retptr = (LPBYTE)-1;
535 return 1;
537 else
538 return 0; /* failed */
540 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
542 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
543 return 0;
545 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
547 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
548 return IMAGE_NT_SIGNATURE;
550 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
551 { IMAGE_OS2_HEADER ne_header;
552 LPBYTE pTypeInfo = (LPBYTE)-1;
554 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
555 return 0;
557 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
558 return 0;
560 size = ne_header.ne_restab - ne_header.ne_rsrctab;
562 if( size > sizeof(NE_TYPEINFO) )
563 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
564 if( pTypeInfo )
565 { _llseek(hFile, mz_header.e_lfanew+ne_header.ne_rsrctab, SEEK_SET);
566 if( _lread( hFile, (char*)pTypeInfo, size) != size )
567 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
568 pTypeInfo = NULL;
572 *retptr = pTypeInfo;
573 return IMAGE_OS2_SIGNATURE;
575 return 0; /* failed */
578 /*************************************************************************
579 * SHELL_LoadResource
581 static HGLOBAL16 SHELL_LoadResource(HINSTANCE16 hInst, HFILE hFile, NE_NAMEINFO* pNInfo, WORD sizeShift)
582 { BYTE* ptr;
583 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, (DWORD)pNInfo->length << sizeShift);
585 TRACE("\n");
587 if( (ptr = (BYTE*)GlobalLock16( handle )) )
588 { _llseek( hFile, (DWORD)pNInfo->offset << sizeShift, SEEK_SET);
589 _lread( hFile, (char*)ptr, pNInfo->length << sizeShift);
590 return handle;
592 return 0;
595 /*************************************************************************
596 * ICO_LoadIcon
598 static HGLOBAL16 ICO_LoadIcon(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIRENTRY lpiIDE)
599 { BYTE* ptr;
600 HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10, lpiIDE->dwBytesInRes);
601 TRACE("\n");
602 if( (ptr = (BYTE*)GlobalLock16( handle )) )
603 { _llseek( hFile, lpiIDE->dwImageOffset, SEEK_SET);
604 _lread( hFile, (char*)ptr, lpiIDE->dwBytesInRes);
605 return handle;
607 return 0;
610 /*************************************************************************
611 * ICO_GetIconDirectory
613 * Read .ico file and build phony ICONDIR struct for GetIconID
615 static HGLOBAL16 ICO_GetIconDirectory(HINSTANCE16 hInst, HFILE hFile, LPicoICONDIR* lplpiID )
616 { WORD id[3]; /* idReserved, idType, idCount */
617 LPicoICONDIR lpiID;
618 int i;
620 TRACE("\n");
621 _llseek( hFile, 0, SEEK_SET );
622 if( _lread(hFile,(char*)id,sizeof(id)) != sizeof(id) ) return 0;
624 /* check .ICO header
626 * - see http://www.microsoft.com/win32dev/ui/icons.htm
629 if( id[0] || id[1] != 1 || !id[2] ) return 0;
631 i = id[2]*sizeof(icoICONDIRENTRY) ;
633 lpiID = (LPicoICONDIR)HeapAlloc( GetProcessHeap(), 0, i + sizeof(id));
635 if( _lread(hFile,(char*)lpiID->idEntries,i) == i )
636 { HGLOBAL16 handle = DirectResAlloc16( hInst, 0x10,
637 id[2]*sizeof(CURSORICONDIRENTRY) + sizeof(id) );
638 if( handle )
639 { CURSORICONDIR* lpID = (CURSORICONDIR*)GlobalLock16( handle );
640 lpID->idReserved = lpiID->idReserved = id[0];
641 lpID->idType = lpiID->idType = id[1];
642 lpID->idCount = lpiID->idCount = id[2];
643 for( i=0; i < lpiID->idCount; i++ )
644 { memcpy((void*)(lpID->idEntries + i),
645 (void*)(lpiID->idEntries + i), sizeof(CURSORICONDIRENTRY) - 2);
646 lpID->idEntries[i].wResId = i;
648 *lplpiID = lpiID;
649 return handle;
652 /* fail */
654 HeapFree( GetProcessHeap(), 0, lpiID);
655 return 0;
658 /*************************************************************************
659 * InternalExtractIcon [SHELL.39]
661 * This abortion is called directly by Progman
663 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
664 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
665 { HGLOBAL16 hRet = 0;
666 HGLOBAL16* RetPtr = NULL;
667 LPBYTE pData;
668 OFSTRUCT ofs;
669 DWORD sig;
670 HFILE hFile = OpenFile( lpszExeFileName, &ofs, OF_READ );
671 UINT16 iconDirCount = 0,iconCount = 0;
672 LPBYTE peimage;
673 HANDLE fmapping;
675 TRACE("(%04x,file %s,start %d,extract %d\n",
676 hInstance, lpszExeFileName, nIconIndex, n);
678 if( hFile == HFILE_ERROR || !n )
679 return 0;
681 hRet = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(HICON16)*n);
682 RetPtr = (HICON16*)GlobalLock16(hRet);
684 *RetPtr = (n == 0xFFFF)? 0: 1; /* error return values */
686 sig = SHELL_GetResourceTable(hFile,&pData);
688 if( sig==IMAGE_OS2_SIGNATURE || sig==1 ) /* .ICO file */
689 { HICON16 hIcon = 0;
690 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(pData + 2);
691 NE_NAMEINFO* pIconStorage = NULL;
692 NE_NAMEINFO* pIconDir = NULL;
693 LPicoICONDIR lpiID = NULL;
695 if( pData == (BYTE*)-1 )
696 { hIcon = ICO_GetIconDirectory(hInstance, hFile, &lpiID); /* check for .ICO file */
697 if( hIcon )
698 { iconDirCount = 1; iconCount = lpiID->idCount;
701 else while( pTInfo->type_id && !(pIconStorage && pIconDir) )
702 { if( pTInfo->type_id == NE_RSCTYPE_GROUP_ICON ) /* find icon directory and icon repository */
703 { iconDirCount = pTInfo->count;
704 pIconDir = ((NE_NAMEINFO*)(pTInfo + 1));
705 TRACE("\tfound directory - %i icon families\n", iconDirCount);
707 if( pTInfo->type_id == NE_RSCTYPE_ICON )
708 { iconCount = pTInfo->count;
709 pIconStorage = ((NE_NAMEINFO*)(pTInfo + 1));
710 TRACE("\ttotal icons - %i\n", iconCount);
712 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1)+pTInfo->count*sizeof(NE_NAMEINFO));
715 /* load resources and create icons */
717 if( (pIconStorage && pIconDir) || lpiID )
718 { if( nIconIndex == (UINT16)-1 )
719 { RetPtr[0] = iconDirCount;
721 else if( nIconIndex < iconDirCount )
722 { UINT16 i, icon;
723 if( n > iconDirCount - nIconIndex )
724 n = iconDirCount - nIconIndex;
726 for( i = nIconIndex; i < nIconIndex + n; i++ )
727 { /* .ICO files have only one icon directory */
729 if( lpiID == NULL )
730 hIcon = SHELL_LoadResource( hInstance, hFile, pIconDir + i, *(WORD*)pData );
731 RetPtr[i-nIconIndex] = GetIconID16( hIcon, 3 );
732 GlobalFree16(hIcon);
735 for( icon = nIconIndex; icon < nIconIndex + n; icon++ )
736 { hIcon = 0;
737 if( lpiID )
738 { hIcon = ICO_LoadIcon( hInstance, hFile, lpiID->idEntries + RetPtr[icon-nIconIndex]);
740 else
741 { for( i = 0; i < iconCount; i++ )
742 { if( pIconStorage[i].id == (RetPtr[icon-nIconIndex] | 0x8000) )
743 { hIcon = SHELL_LoadResource( hInstance, hFile, pIconStorage + i,*(WORD*)pData );
747 if( hIcon )
748 { RetPtr[icon-nIconIndex] = LoadIconHandler16( hIcon, TRUE );
749 FarSetOwner16( RetPtr[icon-nIconIndex], GetExePtr(hInstance) );
751 else
752 { RetPtr[icon-nIconIndex] = 0;
757 if( lpiID )
758 HeapFree( GetProcessHeap(), 0, lpiID);
759 else
760 HeapFree( GetProcessHeap(), 0, pData);
763 if( sig == IMAGE_NT_SIGNATURE)
764 { LPBYTE idata,igdata;
765 PIMAGE_DOS_HEADER dheader;
766 PIMAGE_NT_HEADERS pe_header;
767 PIMAGE_SECTION_HEADER pe_sections;
768 PIMAGE_RESOURCE_DIRECTORY rootresdir,iconresdir,icongroupresdir;
769 PIMAGE_RESOURCE_DATA_ENTRY idataent,igdataent;
770 int i,j;
771 PIMAGE_RESOURCE_DIRECTORY_ENTRY xresent;
772 CURSORICONDIR **cids;
774 fmapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL);
775 if (fmapping == 0)
776 { /* FIXME, INVALID_HANDLE_VALUE? */
777 WARN("failed to create filemap.\n");
778 hRet = 0;
779 goto end_2; /* failure */
781 peimage = MapViewOfFile(fmapping,FILE_MAP_READ,0,0,0);
782 if (!peimage)
783 { WARN("failed to mmap filemap.\n");
784 hRet = 0;
785 goto end_2; /* failure */
787 dheader = (PIMAGE_DOS_HEADER)peimage;
789 /* it is a pe header, SHELL_GetResourceTable checked that */
790 pe_header = (PIMAGE_NT_HEADERS)(peimage+dheader->e_lfanew);
792 /* probably makes problems with short PE headers... but I haven't seen
793 * one yet...
795 pe_sections = (PIMAGE_SECTION_HEADER)(((char*)pe_header)+sizeof(*pe_header));
796 rootresdir = NULL;
798 for (i=0;i<pe_header->FileHeader.NumberOfSections;i++)
799 { if (pe_sections[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
800 continue;
801 /* FIXME: doesn't work when the resources are not in a seperate section */
802 if (pe_sections[i].VirtualAddress == pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
803 { rootresdir = (PIMAGE_RESOURCE_DIRECTORY)((char*)peimage+pe_sections[i].PointerToRawData);
804 break;
808 if (!rootresdir)
809 { WARN("haven't found section for resource directory.\n");
810 goto end_4; /* failure */
813 icongroupresdir = GetResDirEntryW(rootresdir,RT_GROUP_ICONW, (DWORD)rootresdir,FALSE);
815 if (!icongroupresdir)
816 { WARN("No Icongroupresourcedirectory!\n");
817 goto end_4; /* failure */
820 iconDirCount = icongroupresdir->NumberOfNamedEntries+icongroupresdir->NumberOfIdEntries;
822 if( nIconIndex == (UINT16)-1 )
823 { RetPtr[0] = iconDirCount;
824 goto end_3; /* success */
827 if (nIconIndex >= iconDirCount)
828 { WARN("nIconIndex %d is larger than iconDirCount %d\n",nIconIndex,iconDirCount);
829 GlobalFree16(hRet);
830 goto end_4; /* failure */
833 cids = (CURSORICONDIR**)HeapAlloc(GetProcessHeap(),0,n*sizeof(CURSORICONDIR*));
835 /* caller just wanted the number of entries */
836 xresent = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(icongroupresdir+1);
838 /* assure we don't get too much ... */
839 if( n > iconDirCount - nIconIndex )
840 { n = iconDirCount - nIconIndex;
843 /* starting from specified index ... */
844 xresent = xresent+nIconIndex;
846 for (i=0;i<n;i++,xresent++)
847 { CURSORICONDIR *cid;
848 PIMAGE_RESOURCE_DIRECTORY resdir;
850 /* go down this resource entry, name */
851 resdir = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)rootresdir+(xresent->u2.s.OffsetToDirectory));
853 /* default language (0) */
854 resdir = GetResDirEntryW(resdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
855 igdataent = (PIMAGE_RESOURCE_DATA_ENTRY)resdir;
857 /* lookup address in mapped image for virtual address */
858 igdata = NULL;
860 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
861 { if (igdataent->OffsetToData < pe_sections[j].VirtualAddress)
862 continue;
863 if (igdataent->OffsetToData+igdataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
864 continue;
865 igdata = peimage+(igdataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
868 if (!igdata)
869 { WARN("no matching real address for icongroup!\n");
870 goto end_4; /* failure */
872 /* found */
873 cid = (CURSORICONDIR*)igdata;
874 cids[i] = cid;
875 RetPtr[i] = LookupIconIdFromDirectoryEx(igdata,TRUE,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
878 iconresdir=GetResDirEntryW(rootresdir,RT_ICONW,(DWORD)rootresdir,FALSE);
880 if (!iconresdir)
881 { WARN("No Iconresourcedirectory!\n");
882 goto end_4; /* failure */
885 for (i=0;i<n;i++)
886 { PIMAGE_RESOURCE_DIRECTORY xresdir;
887 xresdir = GetResDirEntryW(iconresdir,(LPWSTR)(DWORD)RetPtr[i],(DWORD)rootresdir,FALSE);
888 xresdir = GetResDirEntryW(xresdir,(LPWSTR)0,(DWORD)rootresdir,TRUE);
889 idataent = (PIMAGE_RESOURCE_DATA_ENTRY)xresdir;
890 idata = NULL;
892 /* map virtual to address in image */
893 for (j=0;j<pe_header->FileHeader.NumberOfSections;j++)
894 { if (idataent->OffsetToData < pe_sections[j].VirtualAddress)
895 continue;
896 if (idataent->OffsetToData+idataent->Size > pe_sections[j].VirtualAddress+pe_sections[j].SizeOfRawData)
897 continue;
898 idata = peimage+(idataent->OffsetToData-pe_sections[j].VirtualAddress+pe_sections[j].PointerToRawData);
900 if (!idata)
901 { WARN("no matching real address found for icondata!\n");
902 RetPtr[i]=0;
903 continue;
905 RetPtr[i] = CreateIconFromResourceEx(idata,idataent->Size,TRUE,0x00030000,GetSystemMetrics(SM_CXICON),GetSystemMetrics(SM_CYICON),0);
907 goto end_3; /* sucess */
909 goto end_1; /* return array with icon handles */
911 /* cleaning up (try & catch would be nicer) */
912 end_4: hRet = 0; /* failure */
913 end_3: UnmapViewOfFile(peimage); /* success */
914 end_2: CloseHandle(fmapping);
915 end_1: _lclose( hFile);
916 return hRet;
919 /*************************************************************************
920 * ExtractIcon16 (SHELL.34)
922 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
923 UINT16 nIconIndex )
924 { TRACE("\n");
925 return ExtractIconA( hInstance, lpszExeFileName, nIconIndex );
928 /*************************************************************************
929 * ExtractIconEx16 (SHELL.40)
931 HICON16 WINAPI ExtractIconEx16(
932 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
933 HICON16 *phiconSmall, UINT16 nIcons
935 HICON *ilarge,*ismall;
936 UINT16 ret;
937 int i;
939 if (phiconLarge)
940 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
941 else
942 ilarge = NULL;
943 if (phiconSmall)
944 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
945 else
946 ismall = NULL;
947 ret = ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons);
948 if (ilarge) {
949 for (i=0;i<nIcons;i++)
950 phiconLarge[i]=ilarge[i];
951 HeapFree(GetProcessHeap(),0,ilarge);
953 if (ismall) {
954 for (i=0;i<nIcons;i++)
955 phiconSmall[i]=ismall[i];
956 HeapFree(GetProcessHeap(),0,ismall);
958 return ret;
961 /*************************************************************************
962 * ExtractAssociatedIconA
964 * Return icon for given file (either from file itself or from associated
965 * executable) and patch parameters if needed.
967 HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
968 { TRACE("\n");
969 return ExtractAssociatedIcon16(hInst,lpIconPath,lpiIcon);
972 /*************************************************************************
973 * ExtractAssociatedIcon [SHELL.36]
975 * Return icon for given file (either from file itself or from associated
976 * executable) and patch parameters if needed.
978 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
979 { HICON16 hIcon;
980 WORD wDummyIcon = 0;
982 TRACE("\n");
984 if(lpiIcon == NULL)
985 lpiIcon = &wDummyIcon;
987 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
989 if( hIcon < 2 )
990 { if( hIcon == 1 ) /* no icons found in given file */
991 { char tempPath[0x80];
992 UINT16 uRet = FindExecutable16(lpIconPath,NULL,tempPath);
994 if( uRet > 32 && tempPath[0] )
995 { strcpy(lpIconPath,tempPath);
996 hIcon = ExtractIcon16(hInst, lpIconPath, *lpiIcon);
997 if( hIcon > 2 )
998 return hIcon;
1000 else hIcon = 0;
1003 if( hIcon == 1 )
1004 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
1005 else
1006 *lpiIcon = 6; /* generic icon - found nothing */
1008 GetModuleFileName16(hInst, lpIconPath, 0x80);
1009 hIcon = LoadIcon16( hInst, MAKEINTRESOURCE16(*lpiIcon));
1011 return hIcon;
1014 /*************************************************************************
1015 * FindEnvironmentString [SHELL.38]
1017 * Returns a pointer into the DOS environment... Ugh.
1019 LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
1020 { UINT16 l;
1022 TRACE("\n");
1024 l = strlen(entry);
1025 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
1026 { if( lstrncmpiA(lpEnv, entry, l) )
1027 continue;
1028 if( !*(lpEnv+l) )
1029 return (lpEnv + l); /* empty entry */
1030 else if ( *(lpEnv+l)== '=' )
1031 return (lpEnv + l + 1);
1033 return NULL;
1036 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
1037 { SEGPTR spEnv;
1038 LPSTR lpEnv,lpString;
1039 TRACE("\n");
1041 spEnv = GetDOSEnvironment16();
1043 lpEnv = (LPSTR)PTR_SEG_TO_LIN(spEnv);
1044 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
1046 if( lpString ) /* offset should be small enough */
1047 return spEnv + (lpString - lpEnv);
1048 return (SEGPTR)NULL;
1051 /*************************************************************************
1052 * DoEnvironmentSubst [SHELL.37]
1054 * Replace %KEYWORD% in the str with the value of variable KEYWORD
1055 * from "DOS" environment.
1057 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
1059 LPSTR lpEnv = (LPSTR)PTR_SEG_TO_LIN(GetDOSEnvironment16());
1060 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
1061 LPSTR lpstr = str;
1062 LPSTR lpbstr = lpBuffer;
1064 CharToOemA(str,str);
1066 TRACE("accept %s\n", str);
1068 while( *lpstr && lpbstr - lpBuffer < length )
1070 LPSTR lpend = lpstr;
1072 if( *lpstr == '%' )
1074 do { lpend++; } while( *lpend && *lpend != '%' );
1075 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
1077 LPSTR lpKey;
1078 *lpend = '\0';
1079 lpKey = SHELL_FindString(lpEnv, lpstr+1);
1080 if( lpKey ) /* found key value */
1082 int l = strlen(lpKey);
1084 if( l > length - (lpbstr - lpBuffer) - 1 )
1086 WARN("-- Env subst aborted - string too short\n");
1087 *lpend = '%';
1088 break;
1090 strcpy(lpbstr, lpKey);
1091 lpbstr += l;
1093 else break;
1094 *lpend = '%';
1095 lpstr = lpend + 1;
1097 else break; /* back off and whine */
1099 continue;
1102 *lpbstr++ = *lpstr++;
1105 *lpbstr = '\0';
1106 if( lpstr - str == strlen(str) )
1108 strncpy(str, lpBuffer, length);
1109 length = 1;
1111 else
1112 length = 0;
1114 TRACE("-- return %s\n", str);
1116 OemToCharA(str,str);
1117 HeapFree( GetProcessHeap(), 0, lpBuffer);
1119 /* Return str length in the LOWORD
1120 * and 1 in HIWORD if subst was successful.
1122 return (DWORD)MAKELONG(strlen(str), length);
1125 /*************************************************************************
1126 * ShellHookProc [SHELL.103]
1127 * System-wide WH_SHELL hook.
1129 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
1131 TRACE("%i, %04x, %08x\n", code, wParam,
1132 (unsigned)lParam );
1133 if( SHELL_hHook && SHELL_hWnd )
1135 UINT16 uMsg = 0;
1136 switch( code )
1138 case HSHELL_WINDOWCREATED: uMsg = uMsgWndCreated; break;
1139 case HSHELL_WINDOWDESTROYED: uMsg = uMsgWndDestroyed; break;
1140 case HSHELL_ACTIVATESHELLWINDOW: uMsg = uMsgShellActivate;
1142 PostMessage16( SHELL_hWnd, uMsg, wParam, 0 );
1144 return CallNextHookEx16( WH_SHELL, code, wParam, lParam );
1147 /*************************************************************************
1148 * RegisterShellHook [SHELL.102]
1150 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
1152 TRACE("%04x [%u]\n", hWnd, uAction );
1154 switch( uAction )
1156 case 2: /* register hWnd as a shell window */
1157 if( !SHELL_hHook )
1159 HMODULE16 hShell = GetModuleHandle16( "SHELL" );
1160 HOOKPROC16 hookProc = (HOOKPROC16)NE_GetEntryPoint( hShell, 103 );
1161 SHELL_hHook = SetWindowsHookEx16( WH_SHELL, hookProc, hShell, 0 );
1162 if ( SHELL_hHook )
1164 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
1165 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
1166 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
1168 else
1169 WARN("-- unable to install ShellHookProc()!\n");
1172 if ( SHELL_hHook )
1173 return ((SHELL_hWnd = hWnd) != 0);
1174 break;
1176 default:
1177 WARN("-- unknown code %i\n", uAction );
1178 SHELL_hWnd = 0; /* just in case */
1180 return FALSE;