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
24 #include "wine/port.h"
44 #include "wine/winbase16.h"
45 #include "wine/winuser16.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
51 extern HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
52 LPCSTR lpParameters
,LPCSTR lpDirectory
,
53 INT iShowCmd
, void *callback
);
55 #define HICON_16(h32) (LOWORD(h32))
56 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
57 #define HINSTANCE_16(h32) (LOWORD(h32))
59 typedef struct { /* structure for dropped files */
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 static HICON
convert_icon_to_32( HICON16 icon16
)
78 CURSORICONINFO
*info
= GlobalLock16( icon16
);
79 void *and_bits
= info
+ 1;
80 void *xor_bits
= (BYTE
*)and_bits
+ info
->nHeight
* 2 * ((info
->nWidth
+ 15) / 16);
81 HICON ret
= CreateIcon( 0, info
->nWidth
, info
->nHeight
, info
->bPlanes
, info
->bBitsPerPixel
,
83 GlobalUnlock16( icon16
);
87 /***********************************************************************
88 * DllEntryPoint [SHELL.101]
90 * Initialization code for shell.dll. Automatically loads the
91 * 32-bit shell32.dll to allow thunking up to 32-bit code.
94 * Success: TRUE. Initialization completed successfully.
97 BOOL WINAPI
SHELL_DllEntryPoint(DWORD Reason
, HINSTANCE16 hInst
,
98 WORD ds
, WORD HeapSize
, DWORD res1
, WORD res2
)
103 /*************************************************************************
104 * DragAcceptFiles [SHELL.9]
106 void WINAPI
DragAcceptFiles16(HWND16 hWnd
, BOOL16 b
)
108 DragAcceptFiles(HWND_32(hWnd
), b
);
111 /*************************************************************************
112 * DragQueryFile [SHELL.11]
114 UINT16 WINAPI
DragQueryFile16(
122 LPDROPFILESTRUCT16 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
124 TRACE("(%04x, %x, %p, %u)\n", hDrop
,wFile
,lpszFile
,wLength
);
126 if(!lpDropFileStruct
) goto end
;
128 lpDrop
= (LPSTR
) lpDropFileStruct
+ lpDropFileStruct
->wSize
;
132 while (*lpDrop
++); /* skip filename */
135 i
= (wFile
== 0xFFFF) ? i
: 0;
141 if (!lpszFile
) goto end
; /* needed buffer size */
142 lstrcpynA (lpszFile
, lpDrop
, wLength
);
144 GlobalUnlock16(hDrop
);
148 /*************************************************************************
149 * DragFinish [SHELL.12]
151 void WINAPI
DragFinish16(HDROP16 h
)
158 /*************************************************************************
159 * DragQueryPoint [SHELL.13]
161 BOOL16 WINAPI
DragQueryPoint16(HDROP16 hDrop
, POINT16
*p
)
163 LPDROPFILESTRUCT16 lpDropFileStruct
;
166 lpDropFileStruct
= (LPDROPFILESTRUCT16
) GlobalLock16(hDrop
);
168 memcpy(p
,&lpDropFileStruct
->ptMousePos
,sizeof(POINT16
));
169 bRet
= lpDropFileStruct
->fInNonClientArea
;
171 GlobalUnlock16(hDrop
);
175 /*************************************************************************
176 * FindExecutable (SHELL.21)
178 HINSTANCE16 WINAPI
FindExecutable16( LPCSTR lpFile
, LPCSTR lpDirectory
,
180 { return HINSTANCE_16(FindExecutableA( lpFile
, lpDirectory
, lpResult
));
183 /*************************************************************************
184 * AboutDlgProc (SHELL.33)
186 BOOL16 WINAPI
AboutDlgProc16( HWND16 hWnd
, UINT16 msg
, WPARAM16 wParam
,
194 /*************************************************************************
195 * ShellAbout (SHELL.22)
197 BOOL16 WINAPI
ShellAbout16( HWND16 hWnd
, LPCSTR szApp
, LPCSTR szOtherStuff
, HICON16 icon16
)
199 HICON icon
= convert_icon_to_32( icon16
);
200 BOOL ret
= ShellAboutA( HWND_32(hWnd
), szApp
, szOtherStuff
, icon
);
205 /*************************************************************************
206 * InternalExtractIcon [SHELL.39]
208 * This abortion is called directly by Progman
210 HGLOBAL16 WINAPI
InternalExtractIcon16(HINSTANCE16 hInstance
,
211 LPCSTR lpszExeFileName
, UINT16 nIconIndex
, WORD n
)
214 HICON16
*RetPtr
= NULL
;
216 TRACE("(%04x,file %s,start %d,extract %d\n",
217 hInstance
, lpszExeFileName
, nIconIndex
, n
);
222 hRet
= GlobalAlloc16(GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(*RetPtr
) * n
);
223 RetPtr
= GlobalLock16(hRet
);
225 if (nIconIndex
== (UINT16
)-1) /* get number of icons */
227 RetPtr
[0] = PrivateExtractIconsA(lpszExeFileName
, 0, 0, 0, NULL
, NULL
, 0, LR_DEFAULTCOLOR
);
234 icons
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(*icons
));
235 ret
= PrivateExtractIconsA(lpszExeFileName
, nIconIndex
,
236 GetSystemMetrics(SM_CXICON
),
237 GetSystemMetrics(SM_CYICON
),
238 icons
, NULL
, n
, LR_DEFAULTCOLOR
);
239 if ((ret
!= 0xffffffff) && ret
)
242 for (i
= 0; i
< n
; i
++) RetPtr
[i
] = HICON_16(icons
[i
]);
249 HeapFree(GetProcessHeap(), 0, icons
);
254 /*************************************************************************
255 * ExtractIcon (SHELL.34)
257 HICON16 WINAPI
ExtractIcon16( HINSTANCE16 hInstance
, LPCSTR lpszExeFileName
,
260 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance
), lpszExeFileName
, nIconIndex
));
263 /*************************************************************************
264 * ExtractIconEx (SHELL.40)
266 HICON16 WINAPI
ExtractIconEx16(
267 LPCSTR lpszFile
, INT16 nIconIndex
, HICON16
*phiconLarge
,
268 HICON16
*phiconSmall
, UINT16 nIcons
270 HICON
*ilarge
,*ismall
;
275 ilarge
= HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
279 ismall
= HeapAlloc(GetProcessHeap(),0,nIcons
*sizeof(HICON
));
282 ret
= HICON_16(ExtractIconExA(lpszFile
,nIconIndex
,ilarge
,ismall
,nIcons
));
284 for (i
=0;i
<nIcons
;i
++)
285 phiconLarge
[i
]=HICON_16(ilarge
[i
]);
286 HeapFree(GetProcessHeap(),0,ilarge
);
289 for (i
=0;i
<nIcons
;i
++)
290 phiconSmall
[i
]=HICON_16(ismall
[i
]);
291 HeapFree(GetProcessHeap(),0,ismall
);
296 /*************************************************************************
297 * ExtractAssociatedIcon [SHELL.36]
299 * Return icon for given file (either from file itself or from associated
300 * executable) and patch parameters if needed.
302 HICON16 WINAPI
ExtractAssociatedIcon16(HINSTANCE16 hInst
, LPSTR lpIconPath
, LPWORD lpiIcon
)
304 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst
), lpIconPath
,
308 /*************************************************************************
309 * FindEnvironmentString [SHELL.38]
311 * Returns a pointer into the DOS environment... Ugh.
313 static LPSTR
SHELL_FindString(LPSTR lpEnv
, LPCSTR entry
)
319 for( ; *lpEnv
; lpEnv
+=strlen(lpEnv
)+1 )
320 { if( strncasecmp(lpEnv
, entry
, l
) )
323 return (lpEnv
+ l
); /* empty entry */
324 else if ( *(lpEnv
+l
)== '=' )
325 return (lpEnv
+ l
+ 1);
330 /**********************************************************************/
332 SEGPTR WINAPI
FindEnvironmentString16(LPCSTR str
)
334 LPSTR lpEnv
,lpString
;
337 spEnv
= GetDOSEnvironment16();
339 lpEnv
= MapSL(spEnv
);
340 lpString
= (spEnv
)?SHELL_FindString(lpEnv
, str
):NULL
;
342 if( lpString
) /* offset should be small enough */
343 return spEnv
+ (lpString
- lpEnv
);
347 /*************************************************************************
348 * DoEnvironmentSubst [SHELL.37]
350 * Replace %KEYWORD% in the str with the value of variable KEYWORD
351 * from "DOS" environment. If it is not found the %KEYWORD% is left
352 * intact. If the buffer is too small, str is not modified.
355 * str [I] '\0' terminated string with %keyword%.
356 * [O] '\0' terminated string with %keyword% substituted.
357 * length [I] size of str.
360 * str length in the LOWORD and 1 in HIWORD if subst was successful.
362 DWORD WINAPI
DoEnvironmentSubst16(LPSTR str
,WORD length
)
364 LPSTR lpEnv
= MapSL(GetDOSEnvironment16());
367 LPSTR lpBuffer
= HeapAlloc( GetProcessHeap(), 0, length
);
372 WORD retLength
= length
;
376 TRACE("accept %s\n", str
);
378 while( *lpstr
&& bufCnt
<= length
- 1 ) {
379 if ( *lpstr
!= '%' ) {
380 lpBuffer
[bufCnt
++] = *lpstr
++;
384 for( lpend
= lpstr
+ 1; *lpend
&& *lpend
!= '%'; lpend
++) /**/;
386 envKeyLen
= lpend
- lpstr
- 1;
387 if( *lpend
!= '%' || envKeyLen
== 0)
388 goto err
; /* "%\0" or "%%" found; back off and whine */
391 lpKey
= SHELL_FindString(lpEnv
, lpstr
+1);
394 int l
= strlen(lpKey
);
396 if( bufCnt
+ l
> length
- 1 )
399 memcpy(lpBuffer
+ bufCnt
, lpKey
, l
);
401 } else { /* Keyword not found; Leave the %KEYWORD% intact */
402 if( bufCnt
+ envKeyLen
+ 2 > length
- 1 )
405 memcpy(lpBuffer
+ bufCnt
, lpstr
, envKeyLen
+ 2);
406 bufCnt
+= envKeyLen
+ 2;
412 if (!*lpstr
&& bufCnt
<= length
- 1) {
413 memcpy(str
,lpBuffer
, bufCnt
);
415 retLength
= bufCnt
+ 1;
421 WARN("-- Env subst aborted - string too short or invalid input\n");
422 TRACE("-- return %s\n", str
);
425 HeapFree( GetProcessHeap(), 0, lpBuffer
);
427 return (DWORD
)MAKELONG(retLength
, retStatus
);
430 /*************************************************************************
433 * 32-bit version of the system-wide WH_SHELL hook.
435 static LRESULT WINAPI
SHELL_HookProc(INT code
, WPARAM wParam
, LPARAM lParam
)
437 TRACE("%i, %lx, %08lx\n", code
, wParam
, lParam
);
443 case HSHELL_WINDOWCREATED
:
444 PostMessageA( SHELL_hWnd
, uMsgWndCreated
, wParam
, 0 );
446 case HSHELL_WINDOWDESTROYED
:
447 PostMessageA( SHELL_hWnd
, uMsgWndDestroyed
, wParam
, 0 );
449 case HSHELL_ACTIVATESHELLWINDOW
:
450 PostMessageA( SHELL_hWnd
, uMsgShellActivate
, wParam
, 0 );
454 return CallNextHookEx( SHELL_hHook
, code
, wParam
, lParam
);
457 /*************************************************************************
458 * ShellHookProc [SHELL.103]
459 * System-wide WH_SHELL hook.
461 LRESULT WINAPI
ShellHookProc16(INT16 code
, WPARAM16 wParam
, LPARAM lParam
)
463 return SHELL_HookProc( code
, wParam
, lParam
);
466 /*************************************************************************
467 * RegisterShellHook [SHELL.102]
469 BOOL WINAPI
RegisterShellHook16(HWND16 hWnd
, UINT16 uAction
)
471 TRACE("%04x [%u]\n", hWnd
, uAction
);
475 case 2: /* register hWnd as a shell window */
478 SHELL_hHook
= SetWindowsHookExA( WH_SHELL
, SHELL_HookProc
,
479 GetModuleHandleA("shell32.dll"), 0 );
482 uMsgWndCreated
= RegisterWindowMessageA( lpstrMsgWndCreated
);
483 uMsgWndDestroyed
= RegisterWindowMessageA( lpstrMsgWndDestroyed
);
484 uMsgShellActivate
= RegisterWindowMessageA( lpstrMsgShellActivate
);
487 WARN("-- unable to install ShellHookProc()!\n");
491 return ((SHELL_hWnd
= HWND_32(hWnd
)) != 0);
495 WARN("-- unknown code %i\n", uAction
);
496 SHELL_hWnd
= 0; /* just in case */
502 /***********************************************************************
503 * DriveType (SHELL.262)
505 UINT16 WINAPI
DriveType16( UINT16 drive
)
508 char path
[] = "A:\\";
510 ret
= GetDriveTypeA(path
);
511 switch(ret
) /* some values are not supported in Win16 */
516 case DRIVE_NO_ROOT_DIR
:
524 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
525 * some programs. Do not remove those cases. -MM
527 static inline void fix_win16_hkey( HKEY
*hkey
)
529 if (*hkey
== 0 || *hkey
== (HKEY
)1) *hkey
= HKEY_CLASSES_ROOT
;
532 /******************************************************************************
533 * RegOpenKey [SHELL.1]
535 DWORD WINAPI
RegOpenKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
537 fix_win16_hkey( &hkey
);
538 return RegOpenKeyA( hkey
, name
, retkey
);
541 /******************************************************************************
542 * RegCreateKey [SHELL.2]
544 DWORD WINAPI
RegCreateKey16( HKEY hkey
, LPCSTR name
, PHKEY retkey
)
546 fix_win16_hkey( &hkey
);
547 return RegCreateKeyA( hkey
, name
, retkey
);
550 /******************************************************************************
551 * RegCloseKey [SHELL.3]
553 DWORD WINAPI
RegCloseKey16( HKEY hkey
)
555 fix_win16_hkey( &hkey
);
556 return RegCloseKey( hkey
);
559 /******************************************************************************
560 * RegDeleteKey [SHELL.4]
562 DWORD WINAPI
RegDeleteKey16( HKEY hkey
, LPCSTR name
)
564 fix_win16_hkey( &hkey
);
565 return RegDeleteKeyA( hkey
, name
);
568 /******************************************************************************
569 * RegSetValue [SHELL.5]
571 DWORD WINAPI
RegSetValue16( HKEY hkey
, LPCSTR name
, DWORD type
, LPCSTR data
, DWORD count
)
573 fix_win16_hkey( &hkey
);
574 return RegSetValueA( hkey
, name
, type
, data
, count
);
577 /******************************************************************************
578 * RegQueryValue [SHELL.6]
581 * Is this HACK still applicable?
584 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
585 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
588 DWORD WINAPI
RegQueryValue16( HKEY hkey
, LPCSTR name
, LPSTR data
, LPDWORD count
591 fix_win16_hkey( &hkey
);
592 if (count
) *count
&= 0xffff;
593 return RegQueryValueA( hkey
, name
, data
, (LONG
*) count
);
596 /******************************************************************************
597 * RegEnumKey [SHELL.7]
599 DWORD WINAPI
RegEnumKey16( HKEY hkey
, DWORD index
, LPSTR name
, DWORD name_len
)
601 fix_win16_hkey( &hkey
);
602 return RegEnumKeyA( hkey
, index
, name
, name_len
);
605 /*************************************************************************
606 * SHELL_Execute16 [Internal]
608 static UINT_PTR
SHELL_Execute16(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
609 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
613 WideCharToMultiByte(CP_ACP
, 0, lpCmd
, -1, sCmd
, MAX_PATH
, NULL
, NULL
);
614 ret
= WinExec16(sCmd
, (UINT16
)psei
->nShow
);
615 psei_out
->hInstApp
= HINSTANCE_32(ret
);
619 /*************************************************************************
620 * ShellExecute [SHELL.20]
622 HINSTANCE16 WINAPI
ShellExecute16( HWND16 hWnd
, LPCSTR lpOperation
,
623 LPCSTR lpFile
, LPCSTR lpParameters
,
624 LPCSTR lpDirectory
, INT16 iShowCmd
)
626 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd
), lpOperation
, lpFile
, lpParameters
,
627 lpDirectory
, iShowCmd
, SHELL_Execute16
));
631 /*************************************************************************
634 * Only exported from shell32 on Windows, probably imported
635 * from shell through the 16/32 thunks.
637 void WINAPI
RunDLL_CallEntry16( DWORD proc
, HWND hwnd
, HINSTANCE inst
, LPCSTR cmdline
, INT cmdshow
)
642 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
643 proc
, hwnd
, inst
, debugstr_a(cmdline
), cmdshow
);
645 cmdline_seg
= MapLS( cmdline
);
646 args
[4] = HWND_16(hwnd
);
647 args
[3] = MapHModuleLS(inst
);
648 args
[2] = SELECTOROF(cmdline_seg
);
649 args
[1] = OFFSETOF(cmdline_seg
);
651 WOWCallback16Ex( proc
, WCB16_PASCAL
, sizeof(args
), args
, NULL
);
652 UnMapLS( cmdline_seg
);