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

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

.

File size: 15.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#define INITGUID
17
18//#include "wine/winuser16.h"
19//#include "wine/winbase16.h"
20//#include "wine/shell16.h"
21#include "winerror.h"
22//#include "file.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//#include "syslevel.h"
34#include "imagelist.h"
35
36#include <heapstring.h>
37#include <misc.h>
38
39DECLARE_DEBUG_CHANNEL(exec)
40DECLARE_DEBUG_CHANNEL(shell)
41
42/* .ICO file ICONDIR definitions */
43
44#include "pshpack1.h"
45
46typedef struct
47{
48 BYTE bWidth; /* Width, in pixels, of the image */
49 BYTE bHeight; /* Height, in pixels, of the image */
50 BYTE bColorCount; /* Number of colors in image (0 if >=8bpp) */
51 BYTE bReserved; /* Reserved ( must be 0) */
52 WORD wPlanes; /* Color Planes */
53 WORD wBitCount; /* Bits per pixel */
54 DWORD dwBytesInRes; /* How many bytes in this resource? */
55 DWORD dwImageOffset; /* Where in the file is this image? */
56} icoICONDIRENTRY, *LPicoICONDIRENTRY;
57
58typedef struct
59{
60 WORD idReserved; /* Reserved (must be 0) */
61 WORD idType; /* Resource Type (1 for icons) */
62 WORD idCount; /* How many images? */
63 icoICONDIRENTRY idEntries[1]; /* An entry for each image (idCount of 'em) */
64} icoICONDIR, *LPicoICONDIR;
65
66#include "poppack.h"
67
68static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
69static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
70static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
71
72static HWND16 SHELL_hWnd = 0;
73static HHOOK SHELL_hHook = 0;
74static UINT16 uMsgWndCreated = 0;
75static UINT16 uMsgWndDestroyed = 0;
76static UINT16 uMsgShellActivate = 0;
77HINSTANCE16 SHELL_hInstance = 0;
78HINSTANCE SHELL_hInstance32;
79static int SHELL_Attach = 0;
80
81/***********************************************************************
82 * SHELL_DllEntryPoint [SHELL.entry]
83 *
84 * Initialization code for shell.dll. Automatically loads the
85 * 32-bit shell32.dll to allow thunking up to 32-bit code.
86 *
87 * RETURNS:
88 */
89BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
90 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
91{
92 TRACE_(shell)("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
93 Reason, hInst, ds, HeapSize, res1, res2);
94
95 switch(Reason)
96 {
97 case DLL_PROCESS_ATTACH:
98 SHELL_Attach++;
99 if (SHELL_hInstance)
100 {
101 ERR_(shell)("shell.dll instantiated twice!\n");
102 /*
103 * We should return FALSE here, but that will break
104 * most apps that use CreateProcess because we do
105 * not yet support seperate address-spaces.
106 */
107 return TRUE;
108 }
109
110 SHELL_hInstance = hInst;
111 if(!SHELL_hInstance32)
112 {
113 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
114 {
115 ERR_(shell)("Could not load sibling shell32.dll\n");
116 return FALSE;
117 }
118 }
119 break;
120
121 case DLL_PROCESS_DETACH:
122 if(!--SHELL_Attach)
123 {
124 SHELL_hInstance = 0;
125 if(SHELL_hInstance32)
126 FreeLibrary(SHELL_hInstance32);
127 }
128 break;
129 }
130 return TRUE;
131}
132
133/*************************************************************************
134 * DragAcceptFiles32 [SHELL32.54]
135 */
136void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
137{
138 LONG exstyle;
139
140
141 if( !IsWindow(hWnd) )
142 return;
143 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
144 if (b)exstyle |= WS_EX_ACCEPTFILES;
145 else exstyle &= ~WS_EX_ACCEPTFILES;
146 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
147}
148
149/*************************************************************************
150 * SHELL_DragQueryFile [internal]
151 *
152 */
153static UINT SHELL_DragQueryFile(LPSTR lpDrop, LPWSTR lpwDrop, UINT lFile,
154 LPSTR lpszFile, LPWSTR lpszwFile, UINT lLength)
155{
156 UINT i;
157
158 i = 0;
159 if (lpDrop) {
160 while (i++ < lFile) {
161 while (*lpDrop++); /* skip filename */
162 if (!*lpDrop)
163 return (lFile == 0xFFFFFFFF) ? i : 0;
164 }
165 }
166 if (lpwDrop) {
167 while (i++ < lFile) {
168 while (*lpwDrop++); /* skip filename */
169 if (!*lpwDrop)
170 return (lFile == 0xFFFFFFFF) ? i : 0;
171 }
172 }
173
174 if (lpDrop) i = lstrlenA(lpDrop);
175 if (lpwDrop) i = lstrlenW(lpwDrop);
176 i++;
177 if (!lpszFile && !lpszwFile) {
178 return i; /* needed buffer size */
179 }
180 i = (lLength > i) ? i : lLength;
181 if (lpszFile) {
182 if (lpDrop) lstrcpynA (lpszFile, lpDrop, i);
183 else lstrcpynWtoA(lpszFile, lpwDrop, i);
184 } else {
185 if (lpDrop) lstrcpynAtoW(lpszwFile, lpDrop, i);
186 else lstrcpynW (lpszwFile, lpwDrop, i);
187 }
188 return i;
189}
190
191/*************************************************************************
192 * DragQueryFile32A [SHELL32.81] [shell32.82]
193 */
194UINT WINAPI DragQueryFileA(HDROP hDrop, UINT lFile, LPSTR lpszFile,
195 UINT lLength)
196{ /* hDrop is a global memory block allocated with GMEM_SHARE
197 * with DROPFILESTRUCT as a header and filenames following
198 * it, zero length filename is in the end */
199
200 LPDROPFILESTRUCT lpDropFileStruct;
201 LPSTR lpCurrent;
202 UINT i;
203
204 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
205
206 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
207 if(!lpDropFileStruct)
208 return 0;
209
210 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->lSize;
211 i = SHELL_DragQueryFile(lpCurrent, NULL, lFile, lpszFile, NULL, lLength);
212 GlobalUnlock(hDrop);
213 return i;
214}
215
216/*************************************************************************
217 * DragQueryFile32W [shell32.133]
218 */
219UINT WINAPI DragQueryFileW(HDROP hDrop, UINT lFile, LPWSTR lpszwFile,
220 UINT lLength)
221{
222 LPDROPFILESTRUCT lpDropFileStruct;
223 LPWSTR lpwCurrent;
224 UINT i;
225
226 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
227
228 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
229 if(!lpDropFileStruct)
230 return 0;
231
232 lpwCurrent = (LPWSTR) lpDropFileStruct + lpDropFileStruct->lSize;
233 i = SHELL_DragQueryFile(NULL, lpwCurrent, lFile, NULL, lpszwFile,lLength);
234 GlobalUnlock(hDrop);
235 return i;
236}
237
238
239/*************************************************************************
240 * DragFinish32 [SHELL32.80]
241 */
242void WINAPI DragFinish(HDROP h)
243{ TRACE_(shell)("\n");
244 GlobalFree((HGLOBAL)h);
245}
246
247/*************************************************************************
248 * DragFinish16 [SHELL.12]
249 */
250void WINAPI DragFinish16(HDROP16 h)
251{ TRACE_(shell)("\n");
252 GlobalFree16((HGLOBAL16)h);
253}
254
255
256/*************************************************************************
257 * DragQueryPoint32 [SHELL32.135]
258 */
259BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
260{
261 LPDROPFILESTRUCT lpDropFileStruct;
262 BOOL bRet;
263 TRACE_(shell)("\n");
264 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
265
266 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT));
267 bRet = lpDropFileStruct->fInNonClientArea;
268
269 GlobalUnlock(hDrop);
270 return bRet;
271}
272
273/*************************************************************************
274 * DragQueryPoint16 [SHELL.13]
275 */
276BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
277{
278 LPDROPFILESTRUCT16 lpDropFileStruct;
279 BOOL16 bRet;
280 TRACE_(shell)("\n");
281 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
282
283 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
284 bRet = lpDropFileStruct->fInNonClientArea;
285
286 GlobalUnlock16(hDrop);
287 return bRet;
288}
289
290/*************************************************************************
291 * SHELL_FindExecutable [Internal]
292 *
293 * Utility for code sharing between FindExecutable and ShellExecute
294 */
295HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
296 LPCSTR lpOperation,
297 LPSTR lpResult)
298{ char *extension = NULL; /* pointer to file extension */
299 char tmpext[5]; /* local copy to mung as we please */
300 char filetype[256]; /* registry name for this filetype */
301 LONG filetypelen=256; /* length of above */
302 char command[256]; /* command from registry */
303 LONG commandlen=256; /* This is the most DOS can handle :) */
304 char buffer[256]; /* Used to GetProfileString */
305 HINSTANCE retval=31; /* default - 'No association was found' */
306 char *tok; /* token pointer */
307 int i; /* random counter */
308 char xlpFile[256]; /* result of SearchPath */
309
310 TRACE_(shell)("%s\n", (lpFile != NULL?lpFile:"-") );
311
312 lpResult[0]='\0'; /* Start off with an empty return string */
313
314 /* trap NULL parameters on entry */
315 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
316 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
317 lpFile, lpOperation, lpResult);
318 return 2; /* File not found. Close enough, I guess. */
319 }
320
321 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
322 { TRACE_(shell)("SearchPath32A returned non-zero\n");
323 lpFile = xlpFile;
324 }
325
326 /* First thing we need is the file's extension */
327 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
328 /* File->Run in progman uses */
329 /* .\FILE.EXE :( */
330 TRACE_(shell)("xlpFile=%s,extension=%s\n", xlpFile, extension);
331
332 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
333 { WARN_(shell)("Returning 31 - No association\n");
334 return 31; /* no association */
335 }
336
337 /* Make local copy & lowercase it for reg & 'programs=' lookup */
338 lstrcpynA( tmpext, extension, 5 );
339 CharLowerA( tmpext );
340 TRACE_(shell)("%s file\n", tmpext);
341
342 /* Three places to check: */
343 /* 1. win.ini, [windows], programs (NB no leading '.') */
344 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
345 /* 3. win.ini, [extensions], extension (NB no leading '.' */
346 /* All I know of the order is that registry is checked before */
347 /* extensions; however, it'd make sense to check the programs */
348 /* section first, so that's what happens here. */
349
350 /* See if it's a program - if GetProfileString fails, we skip this
351 * section. Actually, if GetProfileString fails, we've probably
352 * got a lot more to worry about than running a program... */
353 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
354 buffer, sizeof(buffer)) > 0 )
355 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
356
357 tok = strtok(buffer, " \t"); /* ? */
358 while( tok!= NULL)
359 {
360 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
361 {
362 strcpy(lpResult, xlpFile);
363 /* Need to perhaps check that the file has a path
364 * attached */
365 TRACE_(shell)("found %s\n", lpResult);
366 return 33;
367
368 /* Greater than 32 to indicate success FIXME According to the
369 * docs, I should be returning a handle for the
370 * executable. Does this mean I'm supposed to open the
371 * executable file or something? More RTFM, I guess... */
372 }
373 tok=strtok(NULL, " \t");
374 }
375 }
376
377 /* Check registry */
378 if (RegQueryValueA( HKEY_CLASSES_ROOT, tmpext, filetype,
379 &filetypelen ) == ERROR_SUCCESS )
380 {
381 filetype[filetypelen]='\0';
382 TRACE_(shell)("File type: %s\n", filetype);
383
384 /* Looking for ...buffer\shell\lpOperation\command */
385 strcat( filetype, "\\shell\\" );
386 strcat( filetype, lpOperation );
387 strcat( filetype, "\\command" );
388
389 if (RegQueryValueA( HKEY_CLASSES_ROOT, filetype, command,
390 &commandlen ) == ERROR_SUCCESS )
391 {
392 /* Is there a replace() function anywhere? */
393 command[commandlen]='\0';
394 strcpy( lpResult, command );
395 tok=strstr( lpResult, "%1" );
396 if (tok != NULL)
397 {
398 tok[0]='\0'; /* truncate string at the percent */
399 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
400 tok=strstr( command, "%1" );
401 if ((tok!=NULL) && (strlen(tok)>2))
402 {
403 strcat( lpResult, &tok[2] );
404 }
405 }
406 retval=33; /* FIXME see above */
407 }
408 }
409 else /* Check win.ini */
410 {
411 /* Toss the leading dot */
412 extension++;
413 if ( GetProfileStringA( "extensions", extension, "", command,
414 sizeof(command)) > 0)
415 {
416 if (strlen(command)!=0)
417 {
418 strcpy( lpResult, command );
419 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
420 if (tok != NULL)
421 {
422 tok[0]='\0';
423 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
424 tok=strstr( command, "^" ); /* see above */
425 if ((tok != NULL) && (strlen(tok)>5))
426 {
427 strcat( lpResult, &tok[5]);
428 }
429 }
430 retval=33; /* FIXME - see above */
431 }
432 }
433 }
434
435 TRACE_(shell)("returning %s\n", lpResult);
436 return retval;
437}
438
439
440/*************************************************************************
441 * SHELL_GetResourceTable
442 */
443static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
444{ IMAGE_DOS_HEADER mz_header;
445 char magic[4];
446 int size;
447
448 TRACE_(shell)("\n");
449
450 *retptr = NULL;
451 _llseek( hFile, 0, SEEK_SET );
452 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
453 { /* .ICO file ? */
454 if (mz_header.e_cblp == 1)
455 { /* ICONHEADER.idType, must be 1 */
456 *retptr = (LPBYTE)-1;
457 return 1;
458 }
459 else
460 return 0; /* failed */
461 }
462 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
463
464 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
465 return 0;
466
467 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
468
469 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
470 return IMAGE_NT_SIGNATURE;
471
472 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
473 { IMAGE_OS2_HEADER ne_header;
474 LPBYTE pTypeInfo = (LPBYTE)-1;
475
476 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
477 return 0;
478
479 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
480 return 0;
481
482 size = ne_header.rname_tab_offset - ne_header.resource_tab_offset;
483
484//@@@PH no NE support
485#if 0
486 if( size > sizeof(NE_TYPEINFO) )
487 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
488 if( pTypeInfo )
489 { _llseek(hFile, mz_header.e_lfanew+ne_header.resource_tab_offset, SEEK_SET);
490 if( _lread( hFile, (char*)pTypeInfo, size) != size )
491 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
492 pTypeInfo = NULL;
493 }
494 }
495 }
496#endif
497
498 *retptr = pTypeInfo;
499 return IMAGE_OS2_SIGNATURE;
500 }
501 return 0; /* failed */
502}
503
504
505/*************************************************************************
506 * ExtractAssociatedIcon [SHELL.36]
507 *
508 * Return icon for given file (either from file itself or from associated
509 * executable) and patch parameters if needed.
510 */
511HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
512{ TRACE_(shell)("\n");
513 HICON hIcon;
514
515 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
516
517 if( hIcon < 2 )
518 { if( hIcon == 1 ) /* no icons found in given file */
519 { char tempPath[0x104];
520 UINT16 uRet = FindExecutableA(lpIconPath,NULL,tempPath);
521
522 if( uRet > 32 && tempPath[0] )
523 { strcpy(lpIconPath,tempPath);
524 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
525 if( hIcon > 2 )
526 return hIcon;
527 }
528 else hIcon = 0;
529 }
530
531 if( hIcon == 1 )
532 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
533 else
534 *lpiIcon = 6; /* generic icon - found nothing */
535
536 GetModuleFileNameA(hInst, lpIconPath, 0x80);
537 hIcon = LoadIconA( hInst, (LPCSTR)*lpiIcon);
538 }
539 return hIcon;
540}
Note: See TracBrowser for help on using the repository browser.