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

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

Fix: conflict with win32type.h

File size: 14.6 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 <winbase.h>
18#include <winuser.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#include "shell32_main.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/*************************************************************************
83 * DragAcceptFiles32 [SHELL32.54]
84 */
85void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
86{
87 LONG exstyle;
88
89
90 if( !IsWindow(hWnd) )
91 return;
92 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
93 if (b)exstyle |= WS_EX_ACCEPTFILES;
94 else exstyle &= ~WS_EX_ACCEPTFILES;
95 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
96}
97
98/*************************************************************************
99 * SHELL_DragQueryFile [internal]
100 *
101 */
102static UINT SHELL_DragQueryFile(LPSTR lpDrop, LPWSTR lpwDrop, UINT lFile,
103 LPSTR lpszFile, LPWSTR lpszwFile, UINT lLength)
104{
105 UINT i;
106
107 i = 0;
108 if (lpDrop) {
109 while (i++ < lFile) {
110 while (*lpDrop++); /* skip filename */
111 if (!*lpDrop)
112 return (lFile == 0xFFFFFFFF) ? i : 0;
113 }
114 }
115 if (lpwDrop) {
116 while (i++ < lFile) {
117 while (*lpwDrop++); /* skip filename */
118 if (!*lpwDrop)
119 return (lFile == 0xFFFFFFFF) ? i : 0;
120 }
121 }
122
123 if (lpDrop) i = lstrlenA(lpDrop);
124 if (lpwDrop) i = lstrlenW(lpwDrop);
125 i++;
126 if (!lpszFile && !lpszwFile) {
127 return i; /* needed buffer size */
128 }
129 i = (lLength > i) ? i : lLength;
130 if (lpszFile) {
131 if (lpDrop) lstrcpynA (lpszFile, lpDrop, i);
132 else lstrcpynWtoA(lpszFile, lpwDrop, i);
133 } else {
134 if (lpDrop) lstrcpynAtoW(lpszwFile, lpDrop, i);
135 else lstrcpynW (lpszwFile, lpwDrop, i);
136 }
137 return i;
138}
139
140/*************************************************************************
141 * DragQueryFile32A [SHELL32.81] [shell32.82]
142 */
143UINT WINAPI DragQueryFileA(HDROP hDrop, UINT lFile, LPSTR lpszFile,
144 UINT lLength)
145{ /* hDrop is a global memory block allocated with GMEM_SHARE
146 * with DROPFILESTRUCT as a header and filenames following
147 * it, zero length filename is in the end */
148
149 LPDROPFILESTRUCT lpDropFileStruct;
150 LPSTR lpCurrent;
151 UINT i;
152
153 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
154
155 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
156 if(!lpDropFileStruct)
157 return 0;
158
159 lpCurrent = (LPSTR) lpDropFileStruct + lpDropFileStruct->lSize;
160 i = SHELL_DragQueryFile(lpCurrent, NULL, lFile, lpszFile, NULL, lLength);
161 GlobalUnlock(hDrop);
162 return i;
163}
164
165/*************************************************************************
166 * DragQueryFile32W [shell32.133]
167 */
168UINT WINAPI DragQueryFileW(HDROP hDrop, UINT lFile, LPWSTR lpszwFile,
169 UINT lLength)
170{
171 LPDROPFILESTRUCT lpDropFileStruct;
172 LPWSTR lpwCurrent;
173 UINT i;
174
175 TRACE_(shell)("(%08x, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
176
177 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
178 if(!lpDropFileStruct)
179 return 0;
180
181 lpwCurrent = (LPWSTR) lpDropFileStruct + lpDropFileStruct->lSize;
182 i = SHELL_DragQueryFile(NULL, lpwCurrent, lFile, NULL, lpszwFile,lLength);
183 GlobalUnlock(hDrop);
184 return i;
185}
186
187
188/*****************************************************************************
189 * Name : UINT DragQueryFileAorW
190 * Purpose :
191 * Parameters: HDROP hDrop - drop structure handle
192 * UINT iFile - index of file to query
193 * LPTSTR lpszFile - target buffer
194 * UINT cch - target buffer size
195 * Variables :
196 * Result :
197 * Remark :
198 * Status : UNTESTED STUB
199 *
200 * Author : Patrick Haller [Tue, 1999/06/09 20:00]
201 *****************************************************************************/
202
203UINT WIN32API DragQueryFileAorW(HDROP hDrop,
204 UINT iFile,
205 LPTSTR lpszFile,
206 UINT cch)
207{
208 dprintf(("SHELL32: DragQueryFileAorW(%08xh,%08xh,%s,%08xh).\n",
209 hDrop,
210 iFile,
211 lpszFile,
212 cch));
213
214 // @@@PH maybe they want automatic determination here
215 if (VERSION_OsIsUnicode())
216 return DragQueryFileW(hDrop, iFile, (LPWSTR)lpszFile, cch);
217 else
218 return DragQueryFileA(hDrop, iFile, lpszFile, cch);
219}
220
221
222/*************************************************************************
223 * DragFinish32 [SHELL32.80]
224 */
225void WINAPI DragFinish(HDROP h)
226{ TRACE_(shell)("\n");
227 GlobalFree((HGLOBAL)h);
228}
229
230
231/*************************************************************************
232 * DragQueryPoint32 [SHELL32.135]
233 */
234BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
235{
236 LPDROPFILESTRUCT lpDropFileStruct;
237 BOOL bRet;
238 TRACE_(shell)("\n");
239 lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
240
241 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT));
242 bRet = lpDropFileStruct->fInNonClientArea;
243
244 GlobalUnlock(hDrop);
245 return bRet;
246}
247
248/*************************************************************************
249 * SHELL_FindExecutable [Internal]
250 *
251 * Utility for code sharing between FindExecutable and ShellExecute
252 */
253HINSTANCE SHELL_FindExecutable( LPCSTR lpFile,
254 LPCSTR lpOperation,
255 LPSTR lpResult)
256{ char *extension = NULL; /* pointer to file extension */
257 char tmpext[5]; /* local copy to mung as we please */
258 char filetype[256]; /* registry name for this filetype */
259 LONG filetypelen=256; /* length of above */
260 char command[256]; /* command from registry */
261 LONG commandlen=256; /* This is the most DOS can handle :) */
262 char buffer[256]; /* Used to GetProfileString */
263 HINSTANCE retval=31; /* default - 'No association was found' */
264 char *tok; /* token pointer */
265 int i; /* random counter */
266 char xlpFile[256]; /* result of SearchPath */
267
268 TRACE_(shell)("%s\n", (lpFile != NULL?lpFile:"-") );
269
270 lpResult[0]='\0'; /* Start off with an empty return string */
271
272 /* trap NULL parameters on entry */
273 if (( lpFile == NULL ) || ( lpResult == NULL ) || ( lpOperation == NULL ))
274 { WARN_(exec)("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
275 lpFile, lpOperation, lpResult);
276 return 2; /* File not found. Close enough, I guess. */
277 }
278
279 if (SearchPathA( NULL, lpFile,".exe",sizeof(xlpFile),xlpFile,NULL))
280 { TRACE_(shell)("SearchPath32A returned non-zero\n");
281 lpFile = xlpFile;
282 }
283
284 /* First thing we need is the file's extension */
285 extension = strrchr( xlpFile, '.' ); /* Assume last "." is the one; */
286 /* File->Run in progman uses */
287 /* .\FILE.EXE :( */
288 TRACE_(shell)("xlpFile=%s,extension=%s\n", xlpFile, extension);
289
290 if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
291 { WARN_(shell)("Returning 31 - No association\n");
292 return 31; /* no association */
293 }
294
295 /* Make local copy & lowercase it for reg & 'programs=' lookup */
296 lstrcpynA( tmpext, extension, 5 );
297 CharLowerA( tmpext );
298 TRACE_(shell)("%s file\n", tmpext);
299
300 /* Three places to check: */
301 /* 1. win.ini, [windows], programs (NB no leading '.') */
302 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
303 /* 3. win.ini, [extensions], extension (NB no leading '.' */
304 /* All I know of the order is that registry is checked before */
305 /* extensions; however, it'd make sense to check the programs */
306 /* section first, so that's what happens here. */
307
308 /* See if it's a program - if GetProfileString fails, we skip this
309 * section. Actually, if GetProfileString fails, we've probably
310 * got a lot more to worry about than running a program... */
311 if ( GetProfileStringA("windows", "programs", "exe pif bat com",
312 buffer, sizeof(buffer)) > 0 )
313 { for (i=0;i<strlen(buffer); i++) buffer[i]=tolower(buffer[i]);
314
315 tok = strtok(buffer, " \t"); /* ? */
316 while( tok!= NULL)
317 {
318 if (strcmp(tok, &tmpext[1])==0) /* have to skip the leading "." */
319 {
320 strcpy(lpResult, xlpFile);
321 /* Need to perhaps check that the file has a path
322 * attached */
323 TRACE_(shell)("found %s\n", lpResult);
324 return 33;
325
326 /* Greater than 32 to indicate success FIXME According to the
327 * docs, I should be returning a handle for the
328 * executable. Does this mean I'm supposed to open the
329 * executable file or something? More RTFM, I guess... */
330 }
331 tok=strtok(NULL, " \t");
332 }
333 }
334
335 /* Check registry */
336 if (RegQueryValueA( HKEY_CLASSES_ROOT, tmpext, filetype,
337 &filetypelen ) == ERROR_SUCCESS )
338 {
339 filetype[filetypelen]='\0';
340 TRACE_(shell)("File type: %s\n", filetype);
341
342 /* Looking for ...buffer\shell\lpOperation\command */
343 strcat( filetype, "\\shell\\" );
344 strcat( filetype, lpOperation );
345 strcat( filetype, "\\command" );
346
347 if (RegQueryValueA( HKEY_CLASSES_ROOT, filetype, command,
348 &commandlen ) == ERROR_SUCCESS )
349 {
350 /* Is there a replace() function anywhere? */
351 command[commandlen]='\0';
352 strcpy( lpResult, command );
353 tok=strstr( lpResult, "%1" );
354 if (tok != NULL)
355 {
356 tok[0]='\0'; /* truncate string at the percent */
357 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
358 tok=strstr( command, "%1" );
359 if ((tok!=NULL) && (strlen(tok)>2))
360 {
361 strcat( lpResult, &tok[2] );
362 }
363 }
364 retval=33; /* FIXME see above */
365 }
366 }
367 else /* Check win.ini */
368 {
369 /* Toss the leading dot */
370 extension++;
371 if ( GetProfileStringA( "extensions", extension, "", command,
372 sizeof(command)) > 0)
373 {
374 if (strlen(command)!=0)
375 {
376 strcpy( lpResult, command );
377 tok=strstr( lpResult, "^" ); /* should be ^.extension? */
378 if (tok != NULL)
379 {
380 tok[0]='\0';
381 strcat( lpResult, xlpFile ); /* what if no dir in xlpFile? */
382 tok=strstr( command, "^" ); /* see above */
383 if ((tok != NULL) && (strlen(tok)>5))
384 {
385 strcat( lpResult, &tok[5]);
386 }
387 }
388 retval=33; /* FIXME - see above */
389 }
390 }
391 }
392
393 TRACE_(shell)("returning %s\n", lpResult);
394 return retval;
395}
396
397
398/*************************************************************************
399 * SHELL_GetResourceTable
400 */
401static DWORD SHELL_GetResourceTable(HFILE hFile,LPBYTE *retptr)
402{ IMAGE_DOS_HEADER mz_header;
403 char magic[4];
404 int size;
405
406 TRACE_(shell)("\n");
407
408 *retptr = NULL;
409 _llseek( hFile, 0, SEEK_SET );
410 if ((_lread(hFile,&mz_header,sizeof(mz_header)) != sizeof(mz_header)) || (mz_header.e_magic != IMAGE_DOS_SIGNATURE))
411 { /* .ICO file ? */
412 if (mz_header.e_cblp == 1)
413 { /* ICONHEADER.idType, must be 1 */
414 *retptr = (LPBYTE)-1;
415 return 1;
416 }
417 else
418 return 0; /* failed */
419 }
420 _llseek( hFile, mz_header.e_lfanew, SEEK_SET );
421
422 if (_lread( hFile, magic, sizeof(magic) ) != sizeof(magic))
423 return 0;
424
425 _llseek( hFile, mz_header.e_lfanew, SEEK_SET);
426
427 if (*(DWORD*)magic == IMAGE_NT_SIGNATURE)
428 return IMAGE_NT_SIGNATURE;
429
430 if (*(WORD*)magic == IMAGE_OS2_SIGNATURE)
431 { IMAGE_OS2_HEADER ne_header;
432 LPBYTE pTypeInfo = (LPBYTE)-1;
433
434 if (_lread(hFile,&ne_header,sizeof(ne_header))!=sizeof(ne_header))
435 return 0;
436
437 if (ne_header.ne_magic != IMAGE_OS2_SIGNATURE)
438 return 0;
439
440 size = ne_header.rname_tab_offset - ne_header.resource_tab_offset;
441
442//@@@PH no NE support
443#if 0
444 if( size > sizeof(NE_TYPEINFO) )
445 { pTypeInfo = (BYTE*)HeapAlloc( GetProcessHeap(), 0, size);
446 if( pTypeInfo )
447 { _llseek(hFile, mz_header.e_lfanew+ne_header.resource_tab_offset, SEEK_SET);
448 if( _lread( hFile, (char*)pTypeInfo, size) != size )
449 { HeapFree( GetProcessHeap(), 0, pTypeInfo);
450 pTypeInfo = NULL;
451 }
452 }
453 }
454#endif
455
456 *retptr = pTypeInfo;
457 return IMAGE_OS2_SIGNATURE;
458 }
459 return 0; /* failed */
460}
461
462
463/*************************************************************************
464 * ExtractAssociatedIcon [SHELL.36]
465 *
466 * Return icon for given file (either from file itself or from associated
467 * executable) and patch parameters if needed.
468 */
469HICON WINAPI ExtractAssociatedIconA(HINSTANCE hInst, LPSTR lpIconPath, LPWORD lpiIcon)
470{ TRACE_(shell)("\n");
471 HICON hIcon;
472
473 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
474
475 if( hIcon < 2 )
476 { if( hIcon == 1 ) /* no icons found in given file */
477 { char tempPath[0x104];
478 UINT16 uRet = FindExecutableA(lpIconPath,NULL,tempPath);
479
480 if( uRet > 32 && tempPath[0] )
481 { strcpy(lpIconPath,tempPath);
482 hIcon = ExtractIconA(hInst, lpIconPath, *lpiIcon);
483 if( hIcon > 2 )
484 return hIcon;
485 }
486 else hIcon = 0;
487 }
488
489 if( hIcon == 1 )
490 *lpiIcon = 2; /* MSDOS icon - we found .exe but no icons in it */
491 else
492 *lpiIcon = 6; /* generic icon - found nothing */
493
494 GetModuleFileNameA(hInst, lpIconPath, 0x80);
495 hIcon = LoadIconA( hInst, (LPCSTR)*lpiIcon);
496 }
497 return hIcon;
498}
Note: See TracBrowser for help on using the repository browser.