shell32: Make shell.dll into a stand-alone 16-bit module.
[wine/multimedia.git] / dlls / shell.dll16 / shell.c
blob9964b60c2d8caee2eacf400beb552bf53c6e9b1a
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winreg.h"
37 #include "wownt32.h"
38 #include "shellapi.h"
39 #include "winuser.h"
40 #include "wingdi.h"
41 #include "shlobj.h"
42 #include "shlwapi.h"
44 #include "wine/winbase16.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 extern HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
51 LPCSTR lpParameters,LPCSTR lpDirectory,
52 INT iShowCmd, void *callback);
54 #define HICON_16(h32) (LOWORD(h32))
55 #define HICON_32(h16) ((HICON)(ULONG_PTR)(h16))
56 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
57 #define HINSTANCE_16(h32) (LOWORD(h32))
59 typedef struct { /* structure for dropped files */
60 WORD wSize;
61 POINT16 ptMousePos;
62 BOOL16 fInNonClientArea;
63 /* memory block with filenames follows */
64 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
66 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
67 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
68 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
70 static HWND SHELL_hWnd = 0;
71 static HHOOK SHELL_hHook = 0;
72 static UINT uMsgWndCreated = 0;
73 static UINT uMsgWndDestroyed = 0;
74 static UINT uMsgShellActivate = 0;
76 /***********************************************************************
77 * DllEntryPoint [SHELL.101]
79 * Initialization code for shell.dll. Automatically loads the
80 * 32-bit shell32.dll to allow thunking up to 32-bit code.
82 * RETURNS
83 * Success: TRUE. Initialization completed successfully.
84 * Failure: FALSE.
86 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
87 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
89 return TRUE;
92 /*************************************************************************
93 * DragAcceptFiles [SHELL.9]
95 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
97 DragAcceptFiles(HWND_32(hWnd), b);
100 /*************************************************************************
101 * DragQueryFile [SHELL.11]
103 UINT16 WINAPI DragQueryFile16(
104 HDROP16 hDrop,
105 WORD wFile,
106 LPSTR lpszFile,
107 WORD wLength)
109 LPSTR lpDrop;
110 UINT i = 0;
111 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
113 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
115 if(!lpDropFileStruct) goto end;
117 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
119 while (i++ < wFile)
121 while (*lpDrop++); /* skip filename */
122 if (!*lpDrop)
124 i = (wFile == 0xFFFF) ? i : 0;
125 goto end;
129 i = strlen(lpDrop);
130 if (!lpszFile ) goto end; /* needed buffer size */
131 lstrcpynA (lpszFile, lpDrop, wLength);
132 end:
133 GlobalUnlock16(hDrop);
134 return i;
137 /*************************************************************************
138 * DragFinish [SHELL.12]
140 void WINAPI DragFinish16(HDROP16 h)
142 TRACE("\n");
143 GlobalFree16(h);
147 /*************************************************************************
148 * DragQueryPoint [SHELL.13]
150 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
152 LPDROPFILESTRUCT16 lpDropFileStruct;
153 BOOL16 bRet;
154 TRACE("\n");
155 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
157 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
158 bRet = lpDropFileStruct->fInNonClientArea;
160 GlobalUnlock16(hDrop);
161 return bRet;
164 /*************************************************************************
165 * FindExecutable (SHELL.21)
167 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
168 LPSTR lpResult )
169 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
172 /*************************************************************************
173 * AboutDlgProc (SHELL.33)
175 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
176 LPARAM lParam )
178 FIXME( "stub\n" );
179 return FALSE;
183 /*************************************************************************
184 * ShellAbout (SHELL.22)
186 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
187 HICON16 hIcon )
188 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
191 /*************************************************************************
192 * InternalExtractIcon [SHELL.39]
194 * This abortion is called directly by Progman
196 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
197 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
199 HGLOBAL16 hRet = 0;
200 HICON16 *RetPtr = NULL;
202 TRACE("(%04x,file %s,start %d,extract %d\n",
203 hInstance, lpszExeFileName, nIconIndex, n);
205 if (!n)
206 return 0;
208 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
209 RetPtr = GlobalLock16(hRet);
211 if (nIconIndex == (UINT16)-1) /* get number of icons */
213 RetPtr[0] = PrivateExtractIconsA(lpszExeFileName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
215 else
217 UINT ret;
218 HICON *icons;
220 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
221 ret = PrivateExtractIconsA(lpszExeFileName, nIconIndex,
222 GetSystemMetrics(SM_CXICON),
223 GetSystemMetrics(SM_CYICON),
224 icons, NULL, n, LR_DEFAULTCOLOR);
225 if ((ret != 0xffffffff) && ret)
227 int i;
228 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
230 else
232 GlobalFree16(hRet);
233 hRet = 0;
235 HeapFree(GetProcessHeap(), 0, icons);
237 return hRet;
240 /*************************************************************************
241 * ExtractIcon (SHELL.34)
243 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
244 UINT16 nIconIndex )
245 { TRACE("\n");
246 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
249 /*************************************************************************
250 * ExtractIconEx (SHELL.40)
252 HICON16 WINAPI ExtractIconEx16(
253 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
254 HICON16 *phiconSmall, UINT16 nIcons
256 HICON *ilarge,*ismall;
257 UINT16 ret;
258 int i;
260 if (phiconLarge)
261 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
262 else
263 ilarge = NULL;
264 if (phiconSmall)
265 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
266 else
267 ismall = NULL;
268 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
269 if (ilarge) {
270 for (i=0;i<nIcons;i++)
271 phiconLarge[i]=HICON_16(ilarge[i]);
272 HeapFree(GetProcessHeap(),0,ilarge);
274 if (ismall) {
275 for (i=0;i<nIcons;i++)
276 phiconSmall[i]=HICON_16(ismall[i]);
277 HeapFree(GetProcessHeap(),0,ismall);
279 return ret;
282 /*************************************************************************
283 * ExtractAssociatedIcon [SHELL.36]
285 * Return icon for given file (either from file itself or from associated
286 * executable) and patch parameters if needed.
288 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
290 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
291 lpiIcon));
294 /*************************************************************************
295 * FindEnvironmentString [SHELL.38]
297 * Returns a pointer into the DOS environment... Ugh.
299 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
300 { UINT16 l;
302 TRACE("\n");
304 l = strlen(entry);
305 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
306 { if( strncasecmp(lpEnv, entry, l) )
307 continue;
308 if( !*(lpEnv+l) )
309 return (lpEnv + l); /* empty entry */
310 else if ( *(lpEnv+l)== '=' )
311 return (lpEnv + l + 1);
313 return NULL;
316 /**********************************************************************/
318 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
319 { SEGPTR spEnv;
320 LPSTR lpEnv,lpString;
321 TRACE("\n");
323 spEnv = GetDOSEnvironment16();
325 lpEnv = MapSL(spEnv);
326 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
328 if( lpString ) /* offset should be small enough */
329 return spEnv + (lpString - lpEnv);
330 return 0;
333 /*************************************************************************
334 * DoEnvironmentSubst [SHELL.37]
336 * Replace %KEYWORD% in the str with the value of variable KEYWORD
337 * from "DOS" environment. If it is not found the %KEYWORD% is left
338 * intact. If the buffer is too small, str is not modified.
340 * PARAMS
341 * str [I] '\0' terminated string with %keyword%.
342 * [O] '\0' terminated string with %keyword% substituted.
343 * length [I] size of str.
345 * RETURNS
346 * str length in the LOWORD and 1 in HIWORD if subst was successful.
348 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
350 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
351 LPSTR lpstr = str;
352 LPSTR lpend;
353 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
354 WORD bufCnt = 0;
355 WORD envKeyLen;
356 LPSTR lpKey;
357 WORD retStatus = 0;
358 WORD retLength = length;
360 CharToOemA(str,str);
362 TRACE("accept %s\n", str);
364 while( *lpstr && bufCnt <= length - 1 ) {
365 if ( *lpstr != '%' ) {
366 lpBuffer[bufCnt++] = *lpstr++;
367 continue;
370 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
372 envKeyLen = lpend - lpstr - 1;
373 if( *lpend != '%' || envKeyLen == 0)
374 goto err; /* "%\0" or "%%" found; back off and whine */
376 *lpend = '\0';
377 lpKey = SHELL_FindString(lpEnv, lpstr+1);
378 *lpend = '%';
379 if( lpKey ) {
380 int l = strlen(lpKey);
382 if( bufCnt + l > length - 1 )
383 goto err;
385 memcpy(lpBuffer + bufCnt, lpKey, l);
386 bufCnt += l;
387 } else { /* Keyword not found; Leave the %KEYWORD% intact */
388 if( bufCnt + envKeyLen + 2 > length - 1 )
389 goto err;
391 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
392 bufCnt += envKeyLen + 2;
395 lpstr = lpend + 1;
398 if (!*lpstr && bufCnt <= length - 1) {
399 memcpy(str,lpBuffer, bufCnt);
400 str[bufCnt] = '\0';
401 retLength = bufCnt + 1;
402 retStatus = 1;
405 err:
406 if (!retStatus)
407 WARN("-- Env subst aborted - string too short or invalid input\n");
408 TRACE("-- return %s\n", str);
410 OemToCharA(str,str);
411 HeapFree( GetProcessHeap(), 0, lpBuffer);
413 return (DWORD)MAKELONG(retLength, retStatus);
416 /*************************************************************************
417 * SHELL_HookProc
419 * 32-bit version of the system-wide WH_SHELL hook.
421 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
423 TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
425 if (SHELL_hWnd)
427 switch( code )
429 case HSHELL_WINDOWCREATED:
430 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
431 break;
432 case HSHELL_WINDOWDESTROYED:
433 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
434 break;
435 case HSHELL_ACTIVATESHELLWINDOW:
436 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
437 break;
440 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
443 /*************************************************************************
444 * ShellHookProc [SHELL.103]
445 * System-wide WH_SHELL hook.
447 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
449 return SHELL_HookProc( code, wParam, lParam );
452 /*************************************************************************
453 * RegisterShellHook [SHELL.102]
455 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
457 TRACE("%04x [%u]\n", hWnd, uAction );
459 switch( uAction )
461 case 2: /* register hWnd as a shell window */
462 if( !SHELL_hHook )
464 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
465 GetModuleHandleA("shell32.dll"), 0 );
466 if ( SHELL_hHook )
468 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
469 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
470 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
472 else
473 WARN("-- unable to install ShellHookProc()!\n");
476 if ( SHELL_hHook )
477 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
478 break;
480 default:
481 WARN("-- unknown code %i\n", uAction );
482 SHELL_hWnd = 0; /* just in case */
484 return FALSE;
488 /***********************************************************************
489 * DriveType (SHELL.262)
491 UINT16 WINAPI DriveType16( UINT16 drive )
493 UINT ret;
494 char path[] = "A:\\";
495 path[0] += drive;
496 ret = GetDriveTypeA(path);
497 switch(ret) /* some values are not supported in Win16 */
499 case DRIVE_CDROM:
500 ret = DRIVE_REMOTE;
501 break;
502 case DRIVE_NO_ROOT_DIR:
503 ret = DRIVE_UNKNOWN;
504 break;
506 return ret;
510 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
511 * some programs. Do not remove those cases. -MM
513 static inline void fix_win16_hkey( HKEY *hkey )
515 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
518 /******************************************************************************
519 * RegOpenKey [SHELL.1]
521 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
523 fix_win16_hkey( &hkey );
524 return RegOpenKeyA( hkey, name, retkey );
527 /******************************************************************************
528 * RegCreateKey [SHELL.2]
530 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
532 fix_win16_hkey( &hkey );
533 return RegCreateKeyA( hkey, name, retkey );
536 /******************************************************************************
537 * RegCloseKey [SHELL.3]
539 DWORD WINAPI RegCloseKey16( HKEY hkey )
541 fix_win16_hkey( &hkey );
542 return RegCloseKey( hkey );
545 /******************************************************************************
546 * RegDeleteKey [SHELL.4]
548 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
550 fix_win16_hkey( &hkey );
551 return RegDeleteKeyA( hkey, name );
554 /******************************************************************************
555 * RegSetValue [SHELL.5]
557 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
559 fix_win16_hkey( &hkey );
560 return RegSetValueA( hkey, name, type, data, count );
563 /******************************************************************************
564 * RegQueryValue [SHELL.6]
566 * NOTES
567 * Is this HACK still applicable?
569 * HACK
570 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
571 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
572 * Aldus FH4)
574 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
577 fix_win16_hkey( &hkey );
578 if (count) *count &= 0xffff;
579 return RegQueryValueA( hkey, name, data, (LONG*) count );
582 /******************************************************************************
583 * RegEnumKey [SHELL.7]
585 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
587 fix_win16_hkey( &hkey );
588 return RegEnumKeyA( hkey, index, name, name_len );
591 /*************************************************************************
592 * SHELL_Execute16 [Internal]
594 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
595 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
597 UINT ret;
598 char sCmd[MAX_PATH];
599 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
600 ret = WinExec16(sCmd, (UINT16)psei->nShow);
601 psei_out->hInstApp = HINSTANCE_32(ret);
602 return ret;
605 /*************************************************************************
606 * ShellExecute [SHELL.20]
608 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
609 LPCSTR lpFile, LPCSTR lpParameters,
610 LPCSTR lpDirectory, INT16 iShowCmd )
612 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd), lpOperation, lpFile, lpParameters,
613 lpDirectory, iShowCmd, SHELL_Execute16 ));