Authors: Martin Fuchs <martin-fuchs@gmx.net>, Ge van Geldorp <ge@gse.nl>
[wine/multimedia.git] / dlls / shell32 / shell.c
blob6cbb985786e40263c4448f44be515e333cc93b0d
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2000 Juergen Schmied
6 * Copyright 2002 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winreg.h"
38 #include "wownt32.h"
39 #include "dlgs.h"
40 #include "shellapi.h"
41 #include "winuser.h"
42 #include "wingdi.h"
43 #include "shlobj.h"
44 #include "shlwapi.h"
45 #include "ddeml.h"
47 #include "wine/winbase16.h"
48 #include "wine/winuser16.h"
49 #include "shell32_main.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(shell);
54 WINE_DECLARE_DEBUG_CHANNEL(exec);
57 typedef struct { /* structure for dropped files */
58 WORD wSize;
59 POINT16 ptMousePos;
60 BOOL16 fInNonClientArea;
61 /* memory block with filenames follows */
62 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
64 static const char* lpstrMsgWndCreated = "OTHERWINDOWCREATED";
65 static const char* lpstrMsgWndDestroyed = "OTHERWINDOWDESTROYED";
66 static const char* lpstrMsgShellActivate = "ACTIVATESHELLWINDOW";
68 static HWND SHELL_hWnd = 0;
69 static HHOOK SHELL_hHook = 0;
70 static UINT uMsgWndCreated = 0;
71 static UINT uMsgWndDestroyed = 0;
72 static UINT uMsgShellActivate = 0;
73 HINSTANCE16 SHELL_hInstance = 0;
74 HINSTANCE SHELL_hInstance32;
75 static int SHELL_Attach = 0;
77 /***********************************************************************
78 * DllEntryPoint [SHELL.101]
80 * Initialization code for shell.dll. Automatically loads the
81 * 32-bit shell32.dll to allow thunking up to 32-bit code.
83 * RETURNS
84 * Success: TRUE. Initialization completed successfully.
85 * Failure: FALSE.
87 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
88 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
90 TRACE("(%08lx, %04x, %04x, %04x, %08lx, %04x)\n",
91 Reason, hInst, ds, HeapSize, res1, res2);
93 switch(Reason)
95 case DLL_PROCESS_ATTACH:
96 if (SHELL_Attach++) break;
97 SHELL_hInstance = hInst;
98 if(!SHELL_hInstance32)
100 if(!(SHELL_hInstance32 = LoadLibraryA("shell32.dll")))
102 ERR("Could not load sibling shell32.dll\n");
103 return FALSE;
106 break;
108 case DLL_PROCESS_DETACH:
109 if(!--SHELL_Attach)
111 SHELL_hInstance = 0;
112 if(SHELL_hInstance32)
113 FreeLibrary(SHELL_hInstance32);
115 break;
117 return TRUE;
120 /*************************************************************************
121 * DragAcceptFiles [SHELL.9]
123 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
125 DragAcceptFiles(HWND_32(hWnd), b);
128 /*************************************************************************
129 * DragQueryFile [SHELL.11]
131 UINT16 WINAPI DragQueryFile16(
132 HDROP16 hDrop,
133 WORD wFile,
134 LPSTR lpszFile,
135 WORD wLength)
137 LPSTR lpDrop;
138 UINT i = 0;
139 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
141 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
143 if(!lpDropFileStruct) goto end;
145 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
147 while (i++ < wFile)
149 while (*lpDrop++); /* skip filename */
150 if (!*lpDrop)
152 i = (wFile == 0xFFFF) ? i : 0;
153 goto end;
157 i = strlen(lpDrop);
158 i++;
159 if (!lpszFile ) goto end; /* needed buffer size */
160 i = (wLength > i) ? i : wLength;
161 lstrcpynA (lpszFile, lpDrop, i);
162 end:
163 GlobalUnlock16(hDrop);
164 return i;
167 /*************************************************************************
168 * DragFinish [SHELL.12]
170 void WINAPI DragFinish16(HDROP16 h)
172 TRACE("\n");
173 GlobalFree16((HGLOBAL16)h);
177 /*************************************************************************
178 * DragQueryPoint [SHELL.13]
180 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
182 LPDROPFILESTRUCT16 lpDropFileStruct;
183 BOOL16 bRet;
184 TRACE("\n");
185 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
187 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
188 bRet = lpDropFileStruct->fInNonClientArea;
190 GlobalUnlock16(hDrop);
191 return bRet;
194 /*************************************************************************
195 * FindExecutable (SHELL.21)
197 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
198 LPSTR lpResult )
199 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
202 /*************************************************************************
203 * AboutDlgProc (SHELL.33)
205 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
206 LPARAM lParam )
207 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
211 /*************************************************************************
212 * ShellAbout (SHELL.22)
214 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
215 HICON16 hIcon )
216 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
219 /*************************************************************************
220 * InternalExtractIcon [SHELL.39]
222 * This abortion is called directly by Progman
224 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
225 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
227 HGLOBAL16 hRet = 0;
228 HICON16 *RetPtr = NULL;
229 OFSTRUCT ofs;
231 TRACE("(%04x,file %s,start %d,extract %d\n",
232 hInstance, lpszExeFileName, nIconIndex, n);
234 if (!n)
235 return 0;
237 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
238 RetPtr = (HICON16*)GlobalLock16(hRet);
240 if (nIconIndex == (UINT16)-1) /* get number of icons */
242 RetPtr[0] = PrivateExtractIconsA(ofs.szPathName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
244 else
246 UINT ret;
247 HICON *icons;
249 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
250 ret = PrivateExtractIconsA(ofs.szPathName, nIconIndex,
251 GetSystemMetrics(SM_CXICON),
252 GetSystemMetrics(SM_CYICON),
253 icons, NULL, n, LR_DEFAULTCOLOR);
254 if ((ret != 0xffffffff) && ret)
256 int i;
257 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
259 else
261 GlobalFree16(hRet);
262 hRet = 0;
264 HeapFree(GetProcessHeap(), 0, icons);
266 return hRet;
269 /*************************************************************************
270 * ExtractIcon (SHELL.34)
272 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
273 UINT16 nIconIndex )
274 { TRACE("\n");
275 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
278 /*************************************************************************
279 * ExtractIconEx (SHELL.40)
281 HICON16 WINAPI ExtractIconEx16(
282 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
283 HICON16 *phiconSmall, UINT16 nIcons
285 HICON *ilarge,*ismall;
286 UINT16 ret;
287 int i;
289 if (phiconLarge)
290 ilarge = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
291 else
292 ilarge = NULL;
293 if (phiconSmall)
294 ismall = (HICON*)HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
295 else
296 ismall = NULL;
297 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
298 if (ilarge) {
299 for (i=0;i<nIcons;i++)
300 phiconLarge[i]=HICON_16(ilarge[i]);
301 HeapFree(GetProcessHeap(),0,ilarge);
303 if (ismall) {
304 for (i=0;i<nIcons;i++)
305 phiconSmall[i]=HICON_16(ismall[i]);
306 HeapFree(GetProcessHeap(),0,ismall);
308 return ret;
311 /*************************************************************************
312 * ExtractAssociatedIcon [SHELL.36]
314 * Return icon for given file (either from file itself or from associated
315 * executable) and patch parameters if needed.
317 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
319 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
320 lpiIcon));
323 /*************************************************************************
324 * FindEnvironmentString [SHELL.38]
326 * Returns a pointer into the DOS environment... Ugh.
328 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
329 { UINT16 l;
331 TRACE("\n");
333 l = strlen(entry);
334 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
335 { if( strncasecmp(lpEnv, entry, l) )
336 continue;
337 if( !*(lpEnv+l) )
338 return (lpEnv + l); /* empty entry */
339 else if ( *(lpEnv+l)== '=' )
340 return (lpEnv + l + 1);
342 return NULL;
345 /**********************************************************************/
347 SEGPTR WINAPI FindEnvironmentString16(LPSTR str)
348 { SEGPTR spEnv;
349 LPSTR lpEnv,lpString;
350 TRACE("\n");
352 spEnv = GetDOSEnvironment16();
354 lpEnv = MapSL(spEnv);
355 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
357 if( lpString ) /* offset should be small enough */
358 return spEnv + (lpString - lpEnv);
359 return (SEGPTR)NULL;
362 /*************************************************************************
363 * DoEnvironmentSubst [SHELL.37]
365 * Replace %KEYWORD% in the str with the value of variable KEYWORD
366 * from "DOS" environment.
368 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
370 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
371 LPSTR lpBuffer = (LPSTR)HeapAlloc( GetProcessHeap(), 0, length);
372 LPSTR lpstr = str;
373 LPSTR lpbstr = lpBuffer;
375 CharToOemA(str,str);
377 TRACE("accept %s\n", str);
379 while( *lpstr && lpbstr - lpBuffer < length )
381 LPSTR lpend = lpstr;
383 if( *lpstr == '%' )
385 do { lpend++; } while( *lpend && *lpend != '%' );
386 if( *lpend == '%' && lpend - lpstr > 1 ) /* found key */
388 LPSTR lpKey;
389 *lpend = '\0';
390 lpKey = SHELL_FindString(lpEnv, lpstr+1);
391 if( lpKey ) /* found key value */
393 int l = strlen(lpKey);
395 if( l > length - (lpbstr - lpBuffer) - 1 )
397 WARN("-- Env subst aborted - string too short\n");
398 *lpend = '%';
399 break;
401 strcpy(lpbstr, lpKey);
402 lpbstr += l;
404 else break;
405 *lpend = '%';
406 lpstr = lpend + 1;
408 else break; /* back off and whine */
410 continue;
413 *lpbstr++ = *lpstr++;
416 *lpbstr = '\0';
417 if( lpstr - str == strlen(str) )
419 strncpy(str, lpBuffer, length);
420 length = 1;
422 else
423 length = 0;
425 TRACE("-- return %s\n", str);
427 OemToCharA(str,str);
428 HeapFree( GetProcessHeap(), 0, lpBuffer);
430 /* Return str length in the LOWORD
431 * and 1 in HIWORD if subst was successful.
433 return (DWORD)MAKELONG(strlen(str), length);
436 /*************************************************************************
437 * SHELL_HookProc
439 * 32-bit version of the system-wide WH_SHELL hook.
441 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
443 TRACE("%i, %x, %08lx\n", code, wParam, lParam );
445 if (SHELL_hWnd)
447 switch( code )
449 case HSHELL_WINDOWCREATED:
450 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
451 break;
452 case HSHELL_WINDOWDESTROYED:
453 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
454 break;
455 case HSHELL_ACTIVATESHELLWINDOW:
456 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
457 break;
460 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
463 /*************************************************************************
464 * ShellHookProc [SHELL.103]
465 * System-wide WH_SHELL hook.
467 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
469 return SHELL_HookProc( code, wParam, lParam );
472 /*************************************************************************
473 * RegisterShellHook [SHELL.102]
475 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
477 TRACE("%04x [%u]\n", hWnd, uAction );
479 switch( uAction )
481 case 2: /* register hWnd as a shell window */
482 if( !SHELL_hHook )
484 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
485 GetModuleHandleA("shell32.dll"), 0 );
486 if ( SHELL_hHook )
488 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
489 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
490 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
492 else
493 WARN("-- unable to install ShellHookProc()!\n");
496 if ( SHELL_hHook )
497 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
498 break;
500 default:
501 WARN("-- unknown code %i\n", uAction );
502 SHELL_hWnd = 0; /* just in case */
504 return FALSE;
508 /***********************************************************************
509 * DriveType (SHELL.262)
511 UINT16 WINAPI DriveType16( UINT16 drive )
513 UINT ret;
514 char path[] = "A:\\";
515 path[0] += drive;
516 ret = GetDriveTypeA(path);
517 switch(ret) /* some values are not supported in Win16 */
519 case DRIVE_CDROM:
520 ret = DRIVE_REMOTE;
521 break;
522 case DRIVE_NO_ROOT_DIR:
523 ret = DRIVE_UNKNOWN;
524 break;
526 return ret;
530 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
531 * some programs. Do not remove those cases. -MM
533 static inline void fix_win16_hkey( HKEY *hkey )
535 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
538 /******************************************************************************
539 * RegOpenKey [SHELL.1]
541 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
543 fix_win16_hkey( &hkey );
544 return RegOpenKeyA( hkey, name, retkey );
547 /******************************************************************************
548 * RegCreateKey [SHELL.2]
550 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
552 fix_win16_hkey( &hkey );
553 return RegCreateKeyA( hkey, name, retkey );
556 /******************************************************************************
557 * RegCloseKey [SHELL.3]
559 DWORD WINAPI RegCloseKey16( HKEY hkey )
561 fix_win16_hkey( &hkey );
562 return RegCloseKey( hkey );
565 /******************************************************************************
566 * RegDeleteKey [SHELL.4]
568 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
570 fix_win16_hkey( &hkey );
571 return RegDeleteKeyA( hkey, name );
574 /******************************************************************************
575 * RegSetValue [SHELL.5]
577 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
579 fix_win16_hkey( &hkey );
580 return RegSetValueA( hkey, name, type, data, count );
583 /******************************************************************************
584 * RegQueryValue [SHELL.6]
586 * NOTES
587 * Is this HACK still applicable?
589 * HACK
590 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
591 * mask out the high 16 bit. This (not so much incidently) hopefully fixes
592 * Aldus FH4)
594 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
597 fix_win16_hkey( &hkey );
598 if (count) *count &= 0xffff;
599 return RegQueryValueA( hkey, name, data, count );
602 /******************************************************************************
603 * RegEnumKey [SHELL.7]
605 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
607 fix_win16_hkey( &hkey );
608 return RegEnumKeyA( hkey, index, name, name_len );
611 /*************************************************************************
612 * SHELL_Execute16 [Internal]
614 static UINT SHELL_Execute16(WCHAR *lpCmd, void* env, LPSHELLEXECUTEINFOW seiW, BOOL shWait)
616 UINT ret;
617 char sCmd[MAX_PATH];
618 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
619 ret = WinExec16(sCmd, seiW->nShow);
620 seiW->hInstApp = HINSTANCE_32(ret);
621 return ret;
624 /*************************************************************************
625 * ShellExecute [SHELL.20]
627 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
628 LPCSTR lpFile, LPCSTR lpParameters,
629 LPCSTR lpDirectory, INT16 iShowCmd )
631 SHELLEXECUTEINFOW seiW;
632 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
633 HANDLE hProcess = 0;
635 if (lpOperation)
636 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, lpOperation);
638 if (lpFile)
639 seiW.lpFile = __SHCloneStrAtoW(&wFile, lpFile);
641 if (lpParameters)
642 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, lpParameters);
644 if (lpDirectory)
645 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, lpDirectory);
647 seiW.cbSize = sizeof(seiW);
648 seiW.fMask = 0;
649 seiW.hwnd = HWND_32(hWnd);
650 seiW.nShow = iShowCmd;
651 seiW.lpIDList = 0;
652 seiW.lpClass = 0;
653 seiW.hkeyClass = 0;
654 seiW.dwHotKey = 0;
655 seiW.hProcess = hProcess;
657 ShellExecuteExW32 (&seiW, SHELL_Execute16);
659 if (wVerb) SHFree(wVerb);
660 if (wFile) SHFree(wFile);
661 if (wParameters) SHFree(wParameters);
662 if (wDirectory) SHFree(wDirectory);
664 return HINSTANCE_16(seiW.hInstApp);