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

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

Fix: shell32 improvements

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